/*词频统计
**田晨
**2018.9.20
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct word
{
char w[50];
char temp[50];
int count;
}t[100];
int main()
{
char cl[150];
int q=0;
int i=0;
int dele=0;
FILE *fp;
if((fp=fopen("in.txt","r"))==NULL)
exit(1);
while(fscanf(fp,"%s",cl)==1)
{
printf("%s ",cl);
strcpy(t[i].w,cl);
i++;
}
fclose(fp);
printf("\n共有%d个单词\n",i);
for(int j=0;j<i;j++)
{
for(int k=j+1;k<i;k++)
{
if(strcmp(t[j].w,t[k].w)==0)
{
t[j].count++;
dele++;
int m=k;
while(m<i)
{
strcpy(t[m].w,t[m+1].w);
m++;
}
}
}
}
for(int n=0;n<i-dele;n++)
{
for(int a=n+1;a<i-dele;a++)
{
if(strcmp(t[n].w,t[a].w)>0)
{
strcpy(t[0].temp,t[n].w);
strcpy(t[n].w,t[a].w);
strcpy(t[a].w,t[0].temp);
t[n].count=t[n].count+t[a].count;
t[a].count=t[n].count-t[a].count;
t[n].count=t[n].count-t[a].count;
}
}
}
for(int b=0;b<i-dele;b++)
{
printf("%s: ",t[b].w);
printf("%d次",t[b].count+1);
printf("\n");
fp=fopen("out.txt","w");
fputs("\n",fp);
for(int b=0;b<i-dele;b++){
q=strlen(t[b].w);
fputs(t[b].w,fp);
for(int g=0;g<16-q;g++)
fputs(" ",fp);
fprintf(fp,"%d",t[b].count+1);fputs("\n",fp);
}
fclose(fp);
}
return 0;
}
上机2
猜你喜欢
转载自blog.csdn.net/qq_38538626/article/details/82952696
今日推荐
周排行