2014-10-11 49 views
-2

我正在工作的代码将从导入的文件中找到重复单词的数量。 输入流表示包含一系列行的文件。函数 将检查每行,在同一行上查找同一标记的连续出现次数,并沿着它连续出现的次数打印每个重复的标记。不重复的 令牌不会被打印。打印字符串的副本

以下是我有:

#include <iostream> 
#include <fstream> 
#include <cstdlib> 
#include <string> 

using namespace std; 

int main() 
{ 

    ifstream in("file.txt"); 

    if (! in) 
    { 
     cerr << "Could not open file.txt."; 
     return EXIT_FAILURE; 
    } 

    string str; 

    int count = 0; 
    int len=str.length(); 

    while(getline(in,str)){ 

     for(int i = 0; i < len; i++){ 
      if(str.at(i) == str.at(i+1)){ 
       count++; 
      } 
      else if(str.at(i) != str.at(i+1)){ 
       i++; 
      } 
     } 
     cout << str << "*" << count << endl; 
    } 
} 

中的.txt包含:

hello how how are you you you you 
I I I am Jack's Jack's smirking smirking smirking smirking smirking revenge 
bow wow wow yippee yippee yo yippee yippee yay yay yay 
one fish two fish red fish blue fish 
It's the Muppet Show, wakka wakka wakka 

输出应该是:

how*2 you*4 
I*3 Jack's*2 smirking*5 
wow*2 yippee*2 yippee*2 yay*3 

wakka*3 
+0

做这些语句做:'字符串str; int len = str.length();'?你声明一个空字符串,然后取其长度。这是必要的吗?由于空字符串的长度为零,所以使用'int len = 0;'有一个较短的方法。 – 2014-10-11 23:05:36

+0

尝试查看[逐行读取文件](http://stackoverflow.com/q/7868936)和[如何在C++中拆分字符串?](http://stackoverflow.com/q/236129)。您应该能够从两个Stack Overflow问题开发解决方案。 – jww 2014-10-12 04:37:35

回答

-1
#include <iostream> 
#include <string> 
#include <fstream> 
#include <cstdlib> 

using namespace std; 

int main() 
{ 
    ifstream in("file.txt"); 

    if(!in){ 
     cerr << "Could not open file.txt."; 
     return EXIT_FAILURE; 
    } 

    string str; 
    string str2; 
    string n; 
    string tab[100]; 
    string tab3[100]; 
    unsigned int tab2[100]; 
    unsigned int tab4[100]; 
    unsigned int i = 0; 
    unsigned int k = 0; 
    unsigned int l = 0; 
    unsigned int tablenght; 
    unsigned int tablenght2; 

    k = 0; 
    //it reads every line of text in file str2 
    while(getline(in,str2)){ 
     //it add every line of text str2 to str so you get whole file text 
     str += str2; 
     str += ' '; 
     //you need to add a character to mark where was the new line 
     str += "0 "; 
    } 

    for(i = 0; i < str.length(); i++){ 
      /*you check every single character in string str if that char is not 
      space than it writes it to string table tab, if that char is space than it 
      adds one to your index so it will write down the next word in next 
      index of table tab*/ 
      if(str[i] != ' '){ 
       tab[k] += str[i]; 
      }else{ 
       k++; 
       //that is for two spaces 
       if(str[i+1] == ' '){ 
        k--; 
       } 
      } 
    } 
    //k+1 is actually how many words and indexes you wrote to table tab 
    tablenght = k+1; 

    l = 0; 
    k = 0; 
    for(i = 0; i < tablenght; i++){ 
     //you need to reset the number of repeats k to zero if you go to another line 
     if(tab[i] == "0"){ 
      k = 0; 
     } 
     //there you get the number k how many times does some word repeats itself 
     if(tab[i] == tab[i+1]){ 
      k++; 
     //you need to reset k if tab current is not equal to tab next 
     }else{ 
      k = 0; 
     } 
     //there you store k values into integer table tab2 
     tab2[l] = k+1; 
     l++; 
    } 

    l = 0; 
    /*there you need to check if current string of table tab is equal to next string 
    in table tab and if it is you need to set next string to tab3[l] if you dont do 
    that you get something like that you*4 you*4 you*4 you*4 instead of only you*4*/ 
    for(i = 0; i < tablenght-1; i++){ 
     if(tab[i] == tab[i+1]){ 
      tab3[l] = tab[i+1]; 
      tab4[l] = tab2[i]; 
     }else{ 
      l++; 
     } 
     if(tab[i+1] == "0"){ 
      tab3[l] = tab[i+1]; 
     } 
     k++; 
    } 
    tablenght2 = l; 
    //there you cout both tables 
    for(i = 0; i < tablenght2; i++){ 
     /*you need to check if number is bigger than 1 because it need to cout only 
     the words that repeats itself for more than one time than you need to check 
     that table tab3 doesnt contain string with zero that we previously added 
     that we could check later if it needs to go in another line and we 
     need to check that tab3 index is not empty*/ 
     if(tab4[i] > 1 && tab3[i] != "0" && !tab3[i].empty()){ 
      cout << tab3[i] << "*" << tab4[i] << " "; 
     } 
     /*thats the zero that we wrote to table in the begining and that we can now 
     write a new line*/ 
     if(tab3[i] == "0"){ 
      cout << endl; 
     } 
    } 

    return 0; 
} 
+0

试试吧! – klemsi123 2014-10-12 06:53:14

+1

你能解释一下它为什么可行吗?在没有解释的情况下,简单的代码转储就会在StackOverflow中被忽略,除非它通过阅读代码是非常明显的。在这种情况下......它并不那么明显。 – rayryeng 2014-10-12 07:01:53

+0

@rayryeng我添加了评论 – klemsi123 2014-10-12 08:39:27