#include <stdio.h>
#include <string.h>
int main(void)
{
char string[100];
int c = 0, count[26] = {0};
int accum = 0;
int a;
while(1)
{
a = scanf("%s", string);
while ( string[c] != '\0' )
{
if ( string[c] >= 'a' && string[c] <= 'z' ){
count[string[c]-'a']++;
accum++;
}
else if (string[c] >= 'A' && string[c] <= 'Z'){
count[string[c]-'A']++;
accum++;
}
c++;
}
if (a == EOF)
{
for ( c = 0 ; c < 26 ; c++ )
{
if( count[c] != 0 )
printf( "%c %f\n", c+'a', ((double)count[c])/accum);
}
}
}
return 0;
}
所以我有一个程序可以计算在 EOF 之前出现在标准输入中的字母的频率。但是一旦我到达 EOF,我的程序就会进入一个无限循环,并且频率似乎不正确。当我只是将打印语句输入单个字符串时,它工作正常。我真的不知道问题是什么。有人能帮我解决这个问题吗?