1

嗨,我正在用 c 语言制作一个拼写检查器,它在字符串数组中有一个字典,并使用二进制搜索在字典中查找单词。

我的问题是我正在尝试从文件中读取文本并将文本输出回新文件,其中错误的单词突出显示如下:** 拼写错误 ** 但该文件将包含诸如 .,!? 它应该输出到新文件,但在将单词与字典进行比较时显然不存在。

所以我想要这个:

text file: "worng!"

new file: "** worng **!"

我一直在尽我所能解决这个问题,并且在谷歌上花了很长时间,但离解决方案还差得远。到目前为止,我已经编写了以下代码来读取每个字符并填充两个 char 数组一个小写临时用于字典比较,一个输入用于原始单词,如果没有标点符号则可以使用,但显然当标点符号存在时我会以这种方式释放空间我'确定有更好的方法可以做到这一点,但我找不到它,所以任何指针都会受到赞赏。

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

#define MAX_STRING_SIZE 29  /*define longest non-technical word in english dictionary plus 1*/

/*function prototypes*/
int dictWordCount(FILE *ptrF);  /*counts and returns number of words in dictionary*/
void loadDictionary(char ***pArray1, FILE *ptrFile, int counter);   /*create dictionary array from file based on word count*/
void printDictionary(char **pArray2, int size); /*prints the words in the dictionary*/
int binarySearch(char **pArray3, int low, int high, char *value);   /*recursive binary search on char array*/

void main(int argc, char *argv[]){
    int i;  /*index*/
    FILE *pFile;    /*pointer to dictionary file*/
    FILE *pInFile;  /*pointer to text input file*/
    FILE *pOutFile; /*pointer to text output file*/
    char **dict;    /*pointer to array of char pointer - dictionary*/
    int count;      /*number of words in dictionary*/
    int dictElement;    /*element the word has been found at returns -1 if word not found*/

    char input[MAX_STRING_SIZE];    /*input to find in dictionary*/
    char temp[MAX_STRING_SIZE];
    char ch;    /*store each char as read - checking for punctuation or space*/
    int numChar = 0; /*number of char in input string*/

    /*************************************************************************************************/
    /*open dictionary file*/
    pFile = fopen("dictionary.txt", "r");   /*open file dictionary.txt for reading*/
    if(pFile==NULL){    /*if file can't be opened*/
        printf("ERROR: File could not be opened!/n");
        exit(EXIT_FAILURE);
    }

    count = dictWordCount(pFile);
    printf("Number of words is: %d\n", count);

    /*Load Dictionary into array*/
    loadDictionary(&dict, pFile, count);

    /*print dictionary*/
    //printDictionary(dict, count);
    /*************************************************************************************************/
    /*open input file for reading*/
    pInFile = fopen(argv[1], "r");
    if(pInFile==NULL){  /*if file can't be opened*/
        printf("ERROR: File %s could not be opened!/n", argv[1]);
        exit(EXIT_FAILURE);
    }
    /*open output file for writing*/
    pOutFile = fopen(argv[2], "w");
    if(pOutFile==NULL){ /*if file can't be opened*/
        printf("ERROR: File could not be created!/n");
        exit(EXIT_FAILURE);
    }

    do{
        ch = fgetc(pInFile);                /*read char fom file*/

        if(isalpha((unsigned char)ch)){     /*if char is alphabetical char*/
            //printf("char is: %c\n", ch);
            input[numChar] = ch;            /*put char into input array*/
            temp[numChar] = tolower(ch);    /*put char in temp in lowercase for dictionary check*/
            numChar++;                      /*increment char array element counter*/
        }
        else{
            if(numChar != 0){
                input[numChar] = '\0';  /*add end of string char*/
                temp[numChar] = '\0';

                dictElement = binarySearch(dict,0,count-1,temp);    /*check if word is in dictionary*/

                if(dictElement == -1){  /*word not in dictionary*/
                    fprintf(pOutFile,"**%s**%c", input, ch);
                }
                else{   /*word is in dictionary*/
                    fprintf(pOutFile, "%s%c", input, ch);
                }
                numChar = 0;    /*reset numChar for next word*/
            }
        }
    }while(ch != EOF);

    /*******************************************************************************************/
    /*free allocated memory*/
    for(i=0;i<count;i++){
        free(dict[i]);
    }
    free(dict);

    /*close files*/
    fclose(pInFile);
    fclose(pOutFile);

}
4

2 回答 2

1

我不是 100% 确定我是否正确理解了您的问题,但我会试一试。

首先,你的循环

do{
    ch = fgetc(pInFile);
    /* do stuff */
}while(ch != EOF);

当到达文件末尾时也会运行,因此如果文件的最后一个字节是按字母顺序排列的,您将EOF在输出文件中打印一个不需要的字节,或者,因为您在将其传递chunsigned char时强制转换为isalpha(),这通常会导致255 [for EOF = -1and 8 bit unsigned char],它将在某些语言环境(例如 en_US.iso885915)中被视为字母字符,这会导致抑制输入文件的最后一个单词。

ch为了解决这个问题,首先,在传递给 时不要强制转换isalpha(),其次在循环中添加一些逻辑以防止无意处理EOF. 如果需要,我选择用换行符替换它,因为这很简单。

然后它仍然打印出不立即跟随字母字符的非字母字符:

do{
    ch = fgetc(pInFile);                /*read char fom file*/

    if(isalpha(ch)){                    /*if char is alphabetical char*/
        //printf("char is: %c\n", ch);
        input[numChar] = ch;            /*put char into input array*/
        temp[numChar] = tolower(ch);    /*put char in temp in lowercase for dictionary check*/
        numChar++;                      /*increment char array element counter*/
    }
    else{
        if(numChar != 0){
            input[numChar] = '\0';  /*add end of string char*/
            temp[numChar] = '\0';

            dictElement = binarySearch(dict,0,count-1,temp);    /*check if word is in dictionary*/

            if(dictElement == -1){  /*word not in dictionary*/
                fprintf(pOutFile,"**%s**%c", input, (ch == EOF) ? '\n' : ch);
            }
            else{   /*word is in dictionary*/
                fprintf(pOutFile, "%s%c", input, (ch == EOF) ? '\n' : ch);
            }
            numChar = 0;    /*reset numChar for next word*/
        }
        else
        {
            if (ch != EOF) {
                fprintf(pOutFile, "%c",ch);
            }
        }
    }
}while(ch != EOF);
于 2012-04-04T21:36:32.243 回答
0

现在看起来,如果 char 不是按字母顺序排列的,它会触发else块,if(isalpha((unsigned char)ch)){并且字符本身会被忽略。

如果您添加一条语句以完全按照输入的方式打印所有非字母字符,我认为这可以实现您想要的。这需要进入该else块内部和块之后,if(numChar != 0){并且只是一个简单的 fprintf 语句。

于 2012-04-04T21:21:47.180 回答