2014-10-11 43 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