1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > C++ 对给定一个英文文本文件 统计并输出26个英文字母(不区分大小写)的出现频率

C++ 对给定一个英文文本文件 统计并输出26个英文字母(不区分大小写)的出现频率

时间:2024-02-08 01:47:58

相关推荐

C++ 对给定一个英文文本文件 统计并输出26个英文字母(不区分大小写)的出现频率

软件过程管理作业。。。

代码如下

#include<iostream>#include<fstream>using namespace std;int main(void){ifstream s("D:\\test5.txt");char x;int a[26] = {0},b[26];int count1,count2,count3,countthe = 0,countspace = 0; //count1统计字母个数 count2/3作为关键字母定位标记 countthe记录the的个数 countspace 统计空格个数while(s.get(x)){count1 ++;if( x >= 65 && x <= 90 )a[x - 65] ++;if( x >= 97 && x <= 122 )a[x - 97] ++;if(x == 84 || x == 116)count2 = count1;if(x == 72 || x ==104)count3 = count1;if((x == 69 || x == 101) && (count1 - count3) == 1 && (count1- count2) == 2)//通过位置差来判断t、h、e的相对位置countthe ++;if(x == ' ' || x == 10)countspace ++;}for(int i = 0;i < 26; i ++ ){printf("%c出现的频率是:%.2f\n",i+65,(a[i])*1.0 / count1);}printf("the的出现的次数及频率分别是:%d, %.2f",countthe,(countthe * 1.0) / (countspace + 1));return 0;}

一个是c++的文件流,然后是用数组存储相应的字符个数,最后是the的求取算法。评论区多多指教

C++ 对给定一个英文文本文件 统计并输出26个英文字母(不区分大小写)的出现频率 并统计定冠词‘the’的出现次数及频率。 ---易理解版本

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。