1. Main function: Menu with a switch based on (int) from user input. Menu should
ID: 3551203 • Letter: 1
Question
1. Main function: Menu with a switch based on (int) from user input. Menu should include load
document, output document, parse document, analyze document, exit.
2. Create a class called Line. We will consider a sentence a line for this project and not a line from the
text ?le. Private var: string (C++ std lib string) str, int wordcout, int charcount. Public var: int
id. Provide the correct setters and getters for the private vars. Provide the following constructors:
default that sets id to a random number, takes a string to set str and sets a random number for id,
takes string to set str and int to set id. Have a destructor delete the string.
3. Create a class called Document. Private var: int linecont, int wordcout, array of Lines on the
heap, string name. Public var: int id. Provide the correct setters and getters. Provide the following
constructors: no default, takes string for name and random id, and takes name and id.
4. Add a function call loadDocument that takes the name of the ?le. The function will read in the text
?le and create a new Line for each sentence read with Line id equal to the location in the text ?le.
After, set the linecount.
5. Change the getWordCount getter in Line to do the following: if wordcount is not set, count the words
in the sentence, set the value, and return the value, else just return the value.
6. Change the getWordCount getter in Document to do the following: if wordcount is not set, sum up
the word counts for each line, set the value and return the value, else just return the value.
7. Add a function outputDocument in Document that takes in a string and outputs the document to a
?le at a location given by the string.
This is what I have so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
/* Start of Class Line */
class Line
{
public:
int id;
Line();
Line(string str1 ){
int id = rand();
str = new string;
str=str1;
}
~Line();
int getWordCount();
void setString(string str1);
void setWordCount(int value1);
int getCharCount();
void setCharCount(int value2);
private:
string* str;
int wordcount;
int charcount;
};
Line::Line()
{
int id = rand();
}
Line::~Line()
{
delete str;
}
void Line::setString(string str1)
{
str = str1;
}
int Line::getWordCount()
{
if (wordcount = 0)
{
}
return wordcount;
}
void Line::setWordCount(int value1)
{
wordcount = value1;
}
int Line::getCharCount()
{
return charcount;
}
void Line::setCharCount(int value2)
{
charcount = value2;
}
/* End of Class Line */
/* Start of Class Document */
class Document
{
public:
int id;
int getLineCount();
void setLineCount(int value3);
int getwordcount();
void setwordcount(int value4);
private:
int linecount;
int wordcount;
string name;
};
int Document::getLineCount()
{
return linecount;
}
void Document::setLineCount(int value3)
{
linecount = value3;
}
int Document::getwordcount()
{
return wordcount;
}
void Document::setwordcount(int value4)
{
wordcount = value4;
}
// How do you set/get with string? and array on the heap?
/* End of Class Document */
//void loadDocument()
int main()
{
// 1.
Line line();
// 2.
Line* line2 = new Line();
// array1
Line line[3];
int number;
cout << "Please enter number " << endl;
cin >> number;
switch(number)
{
case 1:
cout << "Load Document " << endl;
case 2:
cout << "Output Document " << endl;
case 3:
cout << "Parse Document " << endl;
case 4:
cout << "Analyze Document " << endl;
default:
cout << "Exit " << endl;
}
return 0;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class Line
{
private:
string str;
int wordcount;
int charcount;
public:
int id;
//methods
Line()
{
id = rand()*100;
}
Line(string str1 )
{
int id = rand()*100;
str = str1;
}
Line(string str1,int idnum)
{
int id = idnum;
str = str1;
}
~Line()
{
}
int getWordCount();
void setString(string str1);
string getString();
void setWordCount(int value1);
int getCharCount();
void setCharCount(int value2);
};
string Line ::getString()
{
return str;
}
void Line::setString(string str1)
{
str = str1;
}
int Line::getWordCount()
{
if(wordcount==0)
{
for(int i=0;i<str.length();i++)
{
if(str[i]==' ')
wordcount++;
}
wordcount++;
}
return wordcount;
}
void Line::setWordCount(int value1)
{
wordcount = value1;
}
int Line::getCharCount()
{
return charcount;
}
void Line::setCharCount(int value2)
{
charcount = value2;
}
/* End of Class Line */
/* Start of Class Document */
class Document
{
private:
int linecount;
int wordcount;
string name;
vector<Line> lines;
public:
int id;
//methods
Document(string str)
{
name = str;
id = rand()*100;
wordcount=0;
}
Document(string str,int i)
{
wordcount=0;
name = str;
id=i;
}
int getLineCount();
void setLineCount(int value3);
int getWordCount();
void setwordcount(int value4);
void adLineInDocument(Line li);
Line getLineWithNumber(int i);
};
Line Document::getLineWithNumber(int i)
{
return lines[i];
}
void Document::adLineInDocument(Line li)
{
lines.push_back(li);
}
int Document::getLineCount()
{
return linecount;
}
void Document::setLineCount(int value3)
{
linecount = value3;
}
int Document::getWordCount()
{
if(wordcount==0)
{
for(int i=0;i<linecount;i++)
{
wordcount+=lines[i].getWordCount();
}
}
return wordcount;
}
void Document::setwordcount(int value4)
{
wordcount = value4;
}
Document *loadDocument(string docname)
{
Document *doc = new Document(docname,1);
int totalLines=0;
ifstream myfile;
myfile.open(docname.c_str() ,ifstream::in);
if(!myfile.is_open())
{
cout<<"unable to open file ";
return 0;
}
int totalWords=0;
while(!myfile.eof())
{
string sentance = "";
string word="";
int wordlength;
int wordCount=0;
int startFlag=1;
while(!myfile.eof())
{
if(startFlag==1)
{
wordCount=0;
startFlag=0;
}
myfile>>word;
wordlength = word.length();
sentance+=word;
wordCount++;
if(word[wordlength-1]=='.')
{
totalLines++;
startFlag=1;
break;
}
sentance+=" ";
}
totalWords+=wordCount;
int lineId = int(myfile.tellg())-sentance.length();
Line li(sentance,lineId);//set line id to location in text
li.setCharCount(sentance.length());//set number of char in sentence
li.setWordCount(wordCount);//set wordcount in each line
doc->adLineInDocument(li);//add line into heap array
}
doc->setLineCount(totalLines);
doc->setwordcount(totalWords);
//just to seee its working fine
cout<<"Info of document : ";
cout<<"Total Lines :"<<doc->getLineCount()<<endl;
cout<<"Total Words :"<<doc->getWordCount()<<endl;
cout<<"Info of each line : ";
for(int k=0;k<doc->getLineCount();k++)
{
Line sentence = doc->getLineWithNumber(k);
cout<<" word count :"<<sentence.getWordCount()<<endl;
cout<<"char Count :"<<sentence.getCharCount()<<endl;
cout<<"Sentence :"<<sentence.getString();
}
return doc;
}
int main()
{
int number;
cout<<"Menu ";
cout<<"1. Load Document 2. Output Document 3. Parse Document 4. Analyse Document 5. exit";
cout<<" Select a choice: ";
cin>>number;
switch(number)
{
case 1:
cout << "Load Document " << endl;
loadDocument("docname.txt");
break;
case 2:
cout << "Output Document " << endl;
break;
case 3:
cout << "Parse Document " << endl;
break;
case 4:
cout << "Analyze Document " << endl;
break;
default:
cout << "Exit " << endl;
break;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.