首页 > 其他 > 详细

第三次作业 词频统计

时间:2016-03-17 00:09:38      阅读:158      评论:0      收藏:0      [点我收藏+]

#include <iostream>
#include <string>
using namespace std;
struct Word
{ string Str;
int Count;
};
void CalcCount(Word *words,string &newWord,int size)
{ int m=0;
for(;m<size;m++)
{ if(words[m].Str==newWord)

{ words[m].Count++;
return;
}
else if(words[m].Str=="")
break;
}
words[m].Str=newWord;
words[m].Count =1;
}
int main()
{
Word *words;
string content;
cout<<"输入一串英文:";
getline(cin, content);
int wCount = 1;
for(unsigned int m=0; m<content.length();m++)
{
if(content[m]==‘ ‘)
wCount++;
}
words=new Word[wCount];
string::size_type offset=content.find(‘ ‘);
while(offset!=string::npos)
{
string wStr=content.substr(0,offset);
if (wStr.length()<4)
{
wCount--;
content.erase(0,offset+1);
offset=content.find(‘ ‘);
continue;
}
content.erase(0,offset+1);
CalcCount(words,wStr,wCount);
offset=content.find(‘ ‘);
}
if (content.length()>= 4)
{
CalcCount(words, content, wCount);
}
else wCount--;

for (int n=0; n<wCount; n++)
{
if (words[n].Str=="")
{
wCount--;
}
}
for(m=0;m<wCount-1;m++)
{
cout << words[m].Str << "频率:" << words[m].Count << "次" << endl;
}

delete [] words;
return 0;
}

 技术分享

第三次作业 词频统计

原文:http://www.cnblogs.com/zjcx8775/p/5285731.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!