统计txt文本中各个字符出现的频率和次数

    技术2022-05-19  32

    #include<stdio.h>#include<math.h>

    int main(){  char file[10000]; int statistic[127]; int i=0,j; FILE *fp; if((fp=fopen("My Miraculous Family.txt","r"))==NULL) {  printf("can't open My Miraculous Family.txt");  return 1; } while(!feof(fp))  fread(&file[i++],1,1,fp); fclose(fp); j=i; printf("**********统计My Miraculous Family.txt一文中各个字符出现的次数和频率***********/n/n"); printf("该文件总共有字符%d个!/n/n",j); //有结束符 for (i=0;i<127;i++) {  statistic[i]=0; }

        i=0; while(file[i]!=0) {    statistic[file[i]]++;    i++; }    printf("制表符的个数为=         频率为:%.2f%/n",statistic[9],statistic[9]/(double)j); printf("换行符的个数为=         频率为:%.2f%/n",statistic[10],statistic[10]/(double)j); printf("空格的个数为  =        频率为:%.2f%/n",statistic[32],statistic[32]/(double)j);

     for(i=33;i<127;i++)  printf("%c的个数为=           频率为:%.2f%/n",i,statistic[i],statistic[i]/(double)j);

     printf("其余字符出现的个数均为0!/n");      return 0;}

     


    最新回复(0)