设为首页收藏本站

新微赢技术网

 找回密码
 注册
搜索
热搜: 回贴
查看: 1476|回复: 0
打印 上一主题 下一主题

求助!词频统计程序的修改

[复制链接]
跳转到指定楼层
1#
发表于 2009-11-3 23:35:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
题目要求输入一段文字,统计每个单词的数量并输出,但是下面这段程序只能实现开头字母相同的单词的统计。
希望有人能把核心比较部分修改下,实现对单词的统计
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main()
{
char str[500];
char *strToken;
char strDelimit[] = " ,.?!";
int wordCount = 0;
map<char, int> words;
map<char, int>::iterator iter;
cout << "Please input a passage:" << endl;
cin.getline( str, sizeof(str) );

strToken = strtok( str, strDelimit );
while ( strToken != NULL )
{
iter = words.find( tolower( *strToken ) );
if ( iter == words.end() ) {
words.insert( pair<char, int>( tolower( *strToken ), 1 ) );
}
else {
iter->second++;
}
++wordCount;
strToken = strtok( NULL, strDelimit );
}

for ( iter=words.begin(); iter!=words.end(); ++iter ) {
cout << "Words begin with " << iter->first << ": " << iter->second << endl;
}
cout << "Total words: " << wordCount << endl;
这样的运行结果是:
Please input a passage:
The topic of this assignment is about array, pointer and string. In particular, the goal of the assignment is to give you experience for dividing programs into modules and using the pointer for manipulation of string data.
Words begin with t: 7
Words begin with a: 6
Words begin with i: 4
Words begin with p: 4
您需要登录后才可以回帖 登录 | 注册

本版积分规则

申请友链|小黑屋|最新主题|手机版|新微赢技术网 ( 苏ICP备08020429号 )  

GMT+8, 2024-11-18 12:49 , Processed in 0.097759 second(s), 9 queries , Gzip On, Memcache On.

Powered by xuexi

© 2001-2013 HaiAn.Com.Cn Inc. 寰耽

快速回复 返回顶部 返回列表