新微赢技术网

标题: 求助!词频统计程序的修改 [打印本页]

作者: ■■■■■■    时间: 2009-11-3 23:35
标题: 求助!词频统计程序的修改
题目要求输入一段文字,统计每个单词的数量并输出,但是下面这段程序只能实现开头字母相同的单词的统计。
希望有人能把核心比较部分修改下,实现对单词的统计
#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




欢迎光临 新微赢技术网 (http://bbs.weiying.cn/) Powered by Discuz! X3.2