做链表题时出现的结果跟目标结果不同
#include
#include
#include
#define n 2
struct book //建立结构体数组
{
char zhanghao[8];
char num[11];
char suoyinhao[6];
char time[10];
char returntime[10];
}a[n],b[n],c[n];
struct node //建立结构体链表
{
char zhanghao[8];
char num[11];
char suoyinhao[6];
char time[10];
char returntime[10];
struct node *next;
};
FILE *fp1,*fp2,*fp3;
struct node *insertone(struct node *head1) /*输入a.txt中所有的信息,并构建新的带头结点的链表*/
{
int i;
struct node *p,*q;
head1=q=(struct node *)malloc(sizeof(struct node));
printf(" ~~~~~~欢迎进入借还书信息系统,请输入相关信息~~~~~~\n");
for(i=0;i"账号:");
scanf("%s",a[i].zhanghao);
printf("学号:");
scanf("%s",a[i].num);
printf("图书索引号:");
scanf("%s",a[i].suoyinhao);
printf("借书时间:");
scanf("%s",a[i].time);
printf("\n");
q->next=p;
q=p;
}
p->next=0;
return head1;
}
struct node *inserttwo(struct node *head2) /*输入b.txt中所有的信息,并构建新的带头结点的链表*/
{
int i;
struct node *p,*q;
head2=q=(struct node *)malloc(sizeof(struct node));
for(i=0;i"账号:");
scanf("%s",b[i].zhanghao);
printf("学号:");
scanf("%s",b[i].num);
printf("图书索引号:");
scanf("%s",b[i].suoyinhao);
printf("还书时间:");
scanf("%s",b[i].returntime);
printf("\n");
q->next=p;
q=p;
}
p->next=0;
return head2;
}
void showone(struct node *head1) /*保存到a.txt文件中并显示出来*/
{
int i;
struct node *temp;
temp=head1;
printf("a.txt\n");
fp1=fopen("a.txt","w");
for(i=0;iif(fwrite(&a[i],sizeof(struct book),1,fp1)!=1) /*将结构体数组a[i]中的数据写入a.txt文件*/
printf("文件写入数据错误\n");
fclose(fp1);
fp1=fopen("a.txt","r");
printf("\n账号 学号 图书索引号 借书时间\n");
for(i=0;i1,fp1); /*从a.txt文件中读出数据并打印出来*/
printf("%s %11s %8s %15s\n",a[i].zhanghao,a[i].num,a[i].suoyinhao,a[i].time);
temp=temp->next;
}
fclose(fp1);
}
void showtwo(struct node *head2) /*保存到b.txt文件中并显示出来*/
{
int i;
struct node *temp;
temp=head2;
printf("b.txt\n");
fp2=fopen("b.txt","w");
for(i=0;iif(fwrite(&b[i],sizeof(struct book),1,fp2)!=1) /*将结构体数组b[i]中的数据写入b.txt文件*/
printf("文件写入数据错误\n");
fclose(fp2);
fp2=fopen("b.txt","r");
printf("\n账号 学号 图书索引号 还书时间\n");
for(i=0;i1,fp2); /*从a.txt文件中读出数据并打印出来*/
printf("%s %11s %8s %15s\n",b[i].zhanghao,b[i].num,b[i].suoyinhao,b[i].returntime);
temp=temp->next;
}
fclose(fp2);
}
struct node *loadone(void) /*将文件a中的数据生成一个链表,如果文件不存在,则是一个空链*/
{
int i;
struct node *head1, *p,*q;
struct book a[n];
head1=(struct node *)malloc(sizeof(struct node));
q=head1=NULL;
if((fp1=fopen("a.txt","rb"))==NULL)
return head1;
else
{while(!feof(fp1))
{
for(i=0;iif(fread(&a[i],sizeof(struct book),1,fp1)==1)
{p=(struct node *)malloc(sizeof(struct node));
strcpy(p->zhanghao,a[i].zhanghao);
strcpy(p->num,a[i].num);
strcpy(p->suoyinhao,a[i].suoyinhao);
strcpy(p->time,a[i].time);
head1=p;
p->next=q;
q=head1;
}
}
fclose(fp1);
return head1;
}
}
struct node *loadtwo(void) /*将文件b中的数据生成一个链表,如果文件不存在,则是一个空链*/
{
int i;
struct node *head2, *p,*q;
struct book b[n];
head2=(struct node *)malloc(sizeof(struct node));
q=head2=NULL;
if((fp2=fopen("b.txt","rb"))==NULL)
return head2;
else
{while(!feof(fp2))
{
for(i=0;iif(fread(&b[i],sizeof(struct book),1,fp2)==1)
{p=(struct node *)malloc(sizeof(struct node));
strcpy(p->zhanghao,b[i].zhanghao);
strcpy(p->num,b[i].num);
strcpy(p->suoyinhao,b[i].suoyinhao);
strcpy(p->returntime,b[i].returntime);
head2=p;
p->next=q;
q=head2;
}
}
fclose(fp2);
return head2;
}
}
void showthree(struct node *head1, struct node *head2)
{
int i;
struct node *temp1, *temp2;
temp1 = head1;
temp2 = head2;
// Open "c.txt" for writing
fp3 = fopen("c.txt", "w");
if (fp3 == NULL)
{
printf("Error opening file c.txt!\n");
exit(1);
}
// Combine information from "a.txt" and "b.txt" and save to "c.txt"
while (temp1 != NULL && temp2 != NULL)
{
if (strcmp(temp1->suoyinhao, temp2->suoyinhao) < 0)
{
// Save information from "a.txt" to "c.txt"
if (fwrite(temp1, sizeof(struct node), 1, fp3) != 1)
{
printf("Error writing to file c.txt!\n");
exit(1);
}
temp1 = temp1->next;
}
else if (strcmp(temp1->suoyinhao, temp2->suoyinhao) > 0)
{
// Save information from "b.txt" to "c.txt"
if (fwrite(temp2, sizeof(struct node), 1, fp3) != 1)
{
printf("Error writing to file c.txt!\n");
exit(1);
}
temp2 = temp2->next;
}
else
{
// Save combined information from "a.txt" and "b.txt" to "c.txt"
strcpy(temp1->returntime, temp2->returntime);
if (fwrite(temp1, sizeof(struct node), 1, fp3) != 1)
{
printf("Error writing to file c.txt!\n");
exit(1);
}
temp1 = temp1->next;
temp2 = temp2->next;
}
}
// Save remaining information from "a.txt" or "b.txt" to "c.txt"
while (temp1 != NULL)
{
if (fwrite(temp1, sizeof(struct node), 1, fp3) != 1)
{
printf("Error writing to file c.txt!\n");
exit(1);
}
temp1 = temp1->next;
}
while (temp2 != NULL)
{
if (fwrite(temp2, sizeof(struct node), 1, fp3) != 1)
{
printf("Error writing to file c.txt!\n");
exit(1);
}
temp2 = temp2->next;
}
// Close "c.txt"
fclose(fp3);
// Open "c.txt" for reading
fp3 = fopen("c.txt", "r");
if (fp3 == NULL)
{
printf("Error opening file c.txt!\n");
exit(1);
}
// Display information from "c.txt" on screen
printf("c.txt\n");
printf("\n账号 学号 图书索引号 借书时间 还书时间\n");
while (fread(&c[i], sizeof(struct book), 1, fp3) == 1)
{
printf("%s %11s %8s %15s\n",c[i].zhanghao,c[i].num,c[i].suoyinhao,c[i].time,c[i].returntime);
i++;
}
// Close "c.txt"
fclose(fp3);
}
main()
{
struct node *head;
struct node *head1;
struct node *head2;
head1=insertone(head1);
head2=inserttwo(head2);
showone(head1);
showtwo(head2);
head1=loadone();
head2=loadtwo();
showthree(head1,head2);
}
如果你用最简单的输入来测试,你的输出也是错误的么? 错误在哪里呢? 可以从最简单的地方入手。