C++编写一个实现文章词频统计功能的函数
要求:输入一系列英文单词,单词间用空格隔开。请统计输入过哪些单词,以及各个单词出现的次数,不区分大小写字母,然后输出各个单词及其出现的次数。该函数的原型为:voidsta...
要求:输入一系列英文单词,单词间用空格隔开。请统计输入过哪些单词,以及各个单词出现的次数,不区分大小写字母,然后输出各个单词及其出现的次数。该函数的原型为:void statistic(char string[]);其中参数string是文章字符串,该函数无返回值。编写主函数,对上述函数进行测试。
展开
1个回答
2013-06-28
展开全部
#include <iostream>
#include <map>
#include <sstream>
#include <iomanip>
using namespace std;
void statistic(char str[])
{
istringstream isstrm(str);
string s;
map<string, int> m;
while(isstrm >> s) {
for(string::size_type i = 0; i < s.size(); ++i)
s[i] = tolower(s[i]);
++m[s];
}
cout << setiosflags(ios::left) << setw(20) << "[word]" << "\t[frequency]\n\n";
for(map<string, int>::iterator i = m.begin(); i != m.end(); ++i)
cout << setiosflags(ios::left) << setw(20) << i->first << '\t' << i->second << endl;
}
int main()
{
char* p = "C++ is also used for hardware design \
where design is initially described in C++ \
then analyzed architecturally constrained \
and scheduled to create a register transfer \
level hardware description language via high-level synthesis";
statistic(p);
}
#include <map>
#include <sstream>
#include <iomanip>
using namespace std;
void statistic(char str[])
{
istringstream isstrm(str);
string s;
map<string, int> m;
while(isstrm >> s) {
for(string::size_type i = 0; i < s.size(); ++i)
s[i] = tolower(s[i]);
++m[s];
}
cout << setiosflags(ios::left) << setw(20) << "[word]" << "\t[frequency]\n\n";
for(map<string, int>::iterator i = m.begin(); i != m.end(); ++i)
cout << setiosflags(ios::left) << setw(20) << i->first << '\t' << i->second << endl;
}
int main()
{
char* p = "C++ is also used for hardware design \
where design is initially described in C++ \
then analyzed architecturally constrained \
and scheduled to create a register transfer \
level hardware description language via high-level synthesis";
statistic(p);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |