为什么我的程序跳过重复单词的索引
Why is my program skipping over the indices of repeated words
我的程序有问题。基本上,它找到了文本文件的单词计数,这是最少,重复的单词,并让用户找到一个单词。我的问题是,当我找到一个单词时,它会跳过重复单词的索引。例如,如果文本文件具有以下单词:
一两个一个三五
和用户搜索"三",它将输出索引为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
。
相关文章:
- 文本文件中的单词链表
- 数组索引的值没有增加
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 在指针的帮助下,文本文件中单词的频率
- 查找最接近的大于当前数字的数字的索引
- 在C++中调整向量中的索引
- 重载元组索引运算符-C++
- 如何根据单词在文本中出现的概率输出单词
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 为std::string的某个索引赋值
- 并行用于C++17中数组索引范围内的循环
- 从字符串变量中逐字符读取单词
- 使用std::mt19937从字符串中返回一个随机单词
- 如何用for循环在c++中生成单词三角形
- 将数字转换为单词
- 使用if-else将数字转换为单词
- 为什么我的程序跳过重复单词的索引
- 如何插入一个单词并将字母放在2D数组中的不同索引中
- 为什么在将单词从文本文件C++到行进行多重映射时获得额外的索引值
- 书籍索引,如何查看每个单词并根据其第一个字母进行存储