#include #include "WordArraySupport.h" using namespace std; size_t FindWord(const WordCountT & word, const WordArrayT & words){ bool found{false}; size_t i{0}; while (not found and i < words.Size()) { if (words[i] == word) { found = true; } else { ++i; } } return i; } void Swap(WordCountT & a, WordCountT & b) { WordCountT tmp{a}; a = b; b = tmp; } void SortByWord(WordArrayT & words){ for(size_t current = 0; current < words.Size(); ++current) { size_t small = current; for(size_t j = current+1; j < words.Size() ; ++j) { if (words[j] < words[small]) { small = j; } } if (small != current) { Swap(words[current], words[small]); } } } void SortByCount(WordArrayT & words){ for(size_t current = 0; current < words.Size(); ++current) { size_t small = current; for(size_t j = current+1; j < words.Size() ; ++j) { if (words[j].CountCompare(words[small])) { small = j; } } if (small != current) { Swap(words[current], words[small]); } } } void AddWord(WordArrayT & words, string newWord){ WordCountT word{newWord}; if (word.Word() != "") { size_t pos = FindWord(word, words); if(pos == words.Size() ) { /* if (words.Size() < words.Capacity() ) { words.PushBack(word); } else { cerr << "Could not add " << newWord << ", no space left in the array." << endl; } */ words.PushBack(word); } else { ++words[pos]; } } } void PrintWords(const WordArrayT & words){ cout << endl; cout << "The array holds " << words.Size() << " elements." << endl; cout << "The array can hold " << words.Capacity() << " elements." << endl; cout << "The elements are: " << endl; for(size_t i = 0; i < words.Size(); ++i) { cout << "\t" << words[i] << endl; } cout << endl; }