1、#include#include#includestruct bookint num;char bname50;char wname20;char press50;char sort50;int time;float price;struct book *next;struct book *creatbook(); /创建链表struct book *addbook(struct book *head); /添加图书int yanzheng(struct book *head,int m); /验证新添加的图书编码是否已存在void deletebook(struct book *head);
2、 /删除图书void fprint(struct book *head); /将链表写入文件struct book *load(); /从文件中读取信息并建成链表void print_book(struct book *head); /将链表信息输出void chaxun(struct book *head); /查询图书信息void num_chaxun(struct book *head); /按图书编号查询图书void wname_chaxun(struct book *head); /按作者名查询图书void sort_chaxun(struct book *head); /按类别查询
3、图书void time_chaxun(struct book *head); /按出版时间查询图书void bname_chaxun(struct book *head); /按图书名查询图书void xiugai(struct book *head); /修改图书信息void paixu(struct book *head); /对图书进行排序void num_paixu(struct book *head); /按图书编号排序void time_paixu(struct book *head); /按图书出版时间排序void price_paixu(struct book *head);
4、/按图书价格排序void bname_paixu(struct book *head); /按图书名排序void wname_paixu(struct book *head); /按作者名排序int main()int choice,n,x,y=1,c,c1=1234;char a,d,b10,b110=“yjk“;struct book *head=NULL;while(y)system(“cls“);printf(“nnnnnnn“);printf(“ * 欢迎光临 *nn“);printf(“ * 图书信息管理系统 *nnn“);printf(“nn“);printf(“ =1-用户登录
5、=n“);printf(“ =0-退出系统=n“);printf(“ 请输入您的选择:“);scanf(“%d“,printf(“n“);getchar();switch(n)case 0:y=0;break;case 1:printf(“ 请输入您的用户名:“);gets(b);printf(“n“);printf(“ 请输入您的密码:“);scanf(“%d“,printf(“n“);if(strcmp(b,b1)!=0|c!=c1)printf(“ 验证失败,请重新输入!n“);scanf(“%c“,getchar();system(“cls“); elseprintf(“ 验证通过!请
6、按 Enter 键进入!n“);scanf(“%c“,getchar();x=1;while(x)system(“cls“);printf(“ -n“);printf(“ *图书信息管理系统*n“);printf(“ -nn“);printf(“ *nn“);printf(“ *nn“);printf(“ | 1-添加图书 2-删除图书 |nn“);printf(“ | 3-图书列表 4-图书排序 |nn“);printf(“ | 5-查询图书 6-修改图书 |nn“);printf(“ | 7-录入数据 0-退出系统 |nn“); printf(“ *nn“);printf(“ *nn“);
7、printf(“请输入所选择的序号:“);scanf(“%d“,getchar();system(“cls“);switch(choice)case 0:x=0;break;case 1:head=load();if(head=NULL)printf(“文件为空,请先录入数据!n“);getchar();break;elsehead=addbook(head);printf(“添加成功!n“);printf(“是否将新信息保存到文件?(y/n)n“);scanf(“%c“,getchar();switch(a)case n:break;case y:fprint(head);printf(“保
8、存成功!n“);getchar();break;break;case 2:head=load();if(head=NULL)printf(“文件为空,请先录入数据!n“);getchar();break;elsedeletebook(head);getchar();break;break;case 3:head=load();if(head=NULL)printf(“文件为空,请先录入数据!n“);getchar();break;elseprint_book(head);getchar();break;case 4:head=load();if(head=NULL)printf(“文件为空,请
9、先录入数据!n“);getchar();break;elsepaixu(head);getchar();break;case 5:head=load();if(head=NULL)printf(“文件为空,请先录入数据!n“);getchar();break;elsechaxun(head);getchar();break;case 6:head=load();if(head=NULL)printf(“文件为空,请先录入数据!n“);getchar();break;elsexiugai(head);getchar();break;break;case 7:printf(“注意: 输入图书编码为
10、 0 时结束 !n“);head=creatbook();printf(“是否将输入的信息保存到文件以覆盖文件中已存在的信息?(y/n)n“);getchar();scanf(“%c“,getchar();switch(a)case n:break;case y:fprint(head);printf(“保存成功!n“);getchar();break;break;default:printf(“您的输入有误,请重新输入!n“);getchar();break;break;default:printf(“ 您的输入有误! 请重新输入!n“);getchar();break;/录入数据并形成链表
11、struct book *creatbook()struct book *head,*tail,*p;int num,time,n;char bname50,wname20,press50,sort50;float price;int size=sizeof(struct book);head=tail=NULL;printf(“请输入图书编号:“);scanf(“%d“,printf(“请输入图书名:“);scanf(“%s“,bname);getchar();printf(“请输入作者名:“);scanf(“%s“,wname);getchar();printf(“请输入出版社:“);sc
12、anf(“%s“,press);getchar();printf(“请输入类别:“);scanf(“%s“,sort);getchar();printf(“请输入出版时间:“);scanf(“%d“,getchar();printf(“请输入价格:“);scanf(“%f“,getchar();while(1)p=(struct book *)malloc(size);p-num=num;strcpy(p-bname,bname);strcpy(p-wname,wname);strcpy(p-press,press);strcpy(p-sort,sort);p-time=time;p-pric
13、e=price;p-next=NULL;if(head=NULL)head=p;elsetail-next=p;tail=p;doprintf(“请输入图书编号:“);scanf(“%d“,n=yanzheng(head,num);if(n=0)break;elseprintf(“您输入的编号已存在,请重新输入!n“);while(1);if(num=0)break;else printf(“请输入图书名:“);scanf(“%s“,bname);getchar();printf(“请输入作者名:“);scanf(“%s“,wname);getchar();printf(“请输入出版社:“);
14、scanf(“%s“,press);getchar();printf(“请输入类别:“);scanf(“%s“,sort);getchar();printf(“请输入出版时间:“);scanf(“%d“,getchar();printf(“请输入价格:“);scanf(“%f“,getchar();return head;/插入结点,并且插入后仍按一定顺序struct book *addbook(struct book *head)struct book *ptr,*p1,*p2,*p;char bname50,wname20,press50,sort50;int size=sizeof(st
15、ruct book);int num,time,n=1;float price;doprintf(“请输入图书编号:“);scanf(“%d“,n=yanzheng(head,num);if(n=0)break;elseprintf(“您输入的编号已存在,请重新输入!n“);while(1);printf(“请输入图书名:“);scanf(“%s“,bname);getchar();printf(“请输入作者名:“);scanf(“%s“,wname);getchar();printf(“请输入出版社:“);scanf(“%s“,press);getchar();printf(“请输入类别:“
16、);scanf(“%s“,sort);getchar();printf(“请输入出版时间:“);scanf(“%d“,getchar();printf(“请输入价格:“);scanf(“%f“,getchar();p=(struct book *)malloc(size);p-num=num;strcpy(p-bname,bname);strcpy(p-wname,wname);strcpy(p-press,press);strcpy(p-sort,sort);p-time=time;p-price=price;p2=head;ptr=p;while(ptr-nump2-num)p2=p2-n
17、ext;if(ptr-numnum)if(head=p2)head=ptr;elsep1-next=ptr;p-next=p2;elsep2-next=ptr;p-next=NULL;return head;/验证添加的图书编号是否已存在int yanzheng(struct book *head,int m)struct book *p;p=head;while(p!=NULL)if(p-num=m)break;p=p-next;if(p=NULL)return 0;elsereturn 1;/将新链表写入文件中void fprint(struct book *head)FILE *fp;c
18、har ch=1;struct book *p1;if(fp=fopen(“f1.txt“,“w“)=NULL)printf(“File open error!n“);exit(0);fputc(ch,fp);for(p1=head;p1;p1=p1-next)fprintf(fp,“%d %s %s %s %s %d %fn“,p1-num,p1-bname,p1-wname,p1-press,p1-sort,p1-time,p1-price);fclose(fp);/从文件中读取图书信息struct book *load()FILE *fp;char ch;struct book *hea
19、d,*tail,*p1;head=tail=NULL;if(fp=fopen(“f1.txt“,“r“)=NULL)printf(“File open error!n“);exit(0);ch=fgetc(fp);if(ch=1)while(!feof(fp)p1=(struct book *)malloc(sizeof(struct book);fscanf(fp,“%d%s%s%s%s%d%fn“,if(head=NULL)head=p1;elsetail-next=p1;tail=p1;tail-next=NULL;fclose(fp);return head;elsereturn NULL;/将整个链表的信息输出void print_book(struct book *head)struct book *ptr;if(head=NULL)