实验:输入一篇英文新闻,以“#”结束,统计其中a-z这26个字母各出现的次数和总字符个数。(不区分大小写)

  代码如下:

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. char ch;
  5. char s_letter[26]={'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'};
  6. char b_letter[26]={'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'};
  7. int num[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  8. for (int i = 1; i <= 10000; i++) {
  9. ch = cin.get();
  10. if(ch == '#')break;
  11. for(int j=0;j<=25;j++)
  12. {
  13. if(ch==s_letter[j] || ch==b_letter[j]) num[j]++;
  14. }
  15. }
  16. for(int i=0;i<=25;i++)
  17. {
  18. cout<<s_letter[i]<<"出现的个数为"<<num[i]<<endl;
  19. }
  20. }

(拓展)统计26个字母在一篇新闻中各自出现的频率。

代码如下:

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. char ch;
  5. char s_letter[26]={'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'};
  6. char b_letter[26]={'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'};
  7. int num[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  8. float f_num[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  9. int Sum;
  10. for (int i = 1; i <= 10000; i++) {
  11. ch = cin.get();
  12. if(ch == '#')break;
  13. for(int j=0;j<=25;j++)
  14. {
  15. if(ch==s_letter[j] || ch==b_letter[j]) num[j]++;
  16. }
  17. }
  18. for(int i=0;i<=25;i++)
  19. {
  20. cout<<s_letter[i]<<"出现的个数为"<<num[i]<<endl;
  21. }
  22. for(int i=0;i<=25;i++)
  23. {
  24. Sum+=num[i]; //计算字母总数
  25. }
  26. for(int i=0;i<=25;i++)
  27. {
  28. f_num[i]=num[i]*(1.0)/Sum * 100; //计算字母出现的频率*100
  29. }
  30. for(int i=0;i<=25;i++)
  31. {
  32. cout<<s_letter[i]<<"出现的频率为";
  33. printf("%.3f",f_num[i]);//保留三位小数
  34. cout<<"%"<<endl;//输出频率为百分之几
  35. }

}

实验:输入一篇英文新闻,以“#”结束,统计其中a-z这26个字母各出现的次数和总字符个数。(不区分大小写)

实验:输入一篇英文新闻,以“#”结束,统计其中a-z这26个字母各出现的次数和总字符个数。(不区分大小写)

实验:输入一篇英文新闻,以“#”结束,统计其中a-z这26个字母各出现的次数和总字符个数。(不区分大小写)

(拓展)英文新闻由文件输入,统计其中a-z这26个字母各出现的次数和总字符个数。(不区分大小写)

  1. #include <iostream>
  2. #include <fstream>
  3. #include <cassert>
  4. #include <string>
  5. #include<algorithm>
  6. #include<iomanip>
  7. using namespace std;
  8. int main()
  9. { char s_letter[26]={'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'};
  10. char b_letter[26]={'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'};
  11. int num[26]={0};
  12. float f_num[26]={0};
  13. ifstream infile;
  14. int Sum=0;
  15. infile.open("C:\\Users\\yyz22\\Desktop\\news1.txt");//打开文件txt 根据自身需求更改绝对路径 注意是双反斜杠\\
  16. char ch;
  17. infile >> noskipws;//不跳过空格和换行
  18. while (!infile.eof())//循环直到文本末尾结束
  19. {
  20. infile>>ch;
  21. cout<<ch;
  22. for(int j=0;j<=25;j++)
  23. {
  24. if(ch==s_letter[j] || ch==b_letter[j]) num[j]++;//统计字母出现个数
  25. }
  26. }
  27. infile.close();
  28. cout<<endl;
  29. for(int i=0;i<=25;i++)
  30. {
  31. cout<<s_letter[i]<<"出现的个数为"<<num[i]<<endl;
  32. Sum= Sum + num[i]; //计算字母总数
  33. }
  34. cout<<"总数:"<<Sum<<"\n";
  35. for(int i=0;i<=25;i++)
  36. {
  37. f_num[i]=float(num[i])/Sum; //计算字母出现的频率
  38. }
  39. for(int i=0;i<=25;i++)
  40. {
  41. cout<<s_letter[i]<<"出现的频率为";
  42. cout<<fixed<<setprecision(6)<<f_num[i]*100;
  43. cout<<"%"<<endl;//输出频率为百分之几
  44. }
  45. }

总结:for循环处理ch=cin.get(); 解决了enter结束输入流的问题。分别用if判断是什么字符或者#结束。

 --yyz 1806

2018.10.24

上一篇:原型模式--java代码实现


下一篇:CSS学习笔记08 浮动