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

#define MAX_WORD_LEN 50 #define MAX_WORDS 1000

int cmp(const void *a, const void b) { return strcmp((char **)a, *(char **)b); }

int main() { // 读取单词索引表 char *words[MAX_WORDS]; int n = 0; FILE *fp = fopen('index.txt', 'r'); if (fp == NULL) { printf('Failed to open index file. '); return 1; } char buf[MAX_WORD_LEN + 1]; while (fgets(buf, MAX_WORD_LEN + 1, fp) != NULL) { int len = strlen(buf); if (buf[len - 1] == '\n') { buf[len - 1] = '\0'; } words[n] = strdup(buf); n++; } fclose(fp);

// 对单词索引表进行排序
qsort(words, n, sizeof(char *), cmp);

// 读取英文文章并进行单词检查
fp = fopen('in.txt', 'r');
if (fp == NULL) {
    printf('Failed to open input file.

'); return 1; } FILE *fout = fopen('error.txt', 'w'); if (fout == NULL) { printf('Failed to open output file. '); return 1; } char word[MAX_WORD_LEN + 1]; int word_len = 0; int c; while ((c = fgetc(fp)) != EOF) { if (isalpha(c)) { if (word_len < MAX_WORD_LEN) { word[word_len] = tolower(c); word_len++; } } else { if (word_len > 0) { word[word_len] = '\0'; int found = 0; for (int i = 0; i < n; i++) { if (strcmp(word, words[i]) == 0) { found = 1; break; } } if (!found) { fprintf(fout, '%s\n', word); } word_len = 0; } } } if (word_len > 0) { word[word_len] = '\0'; int found = 0; for (int i = 0; i < n; i++) { if (strcmp(word, words[i]) == 0) { found = 1; break; } } if (!found) { fprintf(fout, '%s\n', word); } }

// 释放内存并关闭文件
for (int i = 0; i < n; i++) {
    free(words[i]);
}
fclose(fp);
fclose(fout);

return 0;

}


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

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