#include #include #include "WordListT.h" #include "WordT.h" using namespace std; WordListT::WordListT(void){ wordCount = 0; current = 0; data=nullptr; } WordT * CopyArray(WordT ary[], int size, bool increase) { WordT * rValue{nullptr}; int i; int newSize = size; if (increase) { newSize++; } rValue = new WordT [newSize]; for (i =0; i < size; i++) { rValue[i] = ary[i]; } return rValue; } WordListT::WordListT(WordListT & other) { data = CopyArray(other.data, other.wordCount, false); wordCount = other.wordCount; current = other.current; } WordListT::~WordListT() { if (data != nullptr) { delete [] data; } } bool WordListT::AddWord(std::string word){ int pos; WordT tmp; WordT * newList; pos = FindWord(word); if (pos < wordCount) { data[pos].IncrementCount(); } else { if (data != nullptr) { newList = CopyArray(data, wordCount, true); delete [] data; data = newList; } else { data = new WordT[1]; } tmp.Word(word); data[wordCount] = tmp; wordCount++; } return true; } 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; }