为什么我的程序跳过重复单词的索引

Why is my program skipping over the indices of repeated words

本文关键字:单词 索引 我的 程序 为什么      更新时间:2023-10-16

我的程序有问题。基本上,它找到了文本文件的单词计数,这是最少,重复的单词,并让用户找到一个单词。我的问题是,当我找到一个单词时,它会跳过重复单词的索引。例如,如果文本文件具有以下单词:

一两个一个三五

和用户搜索"三",它将输出索引为2。它跳过重复的单词。为什么是?

这是我的代码:

int main() {
    int counts[ARRAY_SIZE] = { 0 };
    string words[ARRAY_SIZE];
    ifstream inFile;
    string filename, searchWord, lowerCase;
    int wordCount = 0, totalWords = 0, index = 0;
    int result, mostIndex, leastIndex;
    cout << "Enter a text file: ";
    cin >> filename;
    inFile.open(filename);
    while (!inFile.is_open()) {
        if (filename == "q") {
            inFile.close();
            return 0;
        }
        else if (inFile.is_open()) {
            cout << filename << " is open";
        }
        else {
            cout << "Enter a valid file or type "q".";
        }
        cout << "The file you enetered is not valid, please enter a valid file or type "q" to quit.";
        cin >> filename;
        inFile.open(filename);
    }
    while (!inFile.eof()) {
        while (inFile >> lowerCase) {
            wordCount++;
            lowerCase = convertCase(lowerCase);
            result = search(words, lowerCase, totalWords);
            if (result == NOT_FOUND) {
                words[totalWords] = lowerCase; // lowerCase is a new word, so add it to the array,
                counts[totalWords] = 1;      //  and set it's count to 1.
                totalWords++;
            }
            else {
                counts[result]++; // The word was found, so increment the times we've seen it.
            }
        }
        cout << endl << "Total words: " << wordCount << endl;
        cout << "Search a word: ";
        cin >> searchWord;
        index = search(words, searchWord, totalWords);
        if (index == NOT_FOUND) {
            cout << """ << searchWord << """ << " was not found." << endl;
        }
        else {
            cout << endl << "the word " << """ << searchWord << """ << " is found on at index " << index << endl;
        }
        mostIndex = findIndexOfMost(counts, totalWords);
        leastIndex = findIndexOfLeast(counts, totalWords);
        cout << "The most repeated word is "" << words[mostIndex] << "" and was found " << counts[mostIndex] << " time(s)." << endl;
        cout << "The least repeated word is "" << words[leastIndex] << "" and was found " << counts[leastIndex] << " time(s)." << endl;
    }
    system("pause");
    return 0;
}

string convertCase(string word){
    for (int i = 0; i < word.length(); i++) {
        word[i] = tolower(word[i]);
    }
    return word;
}
int search(string words[], string searchWord, int totalWords){
    int index = NOT_FOUND;
    for (int i = 0; i < totalWords; i++){
        if (words[i] == searchWord){
            index = i;
            break;
        }
    }
    return index;
}
int findIndexOfMost(int counts[], int totalWords){
    int most; // assume most is first count.
    int index = 0, i;
    most = counts[index];
    for (i = 0; i < totalWords; i++)
    {
        if (counts[i] > most){
            most = counts[i];
            index = i;
        }
    }
    return index;
}
int findIndexOfLeast(int counts[], int totalWords) {
    int least, index = 0, i;
    least = counts[index];
    for (i = 0; i < totalWords; i++)
    {
        if (counts[i] < least) {
            least = counts[i];
            index = i;
        }
    }
    return index;
}

我同意您帖子上的评论,但是我确实很快发现了您的错误。我强烈建议您将来尝试调试器,以遵循您关心的变量,看看发生了什么以及它们的更新方式,当他们不像您想象的那样更新时。即使只是打印您的数组words也会向您显示问题。

尚未找到words数组中的单词,因此当您搜索words时,您正在搜索唯一的单词。

    if (result == NOT_FOUND) {
        words[totalWords] = lowerCase; // lowerCase is a new word, so add it to the array,
        counts[totalWords] = 1;      //  and set it's count to 1.
        totalWords++;
    }
    else {
        counts[result]++; // The word was found, so increment the times we've seen it.
    }

文件

one two one one three five

您的变量words将容纳

one two three five

因此,如果您在words中搜索'three',即使它是文件中的第五个单词,也将获得索引2,因此您需要索引4