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 #include #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 #include #include "sub.h" using namespace std;

template void print_list(list); template void print_lines(list); void alpha_insert(string, list&); string strip_punct(string);

int main() { list wdList; string next;

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 void print_list(List < T> lst) { cout << endl; typename List::iterator itr; for (itr = lst.begin(); itr != lst.end(); ++itr) cout << *itr << " "; cout << endl; }

template void print_lines(List lst) { cout << endl; typename List::iterator itr; for (itr = lst.begin(); itr != lst.end(); ++itr) cout << *itr << " " << endl; cout << endl; }

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& wdlst) { WordItem wordit;

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!