Hi, Question: Make this cpp file into 2header and 2 cpp, 5 files total? #include
ID: 3665802 • Letter: H
Question
Hi, Question: Make this cpp file into 2header and 2 cpp, 5 files total?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string readProgramFromFile(string filename);
string getBracesFromString(string programStr);
string getBinaryString(string wellFormedString);
size_t uiDistance(const string &s1, const string &s2);
/*
* Entry point
*/
int main()
{
string programA = "";
string programB = "";
programA = readProgramFromFile("inputA.cpp");
programB = readProgramFromFile("inputB.cpp");
if( programA.length() == 0 || programB.length() == 0 )
{
cout << "programA or programB invalid, exiting.";
return 0;
}
cout << endl << endl;
//cout << "programA:" << endl;
//cout << programA << endl;
//cout << endl;
programA = getBracesFromString(programA);
cout << "programA(Braces): " << programA << endl;
programA = getBinaryString(programA);
cout << "programA(BitString): " << programA << endl;
//cout << "programB:" << endl;
//cout << programB << endl;
//cout << endl;
programB = getBracesFromString(programB);
cout << "programB(Braces): " << programB << endl;
programB = getBinaryString(programB);
cout << "programB(BitString): " << programB << endl;
int largerLength = programA.length();
if( programB.length() > largerLength )
{
largerLength = programB.length();
}
int distance = uiDistance(programA, programB);
float similarity = 1 - ( distance/float(largerLength) );
similarity = similarity * 100;
cout << endl;
cout << "similarity measure: " << similarity << "%" << endl;
if( similarity > 50 )
{
cout << "PLAGIARISM" << endl;
}
else
{
cout << "NO PLAGIARISM" << endl;
}
return 0;
}
/*
* Responsible for calculating the similarity between two strings using the Levenshtein distance metric
*
* // info: https://en.wikipedia.org/wiki/Levenshtein_distance
* // credit to: http://rosettacode.org/wiki/Levenshtein_distance
* @param s1: first candidate string for similarity test
* @param s2: second candidate string for similarity test
* @param return: numeric measure of distance
*/
size_t uiDistance(const string &s1, const string &s2)
{
const size_t m(s1.size());
const size_t n(s2.size());
if( m==0 )
{
return n;
}
if( n==0 )
{
return m;
}
size_t *costs = new size_t[n + 1];
for( size_t k=0; k<=n; k++ )
{
costs[k] = k;
}
size_t i = 0;
for ( std::string::const_iterator it1 = s1.begin(); it1 != s1.end(); ++it1, ++i )
{
costs[0] = i+1;
size_t corner = i;
size_t j = 0;
for ( std::string::const_iterator it2 = s2.begin(); it2 != s2.end(); ++it2, ++j )
{
size_t upper = costs[j+1];
if( *it1 == *it2 )
{
costs[j+1] = corner;
}
else
{
size_t t(upper<corner?upper:corner);
costs[j+1] = (costs[j]<t?costs[j]:t)+1;
}
corner = upper;
}
}
size_t result = costs[n];
delete [] costs;
return result;
}
/*
* Responsible for converting "{" and "}" to bitstring 0 and 1 respectively
*
* @param wellFormedString: string consisting of "{" and "}"
* @param return: bistring due to processing wellFormedString
*/
string getBinaryString(string wellFormedString)
{
string bitString = "";
for(int i = 0; i<wellFormedString.length(); i++)
{
if( wellFormedString[i] == '{' )
{
bitString = bitString + '0';
}
else if( wellFormedString[i] == '}' )
{
bitString = bitString + '1';
}
}
return bitString;
}
/*
* Responsible for removing all text excluding "{" and "}" from plaintext program
*
* @param programStr: c++ program text
* @param return: c++ program string stripped of all characters excluding "{" and "}"
*/
string getBracesFromString(string programStr)
{
string wellFormedString = "";
for(int i = 0; i<programStr.length(); i++)
{
if( programStr[i] == '{' || programStr[i] == '}' )
{
wellFormedString = wellFormedString + programStr[i];
}
}
return wellFormedString;
}
/*
* Responsible for reading program text from file
*
* @param filename: name of program file
* @param return: c++ program string
*/
string readProgramFromFile(string filename)
{
string programText = "";
//input stream for data
ifstream inFile;
//open file
inFile.open(filename.c_str());
//error handling in case file does not exist was unable to open - start
if (!inFile)
{
cout << "The inventory input file " << filename << " does not exist." << endl;
return "";
}
//error handling in case file does not exist was unable to open - end
cout << "Success opening " << filename <<endl;
//read inventory from file - start
string line;
while( !inFile.eof() )
{
getline (inFile, line);
programText = programText + line;
}
inFile.close();
return programText;
}
Explanation / Answer
//MAIN FILE
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string readProgramFromFile(string filename);
string getBracesFromString(string programStr);
string getBinaryString(string wellFormedString);
size_t uiDistance(const string &s1, const string &s2);
/*
* Entry point
*/
int main()
{
string programA = "";
string programB = "";
programA = readProgramFromFile("inputA.cpp");
programB = readProgramFromFile("inputB.cpp");
if( programA.length() == 0 || programB.length() == 0 )
{
cout << "programA or programB invalid, exiting.";
return 0;
}
cout << endl << endl;
//cout << "programA:" << endl;
//cout << programA << endl;
//cout << endl;
programA = getBracesFromString(programA);
cout << "programA(Braces): " << programA << endl;
programA = getBinaryString(programA);
cout << "programA(BitString): " << programA << endl;
//cout << "programB:" << endl;
//cout << programB << endl;
//cout << endl;
programB = getBracesFromString(programB);
cout << "programB(Braces): " << programB << endl;
programB = getBinaryString(programB);
cout << "programB(BitString): " << programB << endl;
int largerLength = programA.length();
if( programB.length() > largerLength )
{
largerLength = programB.length();
}
int distance = uiDistance(programA, programB);
float similarity = 1 - ( distance/float(largerLength) );
similarity = similarity * 100;
cout << endl;
cout << "similarity measure: " << similarity << "%" << endl;
if( similarity > 50 )
{
cout << "PLAGIARISM" << endl;
}
else
{
cout << "NO PLAGIARISM" << endl;
}
return 0;
}
//FIRST CPP FILE
/*
* Responsible for calculating the similarity between two strings using the Levenshtein distance metric
*
* // info: https://en.wikipedia.org/wiki/Levenshtein_distance
* // credit to: http://rosettacode.org/wiki/Levenshtein_distance
* @param s1: first candidate string for similarity test
* @param s2: second candidate string for similarity test
* @param return: numeric measure of distance
*/
size_t uiDistance(const string &s1, const string &s2)
{
const size_t m(s1.size());
const size_t n(s2.size());
if( m==0 )
{
return n;
}
if( n==0 )
{
return m;
}
size_t *costs = new size_t[n + 1];
for( size_t k=0; k<=n; k++ )
{
costs[k] = k;
}
size_t i = 0;
for ( std::string::const_iterator it1 = s1.begin(); it1 != s1.end(); ++it1, ++i )
{
costs[0] = i+1;
size_t corner = i;
size_t j = 0;
for ( std::string::const_iterator it2 = s2.begin(); it2 != s2.end(); ++it2, ++j )
{
size_t upper = costs[j+1];
if( *it1 == *it2 )
{
costs[j+1] = corner;
}
else
{
size_t t(upper<corner?upper:corner);
costs[j+1] = (costs[j]<t?costs[j]:t)+1;
}
corner = upper;
}
}
size_t result = costs[n];
delete [] costs;
return result;
}
//SECOND CPP FILE
/*
* Responsible for converting "{" and "}" to bitstring 0 and 1 respectively
*
* @param wellFormedString: string consisting of "{" and "}"
* @param return: bistring due to processing wellFormedString
*/
string getBinaryString(string wellFormedString)
{
string bitString = "";
for(int i = 0; i<wellFormedString.length(); i++)
{
if( wellFormedString[i] == '{' )
{
bitString = bitString + '0';
}
else if( wellFormedString[i] == '}' )
{
bitString = bitString + '1';
}
}
return bitString;
}
//FIRST HEADER
/*
* Responsible for removing all text excluding "{" and "}" from plaintext program
*
* @param programStr: c++ program text
* @param return: c++ program string stripped of all characters excluding "{" and "}"
*/
string getBracesFromString(string programStr)
{
string wellFormedString = "";
for(int i = 0; i<programStr.length(); i++)
{
if( programStr[i] == '{' || programStr[i] == '}' )
{
wellFormedString = wellFormedString + programStr[i];
}
}
return wellFormedString;
}
//SECOND HEADER
/*
* Responsible for reading program text from file
*
* @param filename: name of program file
* @param return: c++ program string
*/
string readProgramFromFile(string filename)
{
string programText = "";
//input stream for data
ifstream inFile;
//open file
inFile.open(filename.c_str());
//error handling in case file does not exist was unable to open - start
if (!inFile)
{
cout << "The inventory input file " << filename << " does not exist." << endl;
return "";
}
//error handling in case file does not exist was unable to open - end
cout << "Success opening " << filename <<endl;
//read inventory from file - start
string line;
while( !inFile.eof() )
{
getline (inFile, line);
programText = programText + line;
}
inFile.close();
return programText;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.