统计文件中的字符出现频率,并按照频率出现的高低排序<目前还只能处理ansi字符,有测试的朋友请用纯ANSI文件测试><那个T版呢,解释一下那个
程序代码:
#include <stdio.h> int main() { struct Count { int c; int count; }; struct Count counter[256]; int i=0; for(i=0;i<256;i++) {counter[i].c=i;counter[i].count=0;} FILE* fp; fp=fopen("d:\\b.txt","r"); char flag=0xA; int lines=0; char tline[256]={'\0'}; char* cp=tline; char tmp; while(!feof(fp)) { tmp=getc(fp); counter[tmp].count++; } //sort int m,n,tmp2,tmp3; for(m=0;m<256;m++) { for(n=m+1;n<256;n++) { if(counter[m].count>=counter[n].count) { tmp2=counter[m].count; counter[m].count=counter[n].count; counter[n].count=tmp2; tmp3=counter[m].c; counter[m].c=counter[n].c; counter[n].c=tmp3; } } } //sort end int _chars=0; for(i=0;i<256;i++) { if(counter[i].count>0) { _chars+=counter[i].count; if(counter[i].c==0xA) //\n { printf("\\n Number of occurrences=%d\n",counter[i].count); } else if(counter[i].c==0x20) //space { printf("space Number of occurrences=%d\n",counter[i].count); } else if(counter[i].c==0x9) //\t { printf("\\t Number of occurrences=%d\n",counter[i].count); } else if(counter[i].c==0x0) //NULL { printf("NULL Number of occurrences=%d\n",counter[i].count); } else if(counter[i].c==0xD) // { printf("OxD Number of occurrences=%d\n",counter[i].count); } else /*Visible characters*/printf("%c Number of occurrences=%d\n",counter[i].c,counter[i].count); } } printf("File the total number of characters=%d\n",_chars); fclose(fp); return 0; } /* w Number of occurrences=1 V Number of occurrences=1 O Number of occurrences=1 I Number of occurrences=1 E Number of occurrences=1 : Number of occurrences=1 9 Number of occurrences=1 1 Number of occurrences=1 # Number of occurrences=1 ! Number of occurrences=1 g Number of occurrences=2 U Number of occurrences=2 F Number of occurrences=2 D Number of occurrences=2 C Number of occurrences=2 A Number of occurrences=2 ' Number of occurrences=2 _ Number of occurrences=3 > Number of occurrences=3 3 Number of occurrences=3 * Number of occurrences=4 L Number of occurrences=5 < Number of occurrences=5 6 Number of occurrences=6 5 Number of occurrences=6 x Number of occurrences=8 N Number of occurrences=8 % Number of occurrences=8 b Number of occurrences=9 2 Number of occurrences=10 d Number of occurrences=11 h Number of occurrences=12 , Number of occurrences=12 + Number of occurrences=12 } Number of occurrences=15 { Number of occurrences=15 l Number of occurrences=15 a Number of occurrences=16 \ Number of occurrences=16 / Number of occurrences=16 0 Number of occurrences=18 " Number of occurrences=18 ) Number of occurrences=24 ( Number of occurrences=24 p Number of occurrences=25 s Number of occurrences=26 m Number of occurrences=27 ] Number of occurrences=29 [ Number of occurrences=29 . Number of occurrences=29 f Number of occurrences=35 = Number of occurrences=39 ; Number of occurrences=42 \t Number of occurrences=45 i Number of occurrences=55 u Number of occurrences=64 o Number of occurrences=71 e Number of occurrences=75 r Number of occurrences=76 \n Number of occurrences=78 t Number of occurrences=90 n Number of occurrences=94 c Number of occurrences=95 space Number of occurrences=744 File the total number of characters=2094 */
[ 本帖最后由 wp231957 于 2013-1-4 22:38 编辑 ]