int Count(int cou[]) //计算输入的字母的个数
{
char word;
char letter[27]={' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o', 'p','q','r','s','t','u','v','w','x','y','z'};
fstream SourceFile;
SourceFile.open("SourceFile.txt",ios::in);
if (!SourceFile)
{
cout<<"Can't open this file"<<endl;
}
while (!SourceFile.eof())

``````    {
SourceFile.get(word);
if (word>='A'&&word<='Z')
{
word=word+32;
}
int i=word-96;
cou[i]++;
}
SourceFile.close();
cout<<"letter"<<'\t'<<'\t'<<"频率为"<<endl;
for (int j=1;j<=26;j++)
{
if (cou[j]!=0)
{
cout<<letter[j]<<'\t'<<'\t'<<cou[j]<<endl;
}
}
cout<<"读入完事儿！"<<endl;

return 0;
``````

}

1个回答

`````` int Count(int cou[]) //计算输入的字母的个数
{
char word;
char letter[27]={' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o', 'p','q','r','s','t','u','v','w','x','y','z'}; //纯属多余
fstream SourceFile;
SourceFile.open("SourceFile.txt",ios::in); 从SourceFile.txt读取
if (!SourceFile)
{
cout<<"Can't open this file"<<endl;
}
while (!SourceFile.eof()) 循环直到文件结束
{
SourceFile.get(word);
if (word>='A'&&word<='Z') //如果是大写字母，统一转换为小写
{
word=word+32; //32是 'a' - 'A' ascii差
}
int i=word-96; // 96是'a'的ascii
cou[i]++; // cou保存你对应字母的统计数字，+1
}
SourceFile.close(); //关闭文件
cout<<"letter"<<'\t'<<'\t'<<"频率为"<<endl; //输出
for (int j=1;j<=26;j++) //依次输出a~z字符的出现频率
{
if (cou[j]!=0) //如果没有这个字母出现，就不输出
{
cout<<letter[j]<<'\t'<<'\t'<<cou[j]<<endl; //letter[j]可以用 (char)('A' + i) 代替
}
}
cout<<"读入完事儿！"<<endl;

return 0;
}
``````