- /**
- * 對一段英文的詞頻統計
- * @Author [email protected]
- * date 2010/06/17
- */
- #include <iostream>
- #include <string>
- using namespace std;
- /**
- * 單詞對象
- */
- struct Word
- {
- Word() : Str(""), Count(0)
- {}
- string Str;
- int Count;
- /**
- * 交換單詞(用於排序)
- * @param word 交換的單詞對象
- */
- void exchange(Word &word)
- {
- string tStr = word.Str;
- int tCount = word.Count;
- word.Str = Str;
- word.Count = Count;
- Str = tStr;
- Count = tCount;
- }
- };
- /**
- * 統計詞頻
- * @param words 單詞數組
- * @param newWord 單詞內容
- * @param size 單詞總數
- */
- 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;
- }
- /**
- * 以單詞出現頻率降序排列單詞
- * @param words 單詞數組
- * @param size 單詞數量
- */
- 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 << "出現頻率最高的前" << printCount << "個單詞是:" << endl;
- for(int i = 0; i < printCount; i++)
- {
- cout << words[i].Str << "/t頻率:" << words[i].Count << "次" << endl;
- }
- delete [] words;
- return 0;
- }
效果: