哇哈哈哈 终于写完了 我的图书馆管理系统 多谢论坛各位的指点和度娘的帮助
程序代码:
#include<stdio.h> #include<string.h> #include<malloc.h> #include <windows.h> struct book { int num; char name[30]; char author[30]; int classnum; char pub[30]; long int time; float price; struct book *next; }; void new_book(struct book *head); struct book *read_book(); struct book *new_book(); void input_book(struct book *head); void output_all_book(struct book *head); void find_book(struct book *head); void delete_book(struct book *head); void modify_book(struct book *head); void save_book(struct book *head); void main() { struct book *head=NULL; int select,temp,i=0; printf("是否导入图书库\n1·是\t2·否\n"); scanf("%d",&select); if(select==1) { head=read_book(); if(head==NULL) { printf("读取文件失败,请退出系统\n"); i=1; } else printf("导入完成\n"); } else { printf("新建一个图书库\n"); head=new_book(); } if(i!=1) system("cls"); printf("想要做什么:\n"); while(1) { printf("1·录入书本\n2·输出所有书本信息\n3·查询书本\n4·书本删除\n5·修改书本\n6·保存\n7·退出系统\n"); scanf("%d",&select); getchar(); system("cls"); switch(select) { case 1:input_book(head);break; case 2:output_all_book(head);break; case 3:find_book(head);break; case 4:delete_book(head);break; case 5:modify_book(head);break; case 6:save_book(head);break; case 7:break; } if(select==7) { printf("请确认保存\n1·已保存\n2·还未保存\n"); scanf("%d",&temp); } if(temp==1&&select==7) break; } } struct book *new_book() { struct book *p,*last,*head=NULL; int i=1; float x; while(i==1) { p=(struct book*)malloc(sizeof(struct book)); printf("请输入书号"); scanf("%d",&p->num); fflush(stdin); printf("输入书名"); gets(p->name); printf("输入作者"); gets(p->author); printf("输入分类号"); scanf("%d",&p->classnum); fflush(stdin); printf("输入出版社"); gets(p->pub); printf("输入出版时间"); scanf("%d",&p->time); fflush(stdin); printf("输入价格"); scanf("%f",&x); p->price=x; p->next=NULL; if(head==NULL) { head=p; last=p; } else { last->next=p; last=p; } printf("是否继续?1·是\t2·否\n"); scanf("%d",&i); } return head; } void save_book(struct book *head) { FILE *fp; struct book *p; if((fp=fopen("book.txt","wb"))==NULL) { printf("打开文件失败"); } p=head; while(p) { fwrite(p,sizeof(struct book),1,fp); p=p->next; } fclose(fp); } void modify_book(struct book *head) { struct book *p; float x; int temp_num,k=0,i; printf("输入要修改的书号"); scanf("%d",&temp_num); p=head; while(p) { if(p->num==temp_num) { printf("输入修改后的书名"); gets(p->name); printf("输入修改后作者"); gets(p->author); printf("输入修改后分类号"); scanf("%d",&p->classnum); printf("输入修改后出版社"); gets(p->pub); printf("输入修改后出版时间"); scanf("%d",&p->time); printf("输入修改后价格"); scanf("%f",&x); x=p->price; k=1; } p=p->next; } if(k==0) printf("未找到该书\n"); } void delete_book(struct book *head) { int temp_num; struct book *p,*r; printf("请输入要删除书的书号\n"); scanf("%d",&temp_num); r=head; p=head->next; while(r) { if(r->num==temp_num) { r=p; } r=r->next; p=p->next; } } void find_book(struct book *head) { struct book *p; int temp_num; printf("请输入书号"); scanf("%d",&temp_num); p=head; while(p) { if(p->num==temp_num) { printf("书名%s\n",p->name); printf("作者%s\n",p->author); printf("分类号%d\n",p->classnum); printf("出版社%s\n",p->pub); printf("出版时间%d\n",p->time); printf("价格%d\n",p->price); } p=p->next; } } void output_all_book(struct book *head) { struct book *p; int i=1; p=head; while(p) { printf("第%d本书的信息\n",i); printf("书号%d\n",p->num); printf("书名%s\n",p->name); printf("作者%s\n",p->author); printf("分类号%d\n",p->classnum); printf("出版社%s\n",p->pub); printf("出版时间%d\n",p->time); printf("价格%d\n",p->price); i++; p=p->next; } } void input_book(struct book *head) { struct book *p,*last; int i=1; float x; last=head; while(last->next!=NULL) last=last->next; while(i==1) { p=(struct book*)malloc(sizeof(struct book)); printf("请输入书号"); scanf("%d",&p->num); fflush(stdin); printf("输入书名"); gets(p->name); printf("输入作者"); gets(p->author); printf("输入分类号"); scanf("%d",&p->classnum); fflush(stdin); printf("输入出版社"); gets(p->pub); printf("输入出版时间"); scanf("%d",&p->time); fflush(stdin); printf("输入价格"); scanf("%f",&x); p->price=x; p->next=NULL; last->next=p; last=p; printf("是否继续?1·是\t2·否\n"); scanf("%d",&i); } } struct book *read_book() { FILE *fp; struct book *head,*last,*p; head=NULL; if((fp=fopen("book.txt","rb"))==NULL) { return NULL; } while(!feof(fp)) { p=(struct book*)malloc(sizeof(struct book)); if(fread(p,sizeof(struct book),1,fp)==1) { p->next=NULL; if(head==NULL) { head=p; last=p; } else { last->next=p; last=p; } } } fclose(fp); return head; }终于写完了 真心累啊 平时是睡神的我在这两天两夜睡眠加起来不超过八小时 当测试成功那一瞬间 激动的快泪奔了
同时 也让我感觉自己是多么的菜 身为已经读了一年大学的我 连个链表都不会创建 真为自己感到汗颜
接下来一定要多看书 多看代码 多练习 落下的总归是要补上的