Question: Need help /sub.h #include #include #include using namespace std; class WordItem { public: WordItem() :word(), count(0) {} WordItem(string wd) :word(wd), count(1) {} WordItem(const WordItem& wd)
Need help
/sub.h
#include
using namespace std;
class WordItem { public: WordItem() :word(""), count(0) {}
WordItem(string wd) :word(wd), count(1) {}
WordItem(const WordItem& wd) :word(wd.word), count(wd.count) {}
string get_word() const { return word; }
int get_count() const { return count; }
void incrementCount() { count++; }
private: string word; int count; };
ostream& operator << (ostream& os, const WordItem& wd) { os << wd.get_word() << ":" << wd.get_count() << " "; return os; }
//main.cpp
#include #include "sub.h" using namespace std;
template
int main() { list
ifstream inp; inp.open("your_choice_text.txt"); inp >> next; next = strip_punct(next); while (!inp.fail()) { alpha_insert(next, wdList); inp >> next; next = strip_punct(next); } inp.close();
//prints each word and its # of occurances from the choice text. don't mind if it needs a change in here cout << "Word and their counts: "; print_list(wdList);
//Need help here: Iterate over the wdList and determine the word among all WordItems that has a maximal count
//help here as well: report "the most frequent word" [word] " with a count of " [count] return 0; }
template
template
string strip_punct(string x) { for (int i = x.size() - 1; i >= 0; i--) { if (ispunct(x[i])) { x.erase(i--, 1); i++; }
else { return x; } }
return x; }
void alpha_insert(string x, List
if (wdlst.empty()) { wordit = WordItem(x); wdlst.insert(wdlst.begin(), wordit); return; }
/* lastly here as well: else, 1.) A for loop to iterate over the wordlist and check for each WordItem *itr in the current whether its word component is equal to the string x to be inserted.
1a.) if there is a match, update the count of *itr and return. 1b.) if there is no match between x and the word *itr, check whether x is alphabeitically begore the word *itr, is so a new WordItem for x is created and inserted at the current iterator position.
2.) if the program has not returned from within the for loop, string x is not yet in the wordlist and ranks alpahbertically after all words currently in the list; create a new wordItem for x and add it to the end of the wordlist and return. */
return; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
