上机2

/*词频统计
**田晨
**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; 
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值