借还书信息汇总 课程设计报告.docx
《借还书信息汇总 课程设计报告.docx》由会员分享,可在线阅读,更多相关《借还书信息汇总 课程设计报告.docx(25页珍藏版)》请在冰豆网上搜索。
借还书信息汇总课程设计报告
课程设计
课程程序设计基础
题目借还书信息汇总
院系名称计算机学院
班级计算机科学与技术5班
学生姓名学号
组员
指导教师
时间2012年2月24日
1问题要求及任务描述
1.1题目要求
20借还书信息汇总
问题描述:
设有借还书记录文件a.txt,b.txt
结构如下:
a.txt
账号
学号
图书索引号
借书时间
123456
2008001001
TP345
2009-1-1
2423
2010330002
O3.6
2010-2-3
b.txt
账号
学号
图书索引号
还书时间
123456
2008001001
TP345
2009-3-1
2423
2010330002
O3.6
2010-3-20
要求:
[1]从a.txt和b.txt中读取相关信息存储到两个链表中
[2]以账号、学号、图书索引号为关联合并两个文件,合并后格式如下
账号
学号
图书索引号
借书时间
还书时间
123456
2008001001
TP345
2009-1-1
2009-3-1
2423
2010330002
O3.6
2010-2-3
2010-3-20
存储到链表中
[3]将[2]生成的链表数据存储到文件中
注意:
1、a.txt文件中存在与b.txt不匹配项,要求忽略
2、b.txt文件中存在与a.txt不匹配项,要求忽略
1.2主要任务
主要功能:
对借还书信息进行汇总、处理。
将输入两个文件中的相关信息存储到两个相对应的链表中。
合并两个文件的信息,并将信息存储到另一个文件中。
对文件建立链表,使链表中的信息为账号、学好、、图书索引号、借书时间、还书时间。
将生成的链表数据存储到文件中。
2解决问题的主要思路和方法
2.1关键问题
合并两个文件。
对文件建立链表,并将生成的链表数据存储到文件中。
2.2拟采用解决问题的方法
将文件a.txt和b.txt中的信息复制到文件c.txt。
然后对文件c.txt建立链表,保留账号、学好、、图书索引号、借书时间、还书时间。
借还书信息结构体:
structbook
{
charzhanghao[8];
charnum[11];
charsuoyinhao[6];
chartime[10];
charreturntime[10];
}a[n],b[n],c[n];
structnode
{
charzhanghao[8];
charnum[11];
charsuoyinhao[6];
chartime[10];
charreturntime[10];
structnode*next;
};
调用函数结构:
main()
{
head1=insertone(head1);
head2=inserttwo(head2);
showone(head1);
showtwo(head2);
…………
}
子函数:
structnode*insertone(structnode*head1)
structnode*inserttwo(structnode*head2)
…………
2.3主要算法和处理流程图
主要算法:
主函数:
structnode*head;structnode*head1;structnode*head2;
head1=insertone(head1);
head2=inserttwo(head2);
showone(head1);showtwo(head2);
head1=loadone();head2=loadtwo();
hebing(head1,head2);head=loadthree();
showthree(head);
部分子函数:
structnode*insertone(structnode*head1)
inti;structnode*p,*q;
head1=q=(structnode*)malloc(sizeof(structnode));
printf("~~~~~~欢迎进入借还书信息系统,请输入相关信息~~~~~~\n");
for(i=0;ip=(structnode*)malloc(sizeof(structnode));
printf("账号:
");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;returnhead1;
structnode*inserttwo(structnode*head2)
inti;structnode*p,*q;
head2=q=(structnode*)malloc(sizeof(structnode));
for(i=0;ip=(structnode*)malloc(sizeof(structnode));
printf("账号:
");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;returnhead2;
voidshowone(structnode*head1)
inti;structnode*temp;temp=head1;
printf("a.txt\n");fp1=fopen("a.txt","w");
for(i=0;iif(fwrite(&a[i],sizeof(structbook),1,fp1)!
=1)
NY
printf("文件写入数据错误\n");
fclose(fp1);
fp1=fopen("a.txt","r");
printf("\n账号学号图书索引号借书时间\n");
for(i=0;ifread(&a[i],sizeof(structbook),1,fp1);
printf("%s%11s%8s%15s\n",a[i].zhanghao,a[i].num,a[i].suoyinhao,a[i].time);
temp=temp->next;
fclose(fp1);
voidshowtwo(structnode*head2)
inti;structnode*temp;temp=head2;
printf("b.txt\n");fp2=fopen("b.txt","w");
for(i=0;iif(fwrite(&b[i],sizeof(structbook),1,fp2)!
=1)
NY
printf("文件写入数据错误\n");
fclose(fp2);
fp2=fopen("b.txt","r");
printf("\n账号学号图书索引号还书时间\n");
for(i=0;ifread(&b[i],sizeof(structbook),1,fp2);printf("%s%11s%8s%15s\n",b[i].zhanghao,b[i].num,b[i].suoyinhao,b[i].returntime);
temp=temp->next;
fclose(fp2);
structnode*loadone(void)
inti;structnode*head1,*p,*q;structbooka[n];
head1=(structnode*)malloc(sizeof(structnode));
q=head1=NULL;
if((fp1=fopen("a.txt","rb"))==NULL)
NY
Nwhile(!
feof(fp1))Yreturnhead1;
for(i=0;iif(fread(&a[i],sizeof(structbook),1,fp1)==1)
NY
p=(structnode*)malloc
(sizeof(structnode));
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);returnhead1;
3程序实现
3.1程序实现时应考虑的问题
;
3.2主要源代码及说明
#include
#include
#definen2
structbook//建立结构体数组
{
charzhanghao[8];
charnum[11];
charsuoyinhao[6];
chartime[10];
charreturntime[10];
}a[n],b[n],c[n];
structnode//建立结构体链表
{
charzhanghao[8];
charnum[11];
charsuoyinhao[6];
chartime[10];
charreturntime[10];
structnode*next;
};
FILE*fp1,*fp2,*fp3;
structnode*insertone(structnode*head1)/*输入a.txt中所有的信息,并构建新的带头结点的链表*/
{
inti;
structnode*p,*q;
head1=q=(structnode*)malloc(sizeof(structnode));
printf("~~~~~~欢迎进入借还书信息系统,请输入相关信息~~~~~~\n");
for(i=0;i{
p=(structnode*)malloc(sizeof(structnode));
printf("账号:
");
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;
returnhead1;
}
structnode*inserttwo(structnode*head2)/*输入b.txt中所有的信息,并构建新的带头结点的链表*/
{
inti;
structnode*p,*q;
head2=q=(structnode*)malloc(sizeof(structnode));
for(i=0;i{
p=(structnode*)malloc(sizeof(structnode));
printf("账号:
");
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;
returnhead2;
}
voidshowone(structnode*head1)/*保存到a.txt文件中并显示出来*/
{
inti;
structnode*temp;
temp=head1;
printf("a.txt\n");
fp1=fopen("a.txt","w");
for(i=0;iif(fwrite(&a[i],sizeof(structbook),1,fp1)!
=1)/*将结构体数组a[i]中的数据写入a.txt文件*/
printf("文件写入数据错误\n");
fclose(fp1);
fp1=fopen("a.txt","r");
printf("\n账号学号图书索引号借书时间\n");
for(i=0;i{
fread(&a[i],sizeof(structbook),1,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);
}
voidshowtwo(structnode*head2)/*保存到b.txt文件中并显示出来*/
{
inti;
structnode*temp;
temp=head2;
printf("b.txt\n");
fp2=fopen("b.txt","w");
for(i=0;iif(fwrite(&b[i],sizeof(structbook),1,fp2)!
=1)/*将结构体数组b[i]中的数据写入b.txt文件*/
printf("文件写入数据错误\n");
fclose(fp2);
fp2=fopen("b.txt","r");
printf("\n账号学号图书索引号还书时间\n");
for(i=0;i{
fread(&b[i],sizeof(structbook),1,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);
}
structnode*loadone(void)/*将文件a中的数据生成一个链表,如果文件不存在,则是一个空链*/
{
inti;
structnode*head1,*p,*q;
structbooka[n];
head1=(structnode*)malloc(sizeof(structnode));
q=head1=NULL;
if((fp1=fopen("a.txt","rb"))==NULL)
returnhead1;
else
{while(!
feof(fp1))
{
for(i=0;iif(fread(&a[i],sizeof(structbook),1,fp1)==1)
{p=(structnode*)malloc(sizeof(structnode));
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);
returnhead1;
}
}
structnode*loadtwo(void)/*将文件b中的数据生成一个链表,如果文件不存在,则是一个空链*/
{
inti;
structnode*head2,*p,*q;
structbookb[n];
head2=(structnode*)malloc(sizeof(structnode));
q=head2=NULL;
if((fp2=fopen("b.txt","rb"))==NULL)
returnhead2;
else
{while(!
feof(fp2))
{
for(i=0;iif(fread(&b[i],sizeof(structbook),1,fp2)==1)
{p=(structnode*)malloc(sizeof(structnode));
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);
returnhead2;
}
}
voidhebing(structnode*head1,structnode*head2)/*合并两个文件*/
{
charch;
FILE*fp1,*fp2,*fp3;
if((fp1=fopen("a.txt","r"))==NULL)
{printf("打开文件a.txt失败!
\n");
exit
(1);
}
else
printf("打开文件a.txt成功!
\n");
if((fp2=fopen("b.txt","r"))==NULL)
{printf("打开文件b.txt失败!
\n");
exit
(2);
}
else
printf("打开文件b.txt成功!
\n");
if((fp3=fopen("c.txt","r"))==NULL)
{printf("打开文件c.txt失败!
\n");
exit(3);
}
else
printf("打开文件c.txt成功!
\n");
while((ch=fgetc(fp1))!
=EOF)
fputc(ch,fp3);
while((ch=fgetc(fp2))!
=EOF)
fputc(ch,fp3);
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
structnode*loadthree(void)/*将文件c中的数据生成一个链表,如果文件不存在,则是一个空链*/
{
inti;
structnode*head,*p,*q;
structbookc[n];
head=(structnode*)malloc(sizeof(structnode));
q=head=NULL;
if((fp3=fopen("c.txt[]","rb"))==NULL)
returnhead;
else
{while(!
feof(fp3))
{
for(i=0;iif(fread(&c[i],sizeof(structbook),1,fp3)==1)
{p=(structnode*)malloc(sizeof(structnode));
strcpy(c[i].zhanghao,p->zhanghao);
strcpy(c[i].num,p->num);
strcpy(c[i].suoyinhao,p->suoyinhao);
strcpy(c[i].time,p->time);
strcpy(c[i].returntime,p->returntime);
head=p;
p->next=q;
q=head;
}
}
fclose(fp3);
returnhead;
}
}
voidshowthree(structnode*head)/*保存c.txt文件中的信息并显示出来*/
{
inti;
structnode*p;
p=head;
printf("c.txt\n");
fp3=fopen("c.txt","w");
for(i=0;iif(fwrite(&c[i],sizeof(structbook),1,fp3)!
=1)
printf("文件写入数据错误\n");
fclose(fp3);
fp3=fopen("c.txt","r");
printf("\n账号学号图书索引号借书时间还书时间\n");
for(i=0;i{
fread(&c[i],sizeof(structbook),1,fp3);
printf("%s%11s%8s%15s%15s\n",c[i].zhanghao,c[i].num,c[i].suoyinhao,c[i].time,c[i].returntime);
p=p->next;
}
fclose(fp3);
}
main()
{
structnode*head;
structnode*head1;
structnode*head2;
head1=insertone(head1);//输入a.txt文件中的信息