C++ 对一段英文进行词频统计

[cpp]  view plain copy
  1. /** 
  2.  * 对一段英文的词频统计 
  3.  * @Author xxingup@gmail.com 
  4.  * date 2010/06/17 
  5.  */  
  6. #include <iostream>  
  7. #include <string>  
  8.   
  9. using namespace std;  
  10. /** 
  11.  * 单词对象 
  12.  */  
  13. struct Word  
  14. {  
  15.     Word() : Str(""), Count(0)  
  16.     {}  
  17.     string Str;  
  18.     int Count;  
  19.   
  20.     /** 
  21.      * 交换单词(用于排序) 
  22.      * @param word 交换的单词对象 
  23.      */  
  24.     void exchange(Word &word)  
  25.     {  
  26.         string tStr = word.Str;  
  27.         int tCount = word.Count;  
  28.         word.Str = Str;  
  29.         word.Count = Count;  
  30.         Str = tStr;  
  31.         Count = tCount;  
  32.     }  
  33. };  
  34.   
  35. /** 
  36.  * 统计词频 
  37.  * @param words 单词数组 
  38.  * @param newWord 单词内容 
  39.  * @param size 单词总数 
  40.  */  
  41. void CalcCount(Word * words, string &newWord, int size)  
  42. {  
  43.     int i = 0;  
  44.     for(; i < size; i++)  
  45.     {  
  46.         if(words[i].Str == newWord)  
  47.         {  
  48.             words[i].Count++;  
  49.             return;  
  50.         }  
  51.         else if(words[i].Str == "")  
  52.             break;  
  53.     }  
  54.     words[i].Str = newWord;  
  55.     words[i].Count = 1;  
  56. }  
  57.   
  58. /** 
  59.  * 以单词出现频率降序排列单词 
  60.  * @param words 单词数组 
  61.  * @param size 单词数量 
  62.  */  
  63. void SortWordDown(Word * words, int size)  
  64. {  
  65.     for(int i = 0; i < size; i++)  
  66.     {  
  67.         for(int j = 0; j < size-1; j++)  
  68.         {  
  69.             if(words[j].Count <  words[j+1].Count)  
  70.             {  
  71.                 words[j].exchange(words[j+1]);  
  72.             }  
  73.         }  
  74.     }  
  75. }  
  76.   
  77. int main()  
  78. {  
  79.     Word * words;  
  80.     string content;  
  81.     cout << "输入一段英文:";  
  82.     getline(cin, content);  
  83.   
  84.     //计算单词总数  
  85.     int wCount = 1;  
  86.     for(unsigned int i = 0; i < content.length(); i++)  
  87.     {  
  88.         if(content[i] == ' ')  
  89.             wCount++;  
  90.     }  
  91.     words = new Word[wCount];  
  92.   
  93.     string::size_type offset = content.find(' ');//单词都是以空格隔开  
  94.     while(offset != string::npos)  
  95.     {  
  96.         string wStr = content.substr(0, offset);  
  97.         content.erase(0, offset+1);  
  98.         CalcCount(words, wStr, wCount);  
  99.         offset = content.find(' ');  
  100.     }  
  101.     CalcCount(words, content, wCount);//计算最后一个单词  
  102.   
  103.     SortWordDown(words, wCount);  
  104.     int printCount = wCount < 5 ? wCount : 5;  
  105.     cout << "出现频率最高的前" << printCount << "个单词是:" << endl;  
  106.     for(int i = 0; i < printCount; i++)  
  107.     {  
  108.         cout << words[i].Str << "/t频率:" << words[i].Count << "次" << endl;  
  109.     }  
  110.   
  111.     delete [] words;  
  112.     return 0;  
  113. }  

 

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值