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

#define MAX_WORD_LEN 50 #define MAX_INDEX_WORDS 1000

// 读取单词索引表 int read_index_file(char index_file[], char index_words[][MAX_WORD_LEN+1]) { FILE *fp = fopen(index_file, 'r'); if (fp == NULL) { printf('Failed to open index file: %s\n', index_file); return -1; }

int i = 0;
while (fgets(index_words[i], MAX_WORD_LEN+1, fp) != NULL) {
    // 去掉换行符
    index_words[i][strcspn(index_words[i], '\n')] = '\0';
    i++;
    if (i >= MAX_INDEX_WORDS) {
        printf('Too many words in index file: %s\n', index_file);
        break;
    }
}

fclose(fp);
return i;

}

// 将单词转换为小写字母 void to_lower_case(char word[]) { for (int i = 0; word[i] != '\0'; i++) { word[i] = tolower(word[i]); } }

// 检查单词是否在索引表中 int check_word(char word[], char index_words[][MAX_WORD_LEN+1], int num_index_words) { to_lower_case(word); int left = 0, right = num_index_words-1; while (left <= right) { int mid = (left + right) / 2; int cmp = strcmp(word, index_words[mid]); if (cmp == 0) { return 1; } else if (cmp < 0) { right = mid - 1; } else { left = mid + 1; } } return 0; }

// 检查文章中的单词并输出错误单词 void check_article(char article_file[], char index_file[], char error_file[]) { char index_words[MAX_INDEX_WORDS][MAX_WORD_LEN+1]; int num_index_words = read_index_file(index_file, index_words); if (num_index_words <= 0) { return; }

FILE *fp_in = fopen(article_file, 'r');
if (fp_in == NULL) {
    printf('Failed to open article file: %s\n', article_file);
    return;
}

FILE *fp_error = fopen(error_file, 'w');
if (fp_error == NULL) {
    printf('Failed to open error file: %s\n', error_file);
    fclose(fp_in);
    return;
}

char word[MAX_WORD_LEN+1];
while (fscanf(fp_in, '%s', word) != EOF) {
    // 去掉单词中的标点符号
    int len = strlen(word);
    while (len > 0 && !isalpha(word[len-1])) {
        word[len-1] = '\0';
        len--;
    }
    while (len > 0 && !isalpha(word[0])) {
        strcpy(word, word+1);
        len--;
    }
    if (len == 0) {
        continue;
    }

    if (!check_word(word, index_words, num_index_words)) {
        fprintf(fp_error, '%s\n', word);
    }
}

fclose(fp_in);
fclose(fp_error);

}

int main() { check_article('in.txt', 'index.txt', 'error.txt'); return 0; }

C语言实现英文文章单词正确性检查

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

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