C语言实现英文文章单词拼写检查
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LEN 50
#define MAX_WORDS_NUM 1000
// 读取单词索引表,返回单词个数
int readIndex(char indexFile[], char indexTable[][MAX_WORD_LEN+1]) {
FILE *fp;
int num = 0;
char word[MAX_WORD_LEN+1];
fp = fopen(indexFile, 'r');
if (fp == NULL) {
printf('Error: cannot open file %s\n', indexFile);
exit(1);
}
while (fgets(word, MAX_WORD_LEN+1, fp) != NULL) {
word[strcspn(word, '\r\n')] = '\0'; // 去掉行末的换行符
strcpy(indexTable[num], word);
num++;
}
fclose(fp);
return num;
}
// 判断一个字符是否为字母
int isLetter(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
// 将一个字符串转换为小写
void toLower(char str[]) {
int i;
for (i = 0; str[i] != '\0'; i++) {
str[i] = tolower(str[i]);
}
}
// 读取英文文章,返回单词个数
int readArticle(char articleFile[], char article[][MAX_WORD_LEN+1]) {
FILE *fp;
int num = 0;
char c, word[MAX_WORD_LEN+1];
int i, j, len;
fp = fopen(articleFile, 'r');
if (fp == NULL) {
printf('Error: cannot open file %s\n', articleFile);
exit(1);
}
i = 0;
while ((c = fgetc(fp)) != EOF) {
if (isLetter(c)) {
word[i] = c;
i++;
} else if (i > 0) {
word[i] = '\0';
toLower(word);
strcpy(article[num], word);
num++;
i = 0;
}
}
if (i > 0) { // 处理最后一个单词
word[i] = '\0';
toLower(word);
strcpy(article[num], word);
num++;
}
fclose(fp);
return num;
}
// 比较两个字符串的大小,返回值为0表示相等,小于0表示s1<s2,大于0表示s1>s2
int cmpString(const void *s1, const void *s2) {
return strcmp(*(const char **)s1, *(const char **)s2);
}
// 查找单词在单词索引表中的位置,返回值为-1表示未找到
int searchWord(char word[], char indexTable[][MAX_WORD_LEN+1], int num) {
int left = 0, right = num - 1, mid;
while (left <= right) {
mid = (left + right) / 2;
if (strcmp(word, indexTable[mid]) == 0) {
return mid;
} else if (strcmp(word, indexTable[mid]) < 0) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
}
int main() {
char indexFile[] = 'index.txt';
char articleFile[] = 'in.txt';
char errorFile[] = 'error.txt';
char indexTable[MAX_WORDS_NUM][MAX_WORD_LEN+1];
char article[MAX_WORDS_NUM][MAX_WORD_LEN+1];
char errorWords[MAX_WORDS_NUM][MAX_WORD_LEN+1];
int indexNum, articleNum, errorNum = 0;
int i, pos;
// 读取单词索引表
indexNum = readIndex(indexFile, indexTable);
// 读取英文文章
articleNum = readArticle(articleFile, article);
// 检查单词是否在单词索引表中出现
for (i = 0; i < articleNum; i++) {
pos = searchWord(article[i], indexTable, indexNum);
if (pos == -1) {
strcpy(errorWords[errorNum], article[i]);
errorNum++;
}
}
// 对错误单词按字典序排序
qsort(errorWords, errorNum, sizeof(errorWords[0]), cmpString);
// 输出错误单词到文件
FILE *fp;
fp = fopen(errorFile, 'w');
if (fp == NULL) {
printf('Error: cannot open file %s\n', errorFile);
exit(1);
}
for (i = 0; i < errorNum; i++) {
fprintf(fp, '%s\n', errorWords[i]);
}
fclose(fp);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/gmJ6 著作权归作者所有。请勿转载和采集!