/* - BEGIN OF FILE - */ /* - include statements - */ #include #include #include #include #include using namespace std; /* - - - - - - - - - - - */ /* - global variables - */ const int DICT_SIZE = 250000; const int ALPHABET_SIZE = 26; const int FAILED = -1; const int ZERO = 0; const int ONE = 1; /* - - - - - - - - - - - */ /* - function prototypes - */ int readDict(string readin[]); int menu(); int checkString(string m); int checkInput(string m); string toLowerCase(string n); int binarySearch(string list[], int size, string target); void searchFull(string list[], string target, int size); bool match(string target, string w); /* - - - - - - - - - - - - */ int main () { /* - variable initialisation - */ string target; string input; string dictionary[DICT_SIZE]; int size; int find; /* - - - - - - - - - - - - - */ size = readDict(dictionary); if (size == FAILED) { cout << "Read Failed: Check location for dict.txt" << endl; system("pause"); return EXIT_FAILURE; } /* - enable looped menu - */ while(true) { switch(menu()) { case 1: cout << "Enter word" << endl; cin >> input; if (checkString(input) == FAILED) { cout << "Inappropriate Entry" << endl; } else { target = toLowerCase(input); find = binarySearch(dictionary,size,target); if (find == FAILED) { cout << target << " is not in dictionary" << endl; } else { cout << target << " is in dictionary" << endl; } } break; case 2: cout << "Enter word pattern with ? for missing letters " << endl; cin >> input; if (checkString(input) == FAILED) { cout << "Inappropriate Entry" << endl; } else { target = toLowerCase(input); searchFull(dictionary, target, size); } break; case 3: system("pause"); return EXIT_SUCCESS; default: cout << "Invalid Choice" << endl; } } /* - - - - - - - - - - */ } /* - functions - */ int readDict(string readin[]) { //precondition - none //postcondition - reads dict.txt into an array int loop = ZERO; string line; ifstream myfile ("dict.txt"); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); readin[loop] = line; loop++; } myfile.close(); return loop; } else { return FAILED; } } int menu() { //precondition - none //postcondition - users entry point // using a cstring so can to atoi to find menu option // as well as using isdigit function string choice; cout << "\tCrossword Helper " << endl << endl; cout << " 1 : Check if word is in dictionary " << endl; cout << " 2 : Find words in dictionary which match pattern " << endl; cout << " 3 : Quit " << endl; cin >> choice; cin.clear(); if (checkInput(choice) != ZERO) { return FAILED; } return atoi(choice.c_str()); } int checkString(string m) { //precondition - none //postcondition - checks to see whether string m contains any incorrect chars for (int i = ZERO; i < m.length(); i++) { if (isalpha(m.at(i))) { } else if (m.at(i) != '?') { return FAILED; } } return ZERO; } int checkInput(string m) { //precondition - none //postcondition - checks to see whether string m contains any incorrect chars for (int i = ZERO; i < m.length(); i++) { if (isdigit(m.at(i))) { } else { return FAILED; } } return ZERO; } string toLowerCase(string n) { //precondition - none //postcondition - switches string n into lower case for (int i = ZERO; i < n.length(); i++) { if (isupper(n.at(i))) { n.at(i) = tolower(n.at(i)); } } return n; } // the following segment of code has been modified from // http://www.paked.net/subject_pages/computer_science/prog1.htm int binarySearch(string list[], int size, string target) { //precondition - dictionary array, size of dictionary array and target string //postcondition - returns integer based on whether or not string is found in array list[] int left, right, midpt; left = ZERO; right = size - 1; int key; while (left <= right) { midpt = (int) ((left + right) / 2); key = list[midpt].compare(target); if (key == ZERO) { return midpt; } else if (key < ZERO) { left = midpt + ONE; } else { right = midpt - ONE; } } return FAILED; } // end of code segment modified from // http://www.paked.net/subject_pages/computer_science/prog1.htm // the following segment of code has been modified from // http://www.fredosaurus.com/notes-cpp/examples/crosswordhelper/crosswordhelper.html void searchFull(string list[], string target, int size) { //precondition - dictionary array, size of dictionary array and target string //postcondition - sends each term to the match function and if it matches prints it for (int i = ZERO; i < size; i++) { if (match(target, list[i])) { cout << list[i] << endl; } } } bool match(string target, string findMatch) { //precondition - none //postcondition - looks for matches between strings with ? as a wildcard if (findMatch.size() != target.size()) { return false; } for (int i = ZERO; i < target.size(); i++) { if (target[i] != '?') { if (target[i] != findMatch[i]) { return false; } } } return true; } // end of code segment modified from // http://www.fredosaurus.com/notes-cpp/examples/crosswordhelper/crosswordhelper.html /* - - - - - - - */ /* - END OF FILE - */