c 程序中频率输出的问题,应将 txt 文件与关键字进行比较并输出频率

问题描述 投票:0回答:1

希望大家一切都好!我对我的编程作业有点困惑,c 代码的目的是将 txt 文件(作为用户输入给出的名称)与另一个关键字 txt 文件进行比较。然后记录每个单词的出现频率并按降序打印。这应该看起来像这样:

euery 8
common 8
gaue 7
thankes 5
vnkle 4
growes 3
wag 3
seal 3
day 3
soft 3

遗憾的是这不是我的代码产生的结果。代码如下所示:

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

#define MAX_FILENAME_LENGTH 256
#define MAX_TEXT_LENGTH 100000
#define MAX_KEYWORDS 100

// Structure to store keyword frequencies
typedef struct KeywordFrequency {
    char keyword[50];
    int frequency;
} KeywordFrequency;

// Compare function for sorting keyword frequencies
int compareKeywords(const void *a, const void *b) {
    return ((KeywordFrequency *)b)->frequency - ((KeywordFrequency *)a)->frequency;
}

int main() {
    char filename[MAX_FILENAME_LENGTH];
    char text[MAX_TEXT_LENGTH];
    KeywordFrequency keywords[MAX_KEYWORDS];
    int totalKeywords = 0;

    // Ask the user for the name of the text file
    printf("Enter the name of the text file: ");
    scanf("%s", filename);

    // Open and read the text file
    FILE *textFile = fopen(filename, "r");
    if (textFile == NULL) {
        printf("Error opening %s\n", filename);
        return 1;
    }

    // Read the content of the text file
    fread(text, 1, sizeof(text), textFile);
    fclose(textFile);

    // Open and read the keyword file
    FILE *keywordFile = fopen("keyword.txt", "r");
    if (keywordFile == NULL) {
        printf("Error opening keyword.txt\n");
        return 1;
    }

    // Initialize keyword frequencies
    while (fscanf(keywordFile, "%s", keywords[totalKeywords].keyword) != EOF) {
        keywords[totalKeywords].frequency = 0;
        totalKeywords++;
    }
    fclose(keywordFile);

    // Tokenize and compare the text with keywords
    char *token = strtok(text, " ");
    while (token != NULL) {
        for (int i = 0; i < totalKeywords; i++) {
            if (strcmp(token, keywords[i].keyword) == 0) {
                keywords[i].frequency++;
            }
        }
        token = strtok(NULL, " ");
    }

    // Sort keyword frequencies in descending order
    qsort(keywords, totalKeywords, sizeof(KeywordFrequency), compareKeywords);

    // Print the results in a table format
    printf("Keyword\tFrequency\n");
    for (int i = 0; i < totalKeywords; i++) {
        printf("%s\t%d\n", keywords[i].keyword, keywords[i].frequency);
    }

    return 0;
}

enter image description here

任何帮助将不胜感激

c read-write
1个回答
0
投票

有两件事:

第一个是

scanf
格式
%s
读取空格分隔单词。所以没有什么可以标记的。你甚至从未在
text
中读入你试图标记化的任何内容;

第二件事是您将输入文件的每个单词读取到单独的“关键字”元素中,这不是您应该做的。如果输入文件中的单词超过 100 个,则会超出数组范围。

相反,您应该读入另一个变量并搜索当前关键字,并查找它是否在数组中,如果是则增加频率,否则添加它。

© www.soinside.com 2019 - 2024. All rights reserved.