#include #include "DictionaryT.h" using namespace std; DictionaryT::DictionaryT(){ } void DictionaryT::Sort(){ size_t current, small, j; for(current = 0; current < size; ++current) { small = current; for(j = current + 1; j < size; ++j) { if (data[j].Less(data[small])) { small = j; } } if (small != current) { DictionaryEntryT tmp{data[small]}; data[small] = data[current]; data[current] = tmp; } } } size_t DictionaryT::MyFind(DictionaryEntryT key) const{ size_t pos{0}; while (pos < size and not data[pos].Same(key)) { ++pos; } return pos; } size_t DictionaryT::Find(const DictionaryEntryT & key) const{ size_t pos {MyFind(key)}; if (pos == size) { pos = NOT_FOUND; } return pos; } void DictionaryT::AddWord(DictionaryEntryT word){ size_t pos{MyFind(word)}; if (pos < size) { data[pos].IncrementCount(); } else { if (pos < MAX_DICTIONARY) { data[size] = word; ++size; } else { cerr << "Dictionary is full" << endl; } } } DictionaryEntryT DictionaryT::GetEntry(size_t pos) const{ DictionaryEntryT entry; if (pos < size) { entry = data[pos]; } else { cerr << "Dictionary error, index out of bounds" << endl; } return entry; } DictionaryEntryT DictionaryT::GetEntry(std::string word) const{ DictionaryEntryT key{word}; DictionaryEntryT blank; size_t pos = MyFind(key); if (pos < size) { key = data[pos]; } else { key = blank; } return key; } size_t DictionaryT::Size() const{ return size; } size_t DictionaryT::Capacity() const{ return MAX_DICTIONARY; } void DictionaryPrint(const DictionaryT & dict){ size_t i; DictionaryEntryT entry; for(i =0; i < dict.Size(); ++i) { entry = dict.GetEntry(i); cout << entry.GetWord() << " " << entry.GetCount() << endl; } }