编写一个程序,打开和读取一个文本文件,并统计文件中每个单词出现的次数。用改进的二叉查找树存储单词及其出现的次数。程序在读入文件后 会提供一个有三个选项菜单。第一个选项是列出所有的单词和出现的次数。第二个选项是让用户输入一个单词,程序报告该单词在文件中出现的次数。 第三个选项是退出

/编写一个程序,打开和读取一个文本文件,并统计文件中每个单词出现的次数。用改进的二叉查找树存储单词及其出现的次数。程序在读入文件后
会提供一个有三个选项菜单。第一个选项是列出所有的单词和出现的次数。第二个选项是让用户输入一个单词,程序报告该单词在文件中出现的次数。
第三个选项是退出
/

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

#define MAX_WORD_LENGTH 100

typedef struct Node
{
    char word[MAX_WORD_LENGTH];
    int count;
    struct Node *left;
    struct Node *right;
} Node;

Node *createNode(const char *word)
{
    Node *newNode = (Node *)malloc(sizeof(Node));
    strcpy(newNode->word, word);
    newNode->count = 1;
    newNode->left = newNode->right = NULL;
    return newNode;
}

Node *insert(Node *root, const char *word)
{
    if (root == NULL)
    {
        return createNode(word);
    }
    if (strcmp(word, root->word) < 0)
    {
        root->left = insert(root->left, word);
    }
    else if (strcmp(word, root->word) > 0)
    {
        root->right = insert(root->right, word);
    }
    else
    {
        root->count++;
    }
    return root;
}

void display(Node *root)
{
    if (root != NULL)
    {
        display(root->left);
        printf("%s: %d\n", root->word, root->count);
        display(root->right);
    }
}

int getCount(Node *root, const char *word)
{
    if (root == NULL)
    {
        return 0;
    }
    if (strcmp(word, root->word) < 0)
    {
        return getCount(root->left, word);
    }
    else if (strcmp(word, root->word) > 0)
    {
        return getCount(root->right, word);
    }
    else
    {
        return root->count;
    }
}

void freeTree(Node *root)
{
    if (root != NULL)
    {
        freeTree(root->left);
        freeTree(root->right);
        free(root);
    }
}

void toLowerCase(char *str)
{
    for (int i = 0; str[i]; i++)
    {
        str[i] = tolower((unsigned char)str[i]);
    }
}

void processFile(const char *filename, Node **root)
{
    FILE *file = fopen(filename, "r");
    if (file == NULL)
    {
        printf("无法打开文件 %s\n", filename);
        return;
    }

    char word[MAX_WORD_LENGTH];
    while (fscanf(file, "%s", word) != EOF)
    {
        toLowerCase(word); // 转换为小写以避免重复
        *root = insert(*root, word);
    }

    fclose(file);
}

int main(int argc,char *argv[])
{
    Node *root = NULL;
    if(argc!=2)
    {
        printf("可执行文件 [查找文件]\n");
    }
    assert(argv[1]);
    processFile(argv[1], &root);
    
    
    int choice;
    do
    {
        printf("\n菜单:\n");
        printf("1. 列出所有单词及出现次数\n");
        printf("2. 查询单词出现次数\n");
        printf("3. 退出\n");
        printf("请选择操作: ");
        scanf("%d", &choice);
        getchar();
        switch (choice)
        {
        case 1:
            printf("单词及出现次数:\n");
            display(root);
            break;
        case 2:
        {
            char queryWord[MAX_WORD_LENGTH];
            printf("请输入要查询的单词: ");
            fgets(queryWord, sizeof(queryWord), stdin);
            queryWord[strcspn(queryWord, "\n")] = 0;
            toLowerCase(queryWord);
            int count = getCount(root, queryWord);
            if (count > 0)
            {
                printf("单词 '%s' 出现次数: %d\n", queryWord, count);
            }
            else
            {
                printf("单词 '%s' 不在文件中。\n", queryWord);
            }
            break;
        }
        case 3:
            printf("退出程序。\n");
            break;
        default:
            printf("无效选择,请重新输入。\n");
            break;
        }
    } while (choice != 3);
    freeTree(root);
    return 0;
}

posted on 2024-08-13 23:33  wessf  阅读(8)  评论(0编辑  收藏  举报