该程序利用一个给定的单词索引表,对一篇英文文章进行单词正确性检查,并将所有不在索引表中的单词(不区分大小写)输出到一个单独的文件中。

假设:

  1. in.txt中的文章有可能没有经过排版,格式有可能杂乱无章,也有可能没有写完整。
  2. index.txt中的单词个数不超过1000个,每个单词的长度不超过50个字母。
  3. 若出错的单词多次出现,则多次输出。

输入形式

保存单词索引表的文件index.txt和保存英文文章的文件in.txt都位于当前目录下。

输出形式

将出错的单词以字典序由小到大的顺序输出到当前目录下的文件error.txt中,每个单词单独占一行,多次出错的单词多次输出。若没有出现错误单词,则什么也不输出。

样例输入1

假设文件in.txt内容为:

There are two verrsions of the international standards for C. Thee first version was ratified in 1989 by the American National Standards Institue (ANS1) C standard committee.It is often referred as ANS1 C or C89. The secand C standard was completed in 1999. This standard is comonly referred to as C99. C99 is a milestone in C's evolution into a viable programing languga for numerical and scientific computing.

文件index.txt中的单词索引表内容为:

a american and ansi are as by c committee commonly completed computing evolution first for in institue international into is it language milestone national numerical of often or programming ratified referred s scientific secand standard standards the there this to two version versions viable was

样例输出1

文件error.txt中出错的单词应为:

ans ans comonly languga programing thee verrsions

样例1说明

用index.txt中的单词索引表对in.txt中出现的每一个单词进行检查,检查时大小写无关,所以第一个单词There出现在索引表中,不是错误单词;单词verrsions没有出现在索引表中,拼写错误,所以作为出错单词输出;单词ANSI拼写成了ANS1,将其中字母都转换为小写后输出,并且多次出现,多次输出;其他出错单词类似。错误单词输出按照字典序由小到大输出到error.txt文件中。

样例输入2

假设文件in.txt内容为:

There are two versions of the international standard fo

文件index.txt中的单词索引表内容为:

are for international of standards the there two versions

样例输出2

文件error.txt中出错的单词应为:

fo standard

样例2说明

文件in.txt中的单词standard没有出现在索引表文件index.txt中,所以作为错误单词输出。 注意:样例2中in.txt文件内容还不完整,最后的单词fo后没有任何字符,fo也没有出现在索引表中,所以也作为错误单词输出 直接用c代码编写 对单词进行排序内容:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>

#define MAX_WORD_LEN 50#define MAX_WORD_NUM 1000#define MAX_LINE_LEN 1000

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

int main() { // 读取单词索引表 char words[MAX_WORD_NUM][MAX_WORD_LEN + 1]; int word_num = 0; FILE *fp_index = fopen('index.txt', 'r'); while (fscanf(fp_index, '%s', words[word_num]) != EOF) { ++word_num; } fclose(fp_index); // 对单词进行排序 qsort(words, word_num, sizeof(words[0]), cmp); // 读取英文文章 char line[MAX_LINE_LEN + 1]; FILE *fp_input = fopen('in.txt', 'r'); FILE *fp_output = fopen('error.txt', 'w'); while (fgets(line, MAX_LINE_LEN + 1, fp_input) != NULL) { int len = strlen(line); if (line[len - 1] == '\n') { line[len - 1] = '\0'; --len; } char *p = line; while (*p) { // 跳过非字母字符 while (*p && !isalpha(*p)) { ++p; } if (*p) { // 记录单词的起始位置 char *start = p; // 跳过字母字符 while (*p && isalpha(*p)) { ++p; } // 记录单词的结束位置 char *end = p; // 将单词转换为小写 for (char *q = start; q < end; ++q) { *q = tolower(*q); } // 在单词索引表中查找单词 if (word_num == 0 || strcmp(words[0], start) > 0) { fprintf(fp_output, '%s\n', start); } else { int left = 0, right = word_num - 1; while (left <= right) { int mid = (left + right) / 2; int cmp_res = strcmp(words[mid], start); if (cmp_res == 0) { break; } else if (cmp_res < 0) { left = mid + 1; } else { right = mid - 1; } } if (left > right) { fprintf(fp_output, '%s\n', start); } } } } } fclose(fp_input); fclose(fp_output); return 0

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

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

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