// Assignment 11: Spell Check Program Solution #include #include #include #include #include #include #include using namespace std; const int DictionarySize = 20000; const int KeyWordsSize = 84; void getFile(const string& filename,string*, int filesize); bool wordIsInDictionary(const string& word, const string* dictionary); void sort(string* list, int size); void swap(string&, string&); int main() { const string wordFile = "c:/temp/unsorted_dictionary.txt"; const string keywordFile = "c:/temp/keywords.txt"; string word; int notfoundWords = 0; string dictionary[DictionarySize]; string keywords[KeyWordsSize]; getFile(wordFile, dictionary, DictionarySize); getFile(keywordFile, keywords, KeyWordsSize); cout << "Sorting Dictionary data ... "; sort(dictionary, DictionarySize); cout << "done" << endl; cout << "Sorting keyword data ... "; sort(keywords,KeyWordsSize); cout << "done" << endl << endl; for (int i= 0; i < KeyWordsSize; i++) { if (!wordIsInDictionary(keywords[i],dictionary)) { cout << "keyword not found: " << keywords[i]<< endl; notfoundWords++; } } cout << endl << "Number of keywords not found = " << notfoundWords << endl; } void getFile(const string& filename, string* array, int size) { ifstream fin(filename.c_str()); if (!fin) { cerr << "Unable to open word file " << filename << endl; exit(1); } cout << "Reading file: " << filename << endl; for (int i = 0; i < size; i++) { fin >> array[i]; } fin.close(); } bool wordIsInDictionary(const string& word, const string* dictionary) { for (int i = 0 ; i < DictionarySize; i++) if (word == dictionary[i]) return true; return false; } void sort(string* a, int size) { int minIndex; for (int i = 0; i < size - 1; i++) { minIndex = i; for (int j = i+1; j < size; j++) { if (a[j] < a[minIndex]) { minIndex = j; } } if(minIndex != i) { swap(a[i],a[minIndex]); } } } void swap(string& a, string& b) { string temp = a; a = b; b = temp; }