设有3个按词典编辑顺序组织的单词文件,编写从这3个文件中找出第1个在这3个文件中都出现的单词,要求采用算法最快。
我的代码无论在三个文件中输什么单词运行出来都是下图这样,调试也没法逐步进行。
代码如下,求大神看看哪个地方有问题。
#include<stdio.h>
#include<string.h>
#define n 100
int main()
{
FILE *fp1,*fp2,*fp3;
char str1[n],str2[n],str3[n];
if((fp1=fopen("dictionary1.txt","r"))==NULL){
printf("不能打开文件dictionary1.txt\n");
return 0;}
while(!feof(fp1)){
fgets(str1,n,fp1);
if((fp2=fopen("dictionary2.txt","r"))==NULL){
printf("不能打开文件dictionary2.txt\n");
return 0;}
while(!feof(fp2)){
fgets(str2,n,fp2);
if(strcmp(str1,str2)==0){
if((fp3=fopen("dictionary3.txt","r"))==NULL){
printf("不能打开文件dictionary3.txt\n");
return 0;}
while(!feof(fp3)){
fgets(str3,n,fp3);
if(strcmp(str2,str3)==0){
printf("在三个词典中同时出现的第一个词为%s\n",str1);
return 1;}
else continue;}
printf("三个词典中无相同的词\n");
return 0;}
else continue;}
printf("三个词典中无相同的词\n");
return 0;
}
}