m0_56540752
2021-07-02 14:06
采纳率: 100%
浏览 33

用c语言统计英语文章单词的数目

将一篇英文文章存入数据文件IN.DAT ( 每行的长度均小于80个字符,单词之间用空格或圆点隔开)。设计程序,统计各单词出现的次数(不区分大小写)。并按照出现次数由多到少的顺序显示统计结果,并将结果输出到文件0UT.DAT中。

(1)使用子函数实现各项具体功能;

(2)程序中需要添加适当的注释。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • CSDN专家-link 2021-07-02 15:17
    已采纳
    char words[1000][20]; //最多1000个单词,单词长度不超过20个字母
    int n; //当前单词数量
    int a[1000];//各单词对应出现的次数
    
    void upper2lower(char *word)
    {
      for(int i=0;word[i] != 0;i++)
        if(word[i] >= 'A' && word[i] <= 'Z')
          word[i] += 'a' - 'A';
    }
    
    int findword(char *word)
    {
      for(int i=0;i<n;i++)
        if(strcmp(words[i],word)==0)
          return i;
      return -1;
    }
    
    void parseline(char *line)
    {
       char *p = line;
       char word[20] = {0};
       int w = 0;
       while(*p != 0)
      {
        if(*p == ' ' || *p == '.')
        {
          if(w > 0)
          {
            int pos = findword(word);
            if(pos >= 0)
              a[pos]++;
            else
              strcpy(words[n++],word);
            memset(word,0,20);
            w = 0;
          }
        }
        else
        {
          word[w++] = *p;
        }
      }
       p++; 
    }
    
    void readin()
    {
      FILE *fp = fopen("IN.DAT","r");
      if(fp == NULL)
      {
        printf("file open error\n");
        return;
      }
      char buf[100] = {0};
      while(fgets(buf,100,fp))
      {
        parseline(buf);
      }
      fclose();
    }
    
    void saveout()
    {
      FILE *fp = fopen("OUT.DAT","w");
      if(fp == NULL)
      {
        printf("file open error\n");
        return;
      }
      for(int i=0;i<n;i++)
      {
        fprintf(fp,"%s %d\n",words[i],a[i]);
      }
      fclose();
    }
    
    void sort()
    {
      int i,j,k;
      char word[20] = {0};
      for(i=0;i<n-1;i++)
        for(j=0;j<n-i-1;j++)
        {
          if(a[j] < a[j+1])
          {
            k = a[j];
            a[j] = a[j+1];
            a[j+1] = k;
            strcpy(word,words[j]);
            strcpy(words[j],words[j+1]);
            strcpy(words[j+1],word);
          }
        }
    }
    
    void showwords()
    {
        for(int i=0;i<n;i++)
        {
          printf("%s %d\n",words[i],a[i]);
        }
    }
    
    int main()
    {
      readline();
      sort();
      showwords();
      saveout();
      return 0;
    }
    
    点赞 1 评论
  • CSDN专家-link 2021-07-02 14:09

    检索空格和圆点,分解出单词即可

    点赞 评论
  • m0_56540752 2021-07-02 21:00
    #include <stdio.h>
    

    #include <stdlib.h>
    #include <string.h>

    char words[1000][20];
    int n;
    int a[1000];

    void upper2lower(char *word)
    {
    int i;
    for( i=0;word[i] != 0;i++)
    if(word[i] >= 'A' && word[i] <= 'Z')
    word[i] += 'a' - 'A';
    }

    int findword(char *word)
    {
    int i;
    for( i=0;i<n;i++)
    if(strcmp(words[i],word)==0)
    return i;
    return -1;
    }

    void parseline(char *line)
    {
    char *p = line;
    char word[20] = {0};
    int w = 0;
    while(*p != 0)
    {
    if(*p == ' ' || *p == '.')
    {
    if(w > 0)
    {
    int pos = findword(word);
    if(pos >= 0)
    a[pos]++;
    else
    strcpy(words[n++],word);
    memset(word,0,20);
    w = 0;
    }
    }
    else
    {
    word[w++] = *p;
    }
    }
    p++;
    }

    void writeline()
    {
    char x_txt;
    scanf("%s",x_txt);
    FILE *fp;
    fp=fopen("IN.DAT.txt","w");
    if(fp == NULL)
    {
    return 0;
    }
    fprintf(fp,"%s",x_txt);
    fclose(fp);
    }

    void readline()
    {
    FILE *fp = fopen("IN.DAT.txt","r");
    if(fp == NULL)
    {
    printf("file open error\n");
    return;
    }
    char buf[100] = {0};
    while(fgets(buf,100,fp))
    {
    parseline(buf);
    }
    fclose(fp);
    }

    void saveout()
    {
    FILE *fp = fopen("OUT.DAT.txt","w");
    if(fp == NULL)
    {
    printf("file open error\n");
    return;
    }
    int i;
    for( i=0;i<n;i++)
    {
    fprintf(fp,"%s %d\n",words[i],a[i]);
    }
    fclose(fp);
    }

    void sort()
    {
    int i,j,k;
    char word[20] = {0};
    for(i=0;i<n-1;i++)
    for(j=0;j<n-i-1;j++)
    {
    if(a[j] < a[j+1])
    {
    k = a[j];
    a[j] = a[j+1];
    a[j+1] = k;
    strcpy(word,words[j]);
    strcpy(words[j],words[j+1]);
    strcpy(words[j+1],word);
    }
    }
    }

    void showwords()
    {
    int i;
    for( i=0;i<n;i++)
    {
    printf("%s %d\n",words[i],a[i]);
    }
    }

    int main()
    {
    writeline();
    readline();
    sort();
    showwords();
    saveout();
    return 0;
    }

    点赞 评论

相关推荐 更多相似问题