【难度系数】4 级
【任务描述】 对任意一篇英文文章,统计其中每个单词分别出现了多少次,并可替换指定的单词。
【功能要求】
⑴ 英文文章以文件形式输入。
⑵ 统计的结果保存到文件。
⑶ 对单词进行替换时,允许用户选择全部替换或逐个替换。替换完成后,将文章保存到文件。
#include
#include
#include
#include
//定义最大单词长度
#define MAX_WORDS 1000
#define MAX_WORD_LENGTH 50
#define MAX_FILENAME_LENGTH 100
//定义结构体存储文件
typedef struct
{
char word[MAX_WORD_LENGTH];
int count;
} WordCount;
//统计单词
void countWords(FILE* file, WordCount* wordCounts, int* numWords)
{
char word[MAX_WORD_LENGTH];
char ch;
int wordStart = 0;
int i, j;
*numWords = 0;
while ((ch = fgetc(file)) != EOF)
{
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
//大写转小写
word[wordStart++] = tolower(ch);
}
else
{
//单词末尾
word[wordStart] = 0;
int found = 0;
for (i = 0; i < *numWords; i++)
{
if (strcmp(word, wordCounts[i].word) == 0)
{
found = 1;
wordCounts[i].count++;
break;
}
}
if (!found)
{
if (*numWords < MAX_WORDS)
{
strcpy(wordCounts[*numWords].word, word);
wordCounts[*numWords].count = 1;
(*numWords)++;
}
else
{
printf("非法,超过单词的最大限制\n");
return;
}
}
wordStart = 0;
}
}
}
//写入单词个数到文件中
void writeWordCountsToFile(FILE* file, WordCount* wordCounts, int numWords)
{
for (int i = 0; i < numWords; i++) {
fprintf(file, "%s: %d\n", wordCounts[i].word, wordCounts[i].count);
}
}
//逐个替换
void replaceOneOccurrenceInFile(const char* fileName, const char* oldWord, const char* newWord) {
FILE* file = fopen(fileName, "r");
if (file == NULL) {
printf("无法打开文件\n");
return;
}
FILE* tempFile = fopen("passage_div.txt", "w");
if (tempFile == NULL) {
printf("无法打开文件\n");
fclose(file);
return;
}
char word[MAX_WORD_LENGTH];
int wordStart = 0;
char ch,choice;
int occurrenceCount = 1;
int count=1;
while ((ch = fgetc(file)) != EOF) {
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
word[wordStart++] = tolower(ch);
} else {
if (wordStart > 0) {
word[wordStart] = '\0';
if (strcmp(word, oldWord) == 0) {
printf("你需要将第%d个'%s'替换掉吗?(y/n)\n",count,oldWord);
scanf(" %c",&choice);
if (choice=='y'||choice=='Y') {
printf("第 %d 个原来的 '%s' 换成 '%s'.\n", count, oldWord, newWord);
fputs(newWord, tempFile);
occurrenceCount++;
}else{
fputs(word, tempFile);
}
count++;
} else {
fputs(word, tempFile);
}
fputc(ch, tempFile);
wordStart = 0;
} else {
fputc(ch, tempFile);
}
}
}
fclose(file);
fclose(tempFile);
if(occurrenceCount==0){
printf("替换失败,原因:无替换的内容\n");
}else{
printf("替换成功, %d 个当前的 '%s' 替换成 '%s'.\n", occurrenceCount-1, oldWord, newWord);
}
remove(fileName);
rename("passage_div.txt", fileName);
}
//替换所有单词
void replaceAllOccurrences(char* text, char* oldWord, char* newWord)
{
char* occurrence;
while ((occurrence = strstr(text, oldWord)))
{
memmove(occurrence + strlen(newWord), occurrence + strlen(oldWord), strlen(occurrence + strlen(oldWord)) + 1);
strncpy(occurrence, newWord, strlen(newWord));
}
}
//核心进程(包含界面调用与函数带调用)
void coreprogress(int kk)
{
char fileName[MAX_FILENAME_LENGTH];
char oldWord[MAX_WORD_LENGTH];
char newWord[MAX_WORD_LENGTH];
FILE* file;
WordCount wordCounts[MAX_WORDS];
int numWords;
strcpy(fileName,"passage.txt");
file = fopen(fileName, "r");
if (file == NULL)
{
printf("无法打开文件\n");
return;
}
if(kk==0)
{
FILE *fl= fopen("wordcount.txt","w");
if(fl==NULL)
{
printf("无法打开文件\n");
return;
}
// 计算单词个数
countWords(file, wordCounts, &numWords);
fclose(file);
// 输出单词个数(控制板)
printf("单词个数:\n");
for (int i = 0; i < numWords; i++)
{
printf("%s: %d\n", wordCounts[i].word, wordCounts[i].count);
}
//输出单词个数(文件)
fprintf(fl,"单词个数\n");
for (int i = 0; i < numWords; i++)
{
if(wordCounts[i].word[0]!=' '){
fprintf(fl,"%s: %d\n", wordCounts[i].word, wordCounts[i].count);
}
}
fclose(fl);
}
if (kk==1)
{
// 替换单词
printf("输入需要替换的单词: ");
scanf("%s", oldWord);
printf("输入新的单词:");
scanf("%s", newWord);
printf("确认替换所有单词吗?(y/n): ");
char replaceAll;
scanf(" %c", &replaceAll);
if (replaceAll == 'y' || replaceAll == 'Y')
{
file = fopen(fileName, "r");
if (file == NULL)
{
printf("无法打开文件\n");
return;
}
// 读入所有文件存入text中
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
char *text = (char *) malloc(fileSize + 1);
fread(text, 1, fileSize, file);
text[fileSize] = '\0';
fclose(file);
// 全部替换
replaceAllOccurrences(text, oldWord, newWord);
file = fopen(fileName, "w");
if (file == NULL) {
printf("无法打开文件\n");
return;
}
// 写出文件
fwrite(text, 1, strlen(text), file);
fclose(file);
free(text);
puts("替换成功");
}
else
{
//逐个替换功能
replaceOneOccurrenceInFile(fileName, oldWord, newWord);
puts("替换成功");
}
}
}
//开始界面
void startui()
{
printf("\t\t\t\t-----------------------------------\n");
printf("\t\t\t\t| 欢迎使用单词查找替换系统 |\n");
printf("\t\t\t\t-----------------------------------\n");
printf("\t\t\t\t| 0.统计单词个数 |\n");
printf("\t\t\t\t-----------------------------------\n");
printf("\t\t\t\t| 1.单词替换 |\n");
printf("\t\t\t\t-----------------------------------\n");
printf("\t\t\t\t| 2.退出系统 |\n");
printf("\t\t\t\t-----------------------------------\n");
printf("\t\t\t\t请选择功能: ");
int n;
scanf("%d",&n);
getchar();
switch (n)
{
case 0:
coreprogress(0);
break;
case 1:
coreprogress(1);
break;
case 2:
puts("欢迎下次再来");
break;
default:
break;
}
}
//主函数
int main()
{
startui();
return 0;
}