#include #include #include #include #include "MyUtil.h" #include "ActionT.h" #include "WordT.h" using namespace std; const size_t MAX_WORDS{5000}; size_t Find( const WordT ary[], size_t count, string key); void Swap(WordT & a, WordT & b); void Sort(WordT ary[], size_t count); string FixWord(string word); string GetFileName(); void PrintArray( const WordT ary[], size_t count); void ReadWords(WordT wordAry[], size_t & count); ActionT GetUserAction(); void PerformUserAction(ActionT action, bool & done); int main() { bool done{false}; ActionT action{ActionT::QUIT}; WordT words[MAX_WORDS]; size_t wordCount{0}; ReadWords( words, wordCount); Sort(words, wordCount); PrintArray(words, wordCount); while (not done) { action = GetUserAction(); PerformUserAction(action, done); } return 0; } void Swap(WordT & a, WordT & b) { WordT tmp; tmp = a; a = b; b = tmp; return; } void Sort(WordT ary[], size_t count){ size_t current, search, smallest; for( current = 0; current < count-1; current++) { smallest = current; for(search = current +1; search < count; search++) { if (ary[smallest].Word() > ary[search].Word()) { smallest = search; } } if (search != smallest) { Swap(ary[smallest],ary[current]); } } return; } size_t Find( const WordT ary[], size_t count, string key){ size_t current; //size_t rv{MAX_WORDS}; /*(for( current = 0; current < count; current++) { if (ary[current].Word() == key) { rv = current; } } */ /* current = 0; while (current < count and rv != MAX_WORDS) { if (ary[current].Word() == key) { rv = current; } current++; } */ // && & and // || | or // ! not current = 0; while (current < count and ary[current].Word() != key) { current++; } return current; } void PrintArray( const WordT ary[], size_t count){ size_t i; for(i =0; i < count; i++) { cout << setw(20) << ary[i].GetWord() << setw(4) << ary[i].GetCount() << endl; } return; } string GetFileName() { string name; cout << "Enter the name of the input file: "; cin >> name; cout << endl; return name; } string FixWord(string word) { string fixed; fixed = Strip(word); fixed = MakeLower(fixed); return fixed; } void AddAWord(WordT wordAry[], size_t & count, string word){ size_t pos; word = FixWord(word); if (word != ""){ pos = Find(wordAry, count, word); if (pos < count) { wordAry[pos].IncrementCount(); } else if (count < MAX_WORDS) { wordAry[count].SetWord(word); count++; } else { cout << "No space in the array for " << word << endl; } } return; } void ReadWords(WordT wordAry[], size_t & count){ string fileName; ifstream inFile; string word; fileName = GetFileName(); inFile.open(fileName); if (inFile) { count = 0; inFile >> word; while (inFile) { AddAWord(wordAry, count, word); inFile >> word; } inFile.close(); } return; } ActionT GetUserAction(){ cout << "Getting the user action" << endl; return ActionT::QUIT; } void PerformUserAction(ActionT action, bool & done) { cout << "Performing the user action" << endl; done = action == ActionT::QUIT; return; }