2017-02-20 65 views
1

说我有问题计数词汇出现的是,我不完全知道如何重置我的字数。我创建了一个单词搜索,但是当我计算出10个不同单词出现的次数时,它与第一个单词保持相同的数字。我相信我遇到的问题是我用一个for循环问题与多个单词

输出

boy appeared 3 times 
Snape appeared 3 times 
Dumbledore appeared 3 times 
he appeared 3 times 
her appeared 3 times 
the appeared 3 times 
it appeared 3 times 
is appeared 3 times 
will appeared 3 times 
all appeared 3 times 

它应该是什么样子

boy appeared 3 times 
Snape appeared 7 times 
Dumbledore appeared 4 times 
he appeared 27 times 
her appeared 4 times 
the appeared 13 times 
it appeared 6 times 
is appeared 12 times 
will appeared 2 times 
all appeared 3 times 

通过阅读我的代码我敢肯定,我已经比现在更复杂。我将不胜感激我提出的任何建议和更正。

的完整代码提前

#include <iostream> 
#include <fstream> 
#include <string> 
#include <sstream> 
#include <vector> 

// Main Function 
int main() 
{ 
    // Declaration 
    std::string list, passage, word[10]; 
    std::ifstream listFile("WordList.txt", std::ios::in); 
    std::ifstream passageFile("HarryPotterPassage.txt", std::ios::in); 
    std::vector<std::string> vec_wordList, vec_passage; 


    /* Read a file that contains a list of 10 words */ 
    if (listFile.is_open()) 
    { 
     // Store text file in a vector 
     while (listFile) 
     { 
      listFile >> list; 
      vec_wordList.push_back(list); 
     } 

     // Assign vector to individual strings 
     for (int i = 0; i < 10; i++) 
     { 
      word[i] = vec_wordList[i]; 
     } 

     // Close file 
     listFile.close(); 
    } 
    else 
     std::cout << "No file found.\n"; 


    /* Read another file containing a paragraph */ 
    if (passageFile.is_open()) 
    { 
     while (passageFile) 
     { 
      // Store text file in a string 
      std::getline(passageFile, passage); 
     } 

     // Close file 
     passageFile.close(); 
    } 
    else 
     std::cout << "No file found.\n"; 

    //std::cout << passage << '\n'; 


    /* Count the number of words from the first file 
     from the second file that contains the paragraph */ 
    size_t count = 0; 
    std::string::size_type pos = 0; 

    for (int i = 0; i < 10; i++) 
    { 
     while ((pos = passage.find(word[i], pos)) != std::string::npos) 
     { 
      count++; 
      pos += word[i].size(); 
     } 

     std::cout << word[i] << " appeared " << count << " many times\n"; 
    } 

    system("pause"); 
    return 0; 
} 

感谢。

+0

将可能更容易使用'的std :: unoredered_map <的std :: string,int>的'来解决。 –

+0

我认为你需要设置'在'for'循环的每次迭代的开始数= 0'和'POS = 0'。基本上,你可以将这两个声明**移动到**循环中。 –

回答

0

您需要在外部循环的每次迭代的开始重置countpos

换句话说,改变这种:

size_t count = 0; 
std::string::size_type pos = 0; 
for (int i = 0; i < 10; i++) 
{ 
    ... 
} 

要这样:

for (int i = 0; i < 10; i++) 
{ 
    size_t count = 0; 
    std::string::size_type pos = 0; 
    ... 
} 

顺便说一句,我也想改变这种状况10sizeof(word)/sizeof(*word)

+0

如果你对C++ 11或更高,那么我甚至用'为(常量自动&W:字)',并更换所有的'字[I]'和'w'内循环。 –

+0

哇!我知道这很简单。试图弄清楚为什么它不会重置,一直让我头痛24小时。非常感谢。 – gomicoo

+0

@gomicoo:没问题。顺便说一句,我甚至懒得测试我的解决方案。很明显,我认为你需要一个不同的'count'每个'字[I]',并从那里我的结论是,你还需要一个不同的'pos'每个'字[I]'。请注意上面评论中建议的附加修正。 –

1

您使用Word来代替字[9] [I],所以你得到的最后一个字,而不是每个人的结果。 尝试:

for (int i = 0; i < 10; i++) 
{ 
    while ((pos = passage.find(word[i], pos)) != std::string::npos) 
    { 
     count++; 
     pos += word[i].size(); 
    } 

    std::cout << word[i] << " appeared " << count << " many times\n"; 
} 
+0

补给它,我忘了改变它。这就是我最初使用word [i]的方式。我正在改变数字,所以我知道它实际上是什么结果。将它改为word [i]也不适用于我。 – gomicoo