Бинарный поиск по словарю - PullRequest
0 голосов
/ 02 марта 2020

Как вы используете бинарный поиск для поиска слова в файле словаря? В моем файле каждое слово в алфавитном порядке, и в каждой строке словаря есть только одно слово.

Код:

            cout << "Enter a word to be looked up in dictionary: ";

            fgets(searchWord, 81, stdin); // Stores input in character array with '\n' at the end

            nullAdder(searchWord); // Replaces the last char with null for searchWord

            nullCheck(searchWord); // Sanity check

            inStream.seekg(0,ios::beg);
            low = inStream.tellg();

            inStream.seekg(0,ios::end);
            high = inStream.tellg();

            while (low <= high){

                mid = (low + high) / 2;

                inStream.seekg(mid);

                inStream >> fileWord;

                if ((strcmp(searchWord, fileWord) == 0) && (strlen(fileWord) >= 3)){
                    cout << searchWord << " IS in the dictionary." << endl;
                    isWordInDic = true;
                    break;
                }

                else if (strcmp(searchWord, fileWord) < 0) {
                    high = mid - 1;
                }

                else if (strcmp(searchWord, fileWord) > 0) {
                    low = mid + 1;
                }

                else {
                    isWordInDic = false;
                }
            }

            // If inputted word is not in file, prints so
            if (!isWordInDic){
                cout << searchWord << " is NOT in the dictionary." << endl;
            }
...