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

#define MAX_WORD_LEN 50 #define MAX_WORDS_NUM 1000

int main() { char index_file[] = "index.txt"; char in_file[] = "in.txt"; char error_file[] = "error.txt";

// 读取单词索引表
FILE* fp_index = fopen(index_file, "r");
if (fp_index == NULL)
{
    printf("Failed to open file %s\n", index_file);
    return 1;
}
char index_words[MAX_WORDS_NUM][MAX_WORD_LEN + 1];
int index_words_num = 0;
while (fgets(index_words[index_words_num], MAX_WORD_LEN + 1, fp_index) != NULL)
{
    index_words_num++;
}
fclose(fp_index);

// 读取英文文章
FILE* fp_in = fopen(in_file, "r");
if (fp_in == NULL)
{
    printf("Failed to open file %s\n", in_file);
    return 1;
}
char line[MAX_WORD_LEN * MAX_WORDS_NUM + 1];
char* words[MAX_WORDS_NUM];
int words_num = 0;
while (fgets(line, MAX_WORD_LEN * MAX_WORDS_NUM + 1, fp_in) != NULL)
{
    int len = strlen(line);
    if (line[len - 1] == '\n')
    {
        line[len - 1] = '\0';
    }
    char* p = strtok(line, " ");
    while (p != NULL)
    {
        words[words_num] = (char*)malloc(strlen(p) + 1);
        strcpy(words[words_num], p);
        words_num++;
        p = strtok(NULL, " ");
    }
}
fclose(fp_in);

// 检查单词正确性并输出错误单词
FILE* fp_error = fopen(error_file, "w");
if (fp_error == NULL)
{
    printf("Failed to open file %s\n", error_file);
    return 1;
}
int i, j;
for (i = 0; i < words_num; i++)
{
    // 转换为小写字母
    int len = strlen(words[i]);
    for (j = 0; j < len; j++)
    {
        words[i][j] = tolower(words[i][j]);
    }
    // 在单词索引表中查找
    int flag = 0;
    for (j = 0; j < index_words_num; j++)
    {
        if (strcmp(words[i], index_words[j]) == 0)
        {
            flag = 1;
            break;
        }
    }
    // 输出错误单词
    if (!flag)
    {
        fprintf(fp_error, "%s\n", words[i]);
    }
}
fclose(fp_error);

// 释放内存
for (i = 0; i < words_num; i++)
{
    free(words[i]);
}

return 0;

}

C语言英文文章单词正确性检查程序

原文地址: https://www.cveoy.top/t/topic/gmFW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录