#include<stdio.h>
#include<string.h>
struct word
{
char c[30];
int n;
}w[10000];
void main()
{
FILE *fp;
char b[30],ch;
int i=0,m=1,j=0,k=0,t=0,flag=0;
fp=fopen("test.txt","r");
while((ch=fgetc(fp))!=EOF)
{
if('A'<=ch&&ch<='Z') ch=ch+32;
if('a'<=ch && ch<='z')
{b[i]=ch;i++;flag=1;}
else
{
if(ch=='-'&&(ch=fgetc(fp))=='\n')flag=0;
else
{
if(flag==1)
{ b[i]='\0';i=0;flag=0;m=0;
for(j=0;j<k;j++)
if(strcmp(b,w[j].c)==0){m=1;break;}
if(m) w[j].n++;
else
{w[k].n=1;strcpy(w[k].c,b);k++;}
}
}
if('A'<=ch && ch<='Z') ch+=32;
if('a'<=ch && ch<='z'){b[i]=ch;i++;flag=1;}
}
}
for(i=0;i<k&&i<5;i++)
{
t=0;
while(w[t].n==0) t++;
for(j=1;j<k;j++)
{
if(w[j].n>w[t].n) t=j;
else
if(w[j].n==w[t].n)
{
if(strcmp(w[j].c,w[t].c)<0)
t=j;
}
}
printf("%s %d\n",w[t].c,w[t].n);
w[t].n=0;
}
return 0;
}
貌似哪有错误