小霖木木 2022-03-24 21:02 采纳率: 100%
浏览 95
已结题

c语言在文中查找100个单词

输入文本文件的名称应用户的输入来提供。也就是说,输入文件不应该被硬编码到程序的源代码中,但是程序应该向用户询问输入文件。
程序应打印输入文件中的单词总数、输入文件中不同单词的数量,以及最常出现的100个单词及其频率,这些单词按其频率按降序排列。该程序还应测量和打印处理时间。

  • 写回答

3条回答 默认 最新

  • 丨封尘绝念斩丨 2022-03-24 22:02
    关注
    
    #include<stdio.h>
    #include<ctype.h>
    #include<string.h>
    #include<stdlib.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define HASH_TABLE_SIZE 49999
    
    typedef struct word {
        char wrd[80];
        int num;
    }word;
    static int diff_words = 0;
    static int total_words = 0;
    
    word* hashtable[HASH_TABLE_SIZE];
    
    int hash(int number) {
        return number % HASH_TABLE_SIZE;
    }
    void insert(int count, char* w)
    {
        int pos = hash(count);
        word* newWord;
        total_words++;
        while (hashtable[pos])
        {
            if (!strcmp(hashtable[pos]->wrd, w))
            {
                hashtable[pos]->num++;
                return;
            }
            else
            {
                pos++;
            }
        }
        newWord = (word*)malloc(sizeof(word));
        newWord->num = 1;
        strcpy(newWord->wrd, w);
        hashtable[pos] = newWord;
        diff_words++;
    }
    
    
    int main()
    {
        FILE* fp;
        char wrd_temp[80], ch;
        word* head, * curr;
        int i, arr[100], count = 0, j;
    
        word* temp = (word*)malloc(sizeof(word));
        memset(hashtable, 0, HASH_TABLE_SIZE);
        fp = fopen("TheGun.txt", "r");   //只读方式打开,这里输入你的文件路径
        i = 0;
        while (1)
        {
            ch = fgetc(fp);
            if (isalpha(ch) || ch == '\'')
            {
                if (islower(ch))
                {
                    wrd_temp[i++] = ch - 32;  //小写字母转大写
                    count = count + ch - 32;  //count用于计算单词的哈希值
                }
                else
                {
                    wrd_temp[i++] = ch;
                    count = count + ch;
                }
            }
            else
            {
                wrd_temp[i++] = '\0';
                if (strlen(wrd_temp) >= 1)
                {
                    insert(count, wrd_temp);
                }
                memset(wrd_temp, 0, 80);
                i = 0;
                count = 0;
            }
            if (feof(fp))
                break;
        }
        fclose(fp);
        printf("Number of total_words = %d\n", total_words);
        printf("Number of different words = %d\n", diff_words);
        printf("The 100 most common words:\n");
        printf("WORD            NUMBER OF OCCURRENCES\n");
        for (i = 0; i < 10; i++)
        {
            count = 0;
            for (j = 0; j < HASH_TABLE_SIZE; j++)
            {
                if (hashtable[j] != NULL && hashtable[j]->num > count)
                {
                    count = hashtable[j]->num;
                    arr[i] = j;
                }
            }
            printf("%s %d\n", hashtable[arr[i]]->wrd, hashtable[arr[i]]->num);
            hashtable[arr[i]]->num = 0;
        }
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月2日
  • 已采纳回答 3月25日
  • 创建了问题 3月24日

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题