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