| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1182 人关注过本帖
标题:求助!词频统计程序的修改
只看楼主 加入收藏
夕月
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-3-15
收藏
 问题点数:0 回复次数:1 
求助!词频统计程序的修改
题目要求输入一段文字,统计每个单词的数量并输出,但是下面这段程序只能实现开头字母相同的单词的统计。
希望有人能把核心比较部分修改下,实现对单词的统计
#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
Words begin with o: 3
Words begin with d: 2
Words begin with f: 2
Words begin with g: 2
Words begin with m: 2
Words begin with s: 2
Words begin with e: 1
Words begin with u: 1
Words begin with y: 1
Total words: 37

而我要的结果是:
the:n个
topic:m个
。。。。。
搜索更多相关主题的帖子: 词频 统计程序 
2008-03-22 14:41
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
stl还是自己弄懂了在用啊....
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main()
{
    char str[500];
    char *strToken;
    char strDelimit[] = " ,.?!";
    int wordCount = 0;
    map<string, int> words;
    map<string, int>::iterator iter;
    cout << "Please input a passage:" << endl;
    cin.getline( str, sizeof(str) );

    strToken = strtok( str, strDelimit );
    while ( strToken != NULL )
    {
        iter = words.find(strToken );
        if ( iter == words.end() )
        {
                words.insert( pair<string, int>(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;
}

学习需要安静。。海盗要重新来过。。
2008-03-22 17:27
快速回复:求助!词频统计程序的修改
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.396169 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved