C语言统计文本字母出现次数按从大到小顺序排,急用高分
#include <stdio.h>
#include <string.h>
void countc(int count[])
{
int index = 0;
char ch;
for (i = 0; i < 25; i++)
{
count[i] = 0;
}
FILE *fp = fopen("D:\\Data.txt", "r");
while((ch = fgetc(fp))!=EOF)
{
index = ch - 65;
if(index >= 0 && index < 26 ) count[index]++;
else
{
index = ch - 32;
if(index >= 0 && index < 26 ) count0[index]++;
}
}
}
void Swap(int &a,int &b)
{
a=a^b;
b=a^b;
a=a^b;
}
void main()
{
int cnt [26] = {0};
FILE *fp = fopen("D:\\Result.txt", "w");
countc(cnt);
char ch[26];
for(int i=0;i<26;i++)
{
ch[i]=i+65;//把字母赋值进数组
}
for (int i = 0; i < 26; i++)
{
for(int j=25;j>i;J--)
{
if(cnt[j]<cnt[j-1]])
{
Swap(cnt[j],cnt[j-1]);
ch[j]=ch[j]^ch[j-1];
ch[j-1]=ch[j]^ch[j-1];
ch[j]=ch[j]^ch[j-1];
}
}
}
for(int i=0;i<26;i++)
{
cout<<ch[i]<<"字母个数为"<<cnt[i]<<endl;
}
getchar();
}