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

时间:2017-06-08 06:22:28

标签: c++ visual-c++

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

  一个二一一三五

并且用户搜索“3”,它将输出索引为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;
}

1 个答案:

答案 0 :(得分:2)

我同意您对帖子的评论,但我确实很快发现了您的错误。我强烈建议您将来尝试使用调试器来跟踪您关注的变量,看看发生了什么以及它们如何更新,当它们没有像您认为的那样更新时。即使只是打印数组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

因此,如果您在'three'中搜索了words,那么您将获得索引2,即使它是您文件中的第五个字,因此您需要索引{ {1}}。