这是平时的练习,请求集思广益

给定一个文本文件,要求统计给定单词在文本中出现的总次数,并检索输出
某个单词出现在文本中的行号、在该行中出现的次数以及位置。
【功能要求】
(1)建立一个文本文件,文件名由用户用键盘输入;
(2)给定单词计数,输入一个不含空格的单词,统计输出该单词在文本中的出现次数;(提示:采用模式匹配算法实现)
(3)检索给定单词,输入一个单词,检索并输出该单词所在的行号、该行中出现的次数以及在该行中的相应位置。(提示:采用模式匹配算法实现)
有界面菜单选项,应包含:建立文件、统计单词、查找单词、退出程序等,要求使用c语言

  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7588239
  • 你也可以参考下这篇文章:用C语言实现:对关键字码构成哈希表并输出,输出操作次数,输出平均查找长度,输入一个数,输出所在哈希表位置
  • 除此之外, 这篇博客: 指针和数组试题解析(重置版)中的 小编,在这里想说一下,c语言的最后一节 C预处理,可能还需要一些时间,因为小编,昨天才下载了虚拟机 和 linux 系统,还没开始安装。所以无法着手写,因为 C预处理,vs2013很难表达,也就意味可能会讲不清楚。所以这篇文章可能需要点时间,再加上小编初期的文章,是没有排版的(而且可能有些错误,请大家以重置版为准),所以这几天我就把这些重新写。有兴趣的朋友可以看看。(ps:如果哪一天没有更新,意味着小编正在努力学习,为了能给大家呈现一片详细好懂的文章。) 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:


  • 以下回答由chatgpt基于相关博客总结生成:

    我可以给出以下的解决方案:

    1. 用户可以通过界面菜单选项建立一个文本文件,文件名由用户输入。
    #include <stdio.h>
    
    int main() {
      char filename[100];
      printf("请输入文件名:");
      scanf("%s", filename);
      FILE* fp;
      fp = fopen(filename, "w"); //打开文件,读写方式
      fclose(fp); //操作完成后关闭文件
      printf("文件%s创建成功\n", filename);
      return 0;
    }
    
    1. 用户可以输入一个不含空格的单词并统计该单词在文本中的出现次数,采用模式匹配算法实现。
    #include <stdio.h>
    #include <string.h>
    
    int main() {
      char filename[100];
      printf("请输入文件名:");
      scanf("%s", filename);
      char word[100];
      printf("请输入需要统计的单词:");
      scanf("%s", word);
    
      FILE* fp;
      fp = fopen(filename, "r");
      if (fp == NULL) {
        printf("文件不存在,创建文件中...\n");
        fp = fopen(filename, "w");
        fclose(fp);
        printf("文件创建成功!\n");
        return 0;
      }
    
      char line[1000];
      int count = 0;
      int len = strlen(word);
      while (fgets(line, 1000, fp) != NULL) {
        char* p = line;
        while ((p = strstr(p, word)) != NULL) {
          count++;
          p += len;
        }
      }
      printf("单词%s在文件%s中出现了%d次\n", word, filename, count);
      fclose(fp);
      return 0;
    }
    
    1. 用户可以输入一个单词并检索输出该单词所在的行号、该行中出现的次数以及在该行中的相应位置,采用模式匹配算法实现。
    #include <stdio.h>
    #include <string.h>
    
    int main() {
      char filename[100];
      printf("请输入文件名:");
      scanf("%s", filename);
      char word[100];
      printf("请输入需要查找的单词:");
      scanf("%s", word);
    
      FILE* fp;
      fp = fopen(filename, "r");
      if (fp == NULL) {
        printf("文件不存在,创建文件中...\n");
        fp = fopen(filename, "w");
        fclose(fp);
        printf("文件创建成功!\n");
        return 0;
      }
    
      char line[1000];
      int count = 0;
      int lineno = 0;
      int len = strlen(word);
      while (fgets(line, 1000, fp) != NULL) {
        lineno++;
        char* p = line;
        while ((p = strstr(p, word)) != NULL) {
          count++;
          printf("在第%d行,第%d个字符处找到单词%s\n", lineno, (int)(p - line) + 1, word);
          p += len;
        }
      }
      printf("单词%s在文件%s中共出现了%d次\n", word, filename, count);
      fclose(fp);
      return 0;
    }
    
    1. 该程序应该使用C语言实现,并提供给用户以下功能菜单选项:建立文件、统计单词、查找单词、退出程序。
    #include <stdio.h>
    #include <string.h>
    
    int main() {
      while (1) {
        printf("请选择以下操作:\n");
        printf("1.建立文件\n2.统计单词\n3.查找单词\n4.退出程序\n");
        int choice;
        scanf("%d", &choice);
    
        if (choice == 1) {
          char filename[100];
          printf("请输入文件名:");
          scanf("%s", filename);
          FILE* fp;
          fp = fopen(filename, "w");
          fclose(fp);
          printf("文件%s创建成功\n", filename);
        } else if (choice == 2) {
          char filename[100];
          printf("请输入文件名:");
          scanf("%s", filename);
          char word[100];
          printf("请输入需要统计的单词:");
          scanf("%s", word);
    
          FILE* fp;
          fp = fopen(filename, "r");
          if (fp == NULL) {
            printf("文件不存在,创建文件中...\n");
            fp = fopen(filename, "w");
            fclose(fp);
            printf("文件创建成功!\n");
            continue;
          }
    
          char line[1000];
          int count = 0;
          int len = strlen(word);
          while (fgets(line, 1000, fp) != NULL) {
            char* p = line;
            while ((p = strstr(p, word)) != NULL) {
              count++;
              p += len;
            }
          }
          printf("单词%s在文件%s中出现了%d次\n", word, filename, count);
          fclose(fp);
        } else if (choice == 3) {
          char filename[100];
          printf("请输入文件名:");
          scanf("%s", filename);
          char word[100];
          printf("请输入需要查找的单词:");
          scanf("%s", word);
    
          FILE* fp;
          fp = fopen(filename, "r");
          if (fp == NULL) {
            printf("文件不存在,创建文件中...\n");
            fp = fopen(filename, "w");
            fclose(fp);
            printf("文件创建成功!\n");
            continue;
          }
    
          char line[1000];
          int count = 0;
          int lineno = 0;
          int len = strlen(word);
          while (fgets(line, 1000, fp) != NULL) {
            lineno++;
            char* p = line;
            while ((p = strstr(p, word)) != NULL) {
              count++;
              printf("在第%d行,第%d个字符处找到单词%s\n", lineno, (int)(p - line) + 1, word);
              p += len;
            }
          }
          printf("单词%s在文件%s中共出现了%d次\n", word, filename, count);
          fclose(fp);
        } else if (choice == 4) {
          break;
        }
      }
      return 0;
    }
    
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_LINE_LENGTH 1024
#define MAX_FILENAME_LENGTH 256
#define MAX_WORD_LENGTH 256

typedef struct {
    char *line;
    int lineNumber;
    int wordCount;
    int *wordPositions;
} LineData;

void printMenu() {
    printf("1. 建立文件");
    printf("2. 统计单词");
    printf("3. 查找单词");
    printf("4. 退出程序");
}

void createFile(char fileName[]) {
    FILE *file = fopen(fileName, "w");
    if (file == NULL) {
        printf("无法创建文件 %s", fileName);
        return;
    }
    printf("请输入文本内容,结束请输入\"exit\"\n");
    char line[MAX_LINE_LENGTH];
    while (fgets(line, MAX_LINE_LENGTH, stdin) != NULL) {
        if (strcmp(line, "exit") == 0) {
            break;
        }
        fputs(line, file);
    }
    fclose(file);
    printf("文件 %s 创建成功", fileName);
}

int countWords(char fileName[], char word[]) {
    FILE *file = fopen(fileName, "r");
    if (file == NULL) {
        printf("无法打开文件 %s", fileName);
        return -1;
    }
    char line[MAX_LINE_LENGTH];
    int count = 0;
    while (fgets(line, MAX_LINE_LENGTH, file) != NULL) {
        char *match = line;
        while ((match = strstr(match, word)) != NULL) {
            count++;
            match += strlen(word);
        }
    }
    fclose(file);
    printf("文件 %s 中 \"%s\" 一共出现了 %d 次", fileName, word, count);
    return count;
}

LineData *searchWord(char fileName[], char word[], int *lineCount) {
    FILE *file = fopen(fileName, "r");
    if (file == NULL) {
        printf("无法打开文件 %s", fileName);
        return NULL;
    }
    char line[MAX_LINE_LENGTH];
    int lineNumber = 0;
    int matchCount = 0;
    LineData *matches = malloc(sizeof(LineData));
    while (fgets(line, MAX_LINE_LENGTH, file) != NULL) {
        lineNumber++;
        char *match = line;
        int *wordPositions = NULL;
        int wordPositionCount = 0;
        while ((match = strstr(match, word)) != NULL) {
            matchCount++;
            wordPositionCount++;
            wordPositions = realloc(wordPositions, wordPositionCount * sizeof(int));
            wordPositions[wordPositionCount - 1] = match - line;
            match += strlen(word);
        }
        if (wordPositionCount > 0) {
            matches = realloc(matches, matchCount * sizeof(LineData));
            matches[matchCount - 1].line = strdup(line);
            matches[matchCount - 1].lineNumber = lineNumber;
            matches[matchCount - 1].wordCount = wordPositionCount;
            matches[matchCount - 1].wordPositions = wordPositions;
        } else {
            free(wordPositions);
        }
    }
    fclose(file);
    *lineCount = matchCount;
    return matches;
}

void freeMatches(LineData *matches, int matchCount) {
    for (int i = 0; i < matchCount; i++) {
        free(matches[i].line);
        free(matches[i].wordPositions);
    }
    free(matches);
}

int main() {
    char fileName[MAX_FILENAME_LENGTH];
    char word[MAX_WORD_LENGTH];
    int option = -1;
    while (option != 4) {
        printMenu();
        printf("请输入选项:");
        scanf("%d", &option);
        switch(option) {
            case 1:
                printf("请输入文件名:");
                scanf("%s", fileName);
                createFile(fileName);
                break;
            case 2:
                printf("请输入文件名和单词,之间用空格分隔:");
                scanf("%s %s", fileName, word);
                countWords(fileName, word);
                break;
            case 3: {
                int matchCount = 0;
                printf("请输入文件名和单词,之间用空格分隔:");
                scanf("%s %s", fileName, word);
                LineData *matches = searchWord(fileName, word, &matchCount);
                if (matches == NULL) {
                    break;
                }
                printf("共找到 %d 个匹配项:\n", matchCount);
                for (int i = 0; i < matchCount; i++) {
                    printf("第 %d 行,出现 %d 次:\n    %s", matches[i].lineNumber, matches[i].wordCount, matches[i].line);
                    printf("\t位置:");
                    for (int j = 0; j < matches[i].wordCount; j++) {
                        printf("%d ", matches[i].wordPositions[j]);
                    }
                    printf("\n");
                }
                freeMatches(matches, matchCount);
                break;
            }
            case 4:
                printf("退出程序");
                break;
            default:
                printf("无效选项 %d", option);
        }
    }
    return 0;
}