首页 > 技术文章 > 词频统计,感觉好难

WC123 2016-03-21 20:37 原文

   忙了乱七八糟的事,连第三次软件的作业都忘记写了,在此补上,并对此感到很抱歉。看了题目要求,感觉看不懂,上网查了资料,看了一下大神们的编程,才有一点点感觉。但是面对茫茫一片代码依旧很是迷茫。

   看一下代码就知道很难,借鉴了网上大神的基础思想,还是不太懂。

   

#include <iostream>  
#include <string>  
using namespace std;  
 
 Struct Word 
 {  string Str;  
    int Count;  

     void exchange(Word &word)  
    {   string tStr = word.Str;  
        int tCount = word.Count;  
        word.Str = Str;  
        word.Count = Count;  
        Str = tStr;  
        Count = tCount; }  
 };  
 void CalcCount(Word * words, string &newWord, int size)  
 {  
     int i = 0;  
     for(; i < size; i++)  
     {  
        if(words[i].Str == newWord)  
         {  words[i].Count++;  
             return; }  
         else if(words[i].Str == "")  
             break;  
     }  
    words[i].Str = newWord;  
     words[i].Count = 1;  
 }  
 void SortWordDown(Word * words, int size)  
 {  
     for(int i = 0; i < size; i++)  
     {  
         for(int j = 0; j < size-1; j++)  
         {  
             if(words[j].Count <  words[j+1].Count)  
             {  words[j].exchange(words[j+1]); } } } }  
 int main()  
 {  
     Word * words;  
     string content;  
     cout << "";  
     getline(cin, content);  
   
      
     int wCount = 1; 
     

     for(unsigned int i = 0; i < content.length(); i++)  
     {  
         if(content[i] == ' ')  
             wCount++;  
     }  
     words = new Word[wCount];  
     string::size_type offset = content.find(' '); 
     while(offset != string::npos)  
     {  
         string wStr = content.substr(0, offset);  
         content.erase(0, offset+1);  
         CalcCount(words, wStr, wCount);  
         offset = content.find(' ');  
     }  
     CalcCount(words, content, wCount);
     SortWordDown(words, wCount);  
     int printCount = wCount < 5 ? wCount : 5;  
     cout << "单词分别出现的频率为:" << endl;  
     for(i = 0; i < printCount; i++)  
     {  
         cout << words[i].Str << "出现频率为:" << words[i].Count << "" << endl;  
         
     }  
return 0;
 }

 

 

   可以说自己的编程能力很着急,但也没办法,只能慢慢补充,慢慢锻炼编程思想和能力、、、 

推荐阅读