到目前为止我在C中编写一个字典程序我的程序打开一个txt文件(字典),然后打开另一个要检查拼写的txt文件,它将文档中的每个单词与字典文件中的每个单词进行比较打印出文档拼写错误的第一个单词,但是,永远不会回到内部while循环我不知道如何解决这个问题
这是我的代码的最后一部分请帮助!
//compare document to dictionary
char dictword[300];
char misword[300];
FILE *fp1, *fp2;
fp2 = fopen("dictionary.txt","r"); //open dictionary
fp1 = fopen("mispelled.txt","r"); //open document to be checked for spelling
while (test != EOF)//run loop until every misword file has been checked
{
test = fscanf(fp1,"%s",misword); //gets one word from the dictionary file
while (test2 != EOF)//run loop until every word in dict file has been checked
{
test2 = fscanf(fp2,"%s",dictword); //gets one word from the dictionary file
if (0==strcmp(misword,dictword))//if misword is = to dictword
break; //if a word in the misword file matches the dictionary word it must be spelled correct, break out of loop
else if(test2 == EOF)//if one misword is compared with every dictword misword must not be a word
printf("%s This word is not found in the dictionary \n",misword); //thus print it
}
} //while (test != EOF);//run loop until every misword file has been checked
fclose(fp2);
fclose(fp1);
答案 0 :(得分:1)
看起来你的内循环中的逻辑是错误的。您需要迭代字典中的每个单词以查看拼写是否拼写错误。一旦你在字典中找到了这个单词,就可以像你一样打破内循环。但是,在你突破内循环之前,你应该设置一个标志来表明你找到了这个词。这样,在完成内部循环后,检查标志以查看是否找到了该单词。另请注意,此实现将非常缓慢。程序通常会将字典放入哈希表中,以使查找更有效。