C语言实现关键字匹配算法(复制即用)

2023-12-28 15:27:21

前言

无套路,均已上机通过,求个关注求个赞,提供答疑解惑服务。

功能要求

一份C源代码存储在一个文本文件中,请统计该文件中关键字出现的频度,并按此频度对关键字进行排序。要求:

  • 从文本文件InFile.txt读取C源代码,从文本文件Key.txt读取关键字列表。
  • 分别采用如下不同的查找策略进行频度统计:
    • 链式存储上的顺序查找;
    • 基于链地址法的哈希查找。
  • 基于快速排序实现关键字排序。
  • 不论采取哪种查找和排序策略,完成功能均相同。
    • 关键字统计:依次从关键字列表Key.txt中读取关键字,若该关键字未在文本文件InFile.txt中出现,则将其频度计为0;每检索到一次该关键字,则将其频度增加1。统计结束后,将所有关键字及其频度按关键字列表顺序写入文本文件中。其中,无论关键字列表中的关键字是否出现都要计数。不同查找策略所获得的结果分别写入不同的文件(OutFile1.txt,OutFile2.txt)。
    • 关键字排序:根据关键字出现的频度对所有关键字进行从高到低排序,舍弃关键字列表中未出现的关键字。如果关键字出现的频度相等,则按照关键字的首字母从小到大排序,将排序后的关键字及其频度写入文本文件(OutFile3.txt)中。
  • 设计菜单,实现上述功能。

运行截图

在这里插入图片描述

全部代码

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

#define MAX_KEYWORDS 100
#define MAX_KEYWORD_LENGTH 50
#define HASH_TABLE_SIZE 101

typedef struct {
    char keyword[MAX_KEYWORD_LENGTH];
    int frequency;
} KeywordInfo;

typedef struct HashNode {
    KeywordInfo data;
    struct HashNode *next;
} HashNode;

typedef struct {
    HashNode *table[HASH_TABLE_SIZE];
} HashTable;

int compareKeywords(const void *a, const void *b) {
    const KeywordInfo *keywordA = (const KeywordInfo *)a;
    const KeywordInfo *keywordB = (const KeywordInfo *)b;

    if (keywordB->frequency != keywordA->frequency) {
        return keywordB->frequency - keywordA->frequency;
    }

    return strcmp(keywordA->keyword, keywordB->keyword);
}

void initializeHashTable(HashTable *hashTable) {
    int i;
    for (i = 0; i < HASH_TABLE_SIZE; i++) {
        hashTable->table[i] = NULL;
    }
}

unsigned int hashFunction(const char *str) {
    unsigned int hash = 0;
    while (*str) {
        hash = (hash << 5) + *str++;
    }
    return hash % HASH_TABLE_SIZE;
}

HashNode *searchHashTable(HashTable *hashTable, const char *keyword) {
    unsigned int hashValue = hashFunction(keyword);
    HashNode *current = hashTable->table[hashValue];

    while (current != NULL) {
        if (strcmp(current->data.keyword, keyword) == 0) {
            return current;
        }
        current = current->next;
    }

    return NULL;
}

void insertHashTable(HashTable *hashTable, const KeywordInfo *keywordInfo) {
    unsigned int hashValue = hashFunction(keywordInfo->keyword);
    HashNode *newNode = (HashNode *)malloc(sizeof(HashNode));

    if (newNode == NULL) {
        printf("内存分配失败\n");
        exit(1);
    }

    newNode->data = *keywordInfo;
    newNode->next = hashTable->table[hashValue];
    hashTable->table[hashValue] = newNode;
}

void freeHashTable(HashTable *hashTable) {
    int i;
    for (i = 0; i < HASH_TABLE_SIZE; i++) {
        HashNode *current = hashTable->table[i];
        while (current != NULL) {
            HashNode *next = current->next;
            free(current);
            current = next;
        }
    }
}

void countKeywordsSequential(FILE *file, char *keywords[], int numKeywords, KeywordInfo keywordInfo[]) {
    char line[1024];

    rewind(file);

    while (fgets(line, sizeof(line), file) != NULL) {
        for (int i = 0; i < numKeywords; i++) {
            char *pos = strstr(line, keywords[i]);
            while (pos != NULL) {
                if ((pos == line || !isalpha(pos[-1])) && !isalpha(pos[strlen(keywords[i])])) {
                    keywordInfo[i].frequency++;
                }
                pos = strstr(pos + 1, keywords[i]);
            }
        }
    }
}

void countKeywordsHash(FILE *file, HashTable *hashTable, char *keywords[], int numKeywords, KeywordInfo keywordInfo[]) {
    char line[1024];

    rewind(file);

    while (fgets(line, sizeof(line), file) != NULL) {
        for (int i = 0; i < numKeywords; i++) {
            char *pos = strstr(line, keywords[i]);
            while (pos != NULL) {
                if ((pos == line || !isalpha(pos[-1])) && !isalpha(pos[strlen(keywords[i])])) {
                    // 检查哈希表中是否已存在该关键字的节点
                    HashNode *existingNode = searchHashTable(hashTable, keywords[i]);

                    if (existingNode != NULL) {
                        // 如果已存在,则更新哈希表中节点的频度
                        existingNode->data.frequency++;
                    } else {
                        // 如果不存在,则插入新节点到哈希表
                        KeywordInfo newKeywordInfo = { .frequency = 1 };
                        strcpy(newKeywordInfo.keyword, keywords[i]);

                        insertHashTable(hashTable, &newKeywordInfo);
                    }

                    // 更新数组中的频度
                    keywordInfo[i].frequency++;
                }
                pos = strstr(pos + 1, keywords[i]);
            }
        }
    }
}

void writeKeywordStats(FILE *outputFile, KeywordInfo keywordInfo[], int numKeywords) {
    for (int i = 0; i < numKeywords; i++) {
        fprintf(outputFile, "%s: %d\n", keywordInfo[i].keyword, keywordInfo[i].frequency);
    }
}

void writeSortedKeywordStats(FILE *outputFile, KeywordInfo keywordInfo[], int numKeywords) {
    for (int i = 0; i < numKeywords; i++) {
        if (keywordInfo[i].frequency > 0) {
            fprintf(outputFile, "%s: %d\n", keywordInfo[i].keyword, keywordInfo[i].frequency / 2);
        }
    }
}

void writeHashTableStats(FILE *outputFile, HashTable *hashTable, char *keywords[], int numKeywords) {
    for (int i = 0; i < numKeywords; i++) {
        HashNode *node = searchHashTable(hashTable, keywords[i]);
        fprintf(outputFile, "%s: %d\n", keywords[i], (node != NULL) ? node->data.frequency : 0);
    }
}

void sortKeywords(KeywordInfo keywordInfo[], int numKeywords) {
    qsort(keywordInfo, numKeywords, sizeof(KeywordInfo), compareKeywords);
}

int main(void) {
    FILE *keywordFile, *codeFile;
    FILE *outputFile1, *outputFile2, *outputFile3;
    char *keywords[MAX_KEYWORDS];
    int numKeywords = 0;
    char tempKeyword[MAX_KEYWORD_LENGTH];
    KeywordInfo keywordInfo[MAX_KEYWORDS];
    HashTable hashTable;

    // 打开文件
    keywordFile = fopen("Key.txt", "r");
    codeFile = fopen("InFile.txt", "r");
    outputFile1 = fopen("OutFile1.txt", "w");
    outputFile2 = fopen("OutFile2.txt", "w");
    outputFile3 = fopen("OutFile3.txt", "w");

    if (keywordFile == NULL || codeFile == NULL || outputFile1 == NULL || outputFile2 == NULL || outputFile3 == NULL) {
        printf("无法打开文件\n");
        return 1;
    }

    // 读取关键字列表
    while (fscanf(keywordFile, "%s", tempKeyword) != EOF && numKeywords < MAX_KEYWORDS) {
        keywords[numKeywords] = (char *)malloc(strlen(tempKeyword) + 1);
        strcpy(keywords[numKeywords], tempKeyword);
        numKeywords++;
    }

    // 初始化关键字信息数组
    for (int i = 0; i < numKeywords; i++) {
        strcpy(keywordInfo[i].keyword, keywords[i]);
        keywordInfo[i].frequency = 0;
    }

    // 初始化哈希表
    initializeHashTable(&hashTable);

    int choice;
    do {
        // 显示菜单
        printf("\n菜单:\n");
        printf("1. 顺序查找关键字并统计频度(输出到OutFile1.txt)\n");
        printf("2. 哈希查找关键字并统计频度(输出到OutFile2.txt)\n");
        printf("3. 排序关键字并输出到OutFile3.txt\n");
        printf("4. 退出\n");
        printf("请选择操作: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                // 顺序查找关键字并统计频度
                countKeywordsSequential(codeFile, keywords, numKeywords, keywordInfo);
                writeKeywordStats(outputFile1, keywordInfo, numKeywords);
                break;
            case 2:
                // 哈希查找关键字并统计频度
                countKeywordsHash(codeFile, &hashTable, keywords, numKeywords, keywordInfo);
                writeHashTableStats(outputFile2, &hashTable, keywords, numKeywords);
                break;
            case 3:
                // 排序关键字并输出
                sortKeywords(keywordInfo, numKeywords);
                writeSortedKeywordStats(outputFile3, keywordInfo, numKeywords);
                break;
            case 4:
                // 退出
                break;
            default:
                printf("无效的选项,请重新选择\n");
                break;
        }
    } while (choice != 4);

    // 关闭文件和释放内存
    fclose(keywordFile);
    fclose(codeFile);
    fclose(outputFile1);
    fclose(outputFile2);
    fclose(outputFile3);

    for (int i = 0; i < numKeywords; i++) {
        free(keywords[i]);
    }

    freeHashTable(&hashTable);

    return 0;
}

文章来源:https://blog.csdn.net/qq_45295475/article/details/135269556
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。