#include #include #include "WordListT.h" #include "WordT.h" using namespace std; WordListT::WordListT(void){ wordCount = 0; current = 0; } bool WordListT::AddWord(std::string word){ int pos; WordT tmp; bool good{true}; pos = FindWord(word); if (pos < wordCount) { data[pos].IncrementCount(); } else if (wordCount < MAX_WORDS) { tmp.Word(word); data[wordCount] = tmp; wordCount++; } else { cout << "Error: The word list is full, can not add " << word << endl; good = false; } return good; } void Swap(WordT & a, WordT & b) { WordT tmp = a; a = b; b = tmp; } void WordListT::SortWords(void){ int i,j, small; for(i =0; i < wordCount -1; i ++) { small =i; for(j =i+1; j < wordCount; j++) { if (data[small].GetWord() > data[j].GetWord()) { small = j; } } if (small != i) { Swap(data[i],data[small]); } } } int WordListT::Size(void) const{ return wordCount; } void WordListT::FirstWord(void) { current = 0; } WordT WordListT::CurrentWord(void) const { WordT rv; if (current < wordCount) { rv = data[current]; } return rv; } bool WordListT::NextWord(void) { bool rValue{false}; if (current < wordCount) { current ++; rValue = true; } return rValue; } int WordListT::FindWord(std::string word) { int pos{0}; while (pos < wordCount and data[pos].Word() != word) { pos++; } return pos; }