词频统计,感觉好难

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

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

   

#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;
 }

 

 

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

转载于:https://www.cnblogs.com/WC123/p/5303556.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值