C语言问题,求大神解答
#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;
}
} 展开
//Node* head表示头指针
//头指针指向头结点,头结点的值无用,头结点的指针指向链表内第一个元素
//当NULL == head->next时链表为空,当NULL == head时链表为无效链表
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int val;
struct Node* next;
}Node;
void* _malloc(size_t size) {
void* res = malloc(size);
if (NULL == res) {
printf("内存不足,程序正在退出\n");
exit(1);
}
return res;
}
void Append(Node* node, int val) {
Node* next = node->next;
node->next = _malloc(sizeof(Node));
node->next->val = val;
node->next->next = next;
}
void SortInsert(Node* head, int val) {
if (NULL != head) {
Node* last;
for (;;) {
last = head;
head = head->next;
if (NULL == head || head->val >= val) {
break;
}
}
Append(last, val);
}
}
void EraseP(Node* last) {
if (NULL != last && NULL != last->next) {
Node* next = last->next->next;
free(last->next);
last->next = next;
}
}
void Erase(Node* head, int n) {
while (n-- > 0 && NULL != head) {
head = head->next;
}
if (NULL != head) {
EraseP(head);
}
}
void ShowAll(Node* head) {
if (NULL != head) {
while (NULL != (head = head->next)) {
printf("%d -> ", head->val);
}
printf("\n");
}
}
int main(int argc, char* argv[]) {
Node* head = _malloc(sizeof(Node));
head->next = NULL;
SortInsert(head, 4); ShowAll(head);
SortInsert(head, 0); ShowAll(head);
SortInsert(head, 1); ShowAll(head);
SortInsert(head, 3); ShowAll(head);
SortInsert(head, 2); ShowAll(head);
printf("\n");
Erase(head, 3); ShowAll(head);
Erase(head, 3); ShowAll(head);
Erase(head, 0); ShowAll(head);
Erase(head, 1); ShowAll(head);
Erase(head, 0); ShowAll(head);
return 0;
}
大致看了一下,觉得总体思路可行,但具体操作就有问题了。一是feof这个函数的引用就有问题:这个函数不读文件只检查刚刚发生的读操作是否卖到或越过了文件结束符;代码中第一次进入while时还没有发生读动作,无法确定的判断会把错误依次转嫁到文件最后。二是用fgets这个函数读文件是由n或'\n'控制结束的,中间的空格也会被正确读取,而单词是作空格或'\n'隔开的,所以就不会一个单词一个单词地正确读出来。我试着写一个供你参考——
#include "stdio.h"
#include <string.h>
int main(int argc,char *argv[]){
FILE *fp1,*fp2,*fp3;
char w1[21],w2[21],w3[21];//考虑到英文单词不会超过20个字母
int e12,e13;
fp1=fopen("dictionary1.txt","r");
fp2=fopen("dictionary2.txt","r");
if(!fp1 || !fp2 || !(fp3=fopen("dictionary3.txt","r"))){
printf("Failed to open the file(s) and exit...\n");
return 0;
}
while(fscanf(fp1,"%s",w1)==1){//==1这种判断代替了feof,把==1改成',!feof(fp1)'也行
while(fscanf(fp2,"%s",w2)==1)
if((e12=strcmp(w1,w2))<0){
rewind(fp2);
break;
}
else if(e12==0){
while(fscanf(fp3,"%s",w3)==1)
if((e13=strcmp(w1,w3))<0){
rewind(fp3);
break;
}
else if(e13==0){
printf("The word is '%s'\n",w1);
return 0;
}
else if(feof(fp3)){//这个判断区别没找到时文件是否结束
printf("In the 3 dictionaries there is no the same words.\n");
return 0;
}
}
else if(feof(fp2)){//这个判断区别没找到时文件是否结束
printf("In the 3 dictionaries there is no the same words.\n");
return 0;
}
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
printf("In the 3 dictionaries there is no the same words.\n");
return 0;
}
已经充分验证过。查找速度可能还有提高余地。不认为算法很好,有问题续问。