注册 登录
编程论坛 C++教室

[求助]c++统计英文字母的使用频率的程序

huang254 发布于 2007-06-26 17:45, 1790 次点击
1. 课题功能描述
本程序的功能,就是要统计英文字母的使用频率。
2. 问题详细描述
为统计英文字母的使用频率,输入一个不包括空格的由英文字母组成的字符串,长度不超过200个字符。统计26个英文字母的使用频率,不区分大小写。最后按使用频率从大到小输出字母(小写字母)和使用频率(出现的次数)。
5 回复
#2
huang2542007-06-26 17:46

大哥大姐们帮帮忙阿 谢谢 小弟不胜感激

#3
aipb20072007-06-26 17:48
简单,自己先想想!

提示:处理字符串
用map容器
………………
#4
aipb20072007-06-26 17:48
吃饭去罗!
#5
aipb20072007-06-26 19:52
[CODE]#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
//select the letters of input the store them in a string
char word;
string str;
while ((word = cin.get()) != '\n'){
if (isalpha(word)){
word = tolower(word);
str += word;
}
}
//insertion sort for the string
for (int i = 1;i < str.size();++i){
if (str[i] < str[i-1]){
char temp = str[i];
int j = i;
do{
str[j] = str[j-1];
--j;
}
while (j > 0 && temp < str[j-1]);
str[j] = temp;
}
}
if (str.empty()){
cerr << "no letter in your input" << endl;
system("pause");
return EXIT_FAILURE;
}
cout << str << endl;
//count the letter times
int pos = 0;
while (true){
char key = str[pos];
int mark = str.find_first_not_of(key,pos);
if (mark == string::npos){
cout << key << "\t\t" << str.size() - pos << endl;
break;
}
int length = mark - pos;
cout << key << "\t\t" << length << endl;
pos = mark;
}
system("PAUSE");
return EXIT_SUCCESS;
}

[/CODE]

不知道符合要求不
N久前写的,可以接受任何形式的输入,回车键结束。
#6
野比2007-06-26 20:06
aipb兄.. 太包办了吧... 提示一下就好了...
1