C語言鏈表式圖書管理系統_第1頁
C語言鏈表式圖書管理系統_第2頁
C語言鏈表式圖書管理系統_第3頁
C語言鏈表式圖書管理系統_第4頁
C語言鏈表式圖書管理系統_第5頁
已閱讀5頁,還剩22頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、#include<stdio.h>#include<stdlib.h>#include<string.h>struct bookint num;char bname50;char wname20;char press50;char sort50;int time;float price; struct book *next;struct book *creatbook(); /創(chuàng)建鏈表struct book *addbook(struct book *head); /添加圖書int yanzheng(struct book *head,int m); /驗證

2、新添加的圖書編碼是否已存在void deletebook(struct book *head); /刪除圖書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); /按作者

3、名查詢圖書void sort_chaxun(struct book *head); /按類別查詢圖書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);

4、 /按圖書出版時間排序void price_paixu(struct book *head); /按圖書價格排序void bname_paixu(struct book *head); /按圖書名排序void wname_paixu(struct book *head); /按作者名排序int main()int choice,n,x,y=1,c,c1=1234560;char a,d,b20,b120="124567890"struct book *head=NULL;while(y)system("cls"); printf("nnnnnn

5、n"); printf(" * 歡迎光臨 *nn"); printf(" * 圖書信息管理系統 *nnn");printf("nn");printf(" =1-用戶登錄=n");printf(" =0-退出系統=n");printf(" 請輸入您的選擇:");scanf("%d",&n);printf("n");getchar();switch(n)case 0:y=0;break;case 1: printf(&qu

6、ot; 請輸入您的用戶名:"); gets(b); printf("n"); printf(" 請輸入您的密碼:"); scanf("%d",&c); printf("n"); if(strcmp(b,b1)!=0|c!=c1) printf(" 驗證失敗,請重新輸入!n"); scanf("%c",&d); getchar(); system("cls"); else printf(" 驗證通過!請按Enter鍵進入!

7、n"); scanf("%c",&d); getchar(); x=1; while(x) system("cls"); printf(" -n"); printf(" *圖書信息管理系統*n"); printf(" -nn"); printf(" *nn"); printf(" *nn"); printf(" | 1-添加圖書 2-刪除圖書 |nn"); printf(" | 3-圖書列表 4-圖書排序 |

8、nn"); printf(" | 5-查詢圖書 6-修改圖書 |nn"); printf(" | 7-錄入數據 0-退出系統 |nn"); printf(" *nn"); printf(" *nn"); printf("請輸入所選擇的序號:"); scanf("%d",&choice); getchar(); system("cls"); switch(choice) case 0: x=0;break; case 1: head=loa

9、d(); if(head=NULL) printf("文件為空,請先錄入數據!n"); getchar(); break; else head=addbook(head); printf("添加成功!n"); printf("是否將新信息保存到文件?(y/n)n"); scanf("%c",&a); getchar(); switch(a) case 'n': break; case 'y': fprint(head); printf("保存成功!n");

10、 getchar(); break; break; case 2: head=load(); if(head=NULL) printf("文件為空,請先錄入數據!n"); getchar(); break; else deletebook(head); getchar(); break; break; case 3: head=load(); if(head=NULL) printf("文件為空,請先錄入數據!n"); getchar(); break; else print_book(head); getchar(); break; case 4: h

11、ead=load(); if(head=NULL) printf("文件為空,請先錄入數據!n"); getchar(); break; else paixu(head); getchar(); break; case 5: head=load(); if(head=NULL) printf("文件為空,請先錄入數據!n"); getchar(); break; else chaxun(head); getchar(); break; case 6: head=load(); if(head=NULL) printf("文件為空,請先錄入數據!

12、n"); getchar(); break; else xiugai(head); getchar(); break; break; case 7: printf("注意:輸入圖書編碼為0時結束!n"); head=creatbook(); printf("是否將輸入的信息保存到文件以覆蓋文件中已存在的信息?(y/n)n"); getchar(); scanf("%c",&a); getchar(); switch(a) case 'n': break; case 'y': fprin

13、t(head); printf("保存成功!n"); getchar(); break; break; default: printf("您的輸入有誤,請重新輸入!n"); getchar(); break;break;default:printf(" 您的輸入有誤! 請重新輸入!n");getchar();break;/錄入數據并形成鏈表struct book *creatbook()struct book *head,*tail,*p;int num,time,n;char bname50,wname20,press50,sort

14、50;float price;int size=sizeof(struct book);head=tail=NULL;printf("請輸入圖書編號:");scanf("%d",&num); printf("請輸入圖書名:"); scanf("%s",bname);getchar(); printf("請輸入作者名:"); scanf("%s",wname);getchar(); printf("請輸入出版社:"); scanf("%s&

15、quot;,press);getchar();printf("請輸入類別:"); scanf("%s",sort);getchar(); printf("請輸入出版時間:"); scanf("%d",&time);getchar(); printf("請輸入價格:"); scanf("%f",&price);getchar();while(1)p=(struct book *)malloc(size);p->num=num;strcpy(p->bn

16、ame,bname);strcpy(p->wname,wname);strcpy(p->press,press);strcpy(p->sort,sort);p->time=time;p->price=price;p->next=NULL;if(head=NULL)head=p;elsetail->next=p;tail=p;do printf("請輸入圖書編號:"); scanf("%d",&num);n=yanzheng(head,num);if(n=0)break;elseprintf("您

17、輸入的編號已存在,請重新輸入!n");while(1);if(num=0)break;else printf("請輸入圖書名:");scanf("%s",bname);getchar();printf("請輸入作者名:");scanf("%s",wname);getchar();printf("請輸入出版社:");scanf("%s",press);getchar();printf("請輸入類別:");scanf("%s",s

18、ort);getchar();printf("請輸入出版時間:");scanf("%d",&time);getchar();printf("請輸入價格:");scanf("%f",&price);getchar();return head;/插入結點,并且插入后仍按一定順序struct book *addbook(struct book *head)struct book *ptr,*p1,*p2,*p; char bname50,wname20,press50,sort50;int size=si

19、zeof(struct book); int num,time,n=1;float price;do printf("請輸入圖書編號:"); scanf("%d",&num);n=yanzheng(head,num);if(n=0)break;elseprintf("您輸入的編號已存在,請重新輸入!n");while(1); printf("請輸入圖書名:"); scanf("%s",bname);getchar(); printf("請輸入作者名:"); scanf

20、("%s",wname);getchar(); printf("請輸入出版社:"); scanf("%s",press);getchar();printf("請輸入類別:"); scanf("%s",sort);getchar(); printf("請輸入出版時間:"); scanf("%d",&time);getchar(); printf("請輸入價格:"); scanf("%f",&price)

21、;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->num>p2->num)&&(p2->next!=NULL)p1=p2;p2=p2->next;if(ptr->

22、num<=p2->num)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(st

23、ruct book *head)FILE *fp;char 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-&g

24、t;sort,p1->time,p1->price);fclose(fp);/從文件中讀取圖書信息struct book *load()FILE *fp;char ch;struct book *head,*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 *)mal

25、loc(sizeof(struct book); fscanf(fp,"%d%s%s%s%s%d%fn",&p1->num,p1->bname,p1->wname,p1->press,p1->sort,&p1->time,&p1->price); if(head=NULL) head=p1; else tail->next=p1; tail=p1; tail->next=NULL; fclose(fp); return head;elsereturn NULL;/將整個鏈表的信息輸出void pr

26、int_book(struct book *head)struct book *ptr;if(head=NULL)printf("n沒有信息!n");return;printf(" 圖書信息列表如下n");printf(" =n");printf(" 編號 圖書名 作者名 出版社 類別 出版時間 價格n");for(ptr=head;ptr;ptr=ptr->next)printf(" %d %s %s %s %s %d %.2fn",ptr->num,ptr->bname,p

27、tr->wname,ptr->press,ptr->sort,ptr->time,ptr->price);printf(" =n");/刪除圖書信息void deletebook(struct book *head)int a;char b,ch='1'struct book *p1,*p2;FILE *fp;printf("請輸入要刪除的圖書編號:");scanf("%d",&a); p1=head; if(p1->num=a&&p1->next=NU

28、LL) /對于文件中只有一組數據printf("是否清空文件!(y/n)n");getchar();scanf("%c",&b);getchar();switch(b)case 'n':break;case 'y': if(fp=fopen("f1.txt","w")=NULL) printf("File open error!n"); exit(0); fclose(fp);printf("文件已清空!n");else while(p

29、1->num!=a&&p1->next!=NULL) p2=p1; p1=p1->next; if(p1->next=NULL) if(p1->num=a) p2->next=NULL; printf("是否確定從文件中徹底刪除該圖書?(y/n)n"); getchar(); scanf("%c",&b); switch(b) case 'n': break; case 'y': fprint(head); printf("刪除成功!n");

30、getchar(); break; else printf("沒有找到要刪除的數據!n"); getchar(); else if(p1=head) head=p1->next; printf("是否確定從文件中徹底刪除該圖書?(y/n)n"); getchar(); scanf("%c",&b); switch(b) case 'n': break; case 'y': fprint(head); printf("刪除成功!n"); getchar(); break;

31、 else p2->next=p1->next; printf("是否確定從文件中徹底刪除該圖書?(y/n)n"); getchar(); scanf("%c",&b); switch(b) case 'n': break; case 'y': fprint(head); printf("刪除成功!n"); getchar(); break; /圖書查詢void chaxun(struct book *head)int a;printf(" =n");printf

32、(" * 1-按圖書編號查詢 2-按圖書名查詢 *n");printf(" * 3-按圖書類別查詢 4-按作者名查詢 *n");printf(" * 5-按出版時間查詢 0-退出查詢 *n");printf(" =n");printf("請輸入所選擇的編號:");scanf("%d",&a);getchar();switch(a)case 0:break;case 1:num_chaxun(head);break;case 2:bname_chaxun(head);b

33、reak;case 3:sort_chaxun(head);break;case 4:wname_chaxun(head);break;case 5:time_chaxun(head);break;default:printf("您的輸入有誤!n");break;/按編號查詢圖書信息void num_chaxun(struct book *head)int a;struct book *p; printf("請選擇您要查詢的圖書編號:");scanf("%d",&a);getchar();p=head; while(p!=NU

34、LL)if(p->num=a)break;p=p->next;if(p=NULL)printf("沒有找到該編號的圖書!n");elseprintf(" 你所查詢的圖書信息如下n");printf(" =n");printf(" * 編號 圖書名 作者名 出版社 類別 出版時間 價格 *n");printf(" * %d %s %s %s %s %d %.2f *n",p->num,p->bname,p->wname,p->press,p->sort,p

35、->time,p->price);printf(" =n");/按圖書名查詢圖書信息void bname_chaxun(struct book *head)char a50;int flag=0;struct book *p; printf("請選擇您要查詢的圖書名:");gets(a);p=head;while(p!=NULL)if(strcmp(p->bname,a)=0)flag=1;break;p=p->next; if(flag=0)printf("沒有找到該圖書名的圖書!n");else print

36、f(" 你所查詢的圖書信息如下n");printf(" =n");printf(" * 編號 圖書名 作者名 出版社 類別 出版時間 價格 *n"); while(p!=NULL)if(strcmp(p->bname,a)=0)printf(" * %d %s %s %s %s %d %.2f *n",p->num,p->bname,p->wname,p->press,p->sort,p->time,p->price); p=p->next; printf(&q

37、uot; =n");/按作者名查詢圖書信息void wname_chaxun(struct book *head)char a50;int flag=0;struct book *p; printf("請選擇您要查詢的圖書作者名:");gets(a);p=head; while(p!=NULL)if(strcmp(p->wname,a)=0)flag=1;break;p=p->next; if(flag=0)printf("沒有找到該圖書名的圖書!n");else printf(" 你所查詢的圖書信息如下n");

38、printf(" =n");printf(" * 編號 圖書名 作者名 出版社 類別 出版時間 價格 *n"); while(p!=NULL)if(strcmp(p->wname,a)=0)printf(" * %d %s %s %s %s %d %.2f *n",p->num,p->bname,p->wname,p->press,p->sort,p->time,p->price);flag=1; p=p->next; printf(" =n");/按圖書類別查

39、詢圖書信息void sort_chaxun(struct book *head)char a50;int flag=0;struct book *p; printf("請選擇您要查詢的圖書類別:");gets(a);p=head; while(p!=NULL)if(strcmp(p->sort,a)=0)flag=1;break;p=p->next; if(flag=0)printf("沒有找到該圖書名的圖書!n");else printf(" 你所查詢的圖書信息如下n");printf(" =n");

40、printf(" * 編號 圖書名 作者名 出版社 類別 出版時間 價格 *n"); while(p!=NULL)if(strcmp(p->sort,a)=0)printf(" * %d %s %s %s %s %d %.2f *n",p->num,p->bname,p->wname,p->press,p->sort,p->time,p->price);flag=1; p=p->next; printf(" =n");/按圖書出版時間查詢圖書信息void time_chaxun(s

41、truct book *head)int a,flag=0;struct book *p; printf("請選擇您要查詢的圖書出版時間:");scanf("%d",&a); getchar();p=head; while(p!=NULL)if(p->time=a)flag=1;break;p=p->next; if(flag=0)printf("沒有找到該圖書名的圖書!n");else printf(" 你所查詢的圖書信息如下n");printf(" =n");printf

42、(" * 編號 圖書名 作者名 出版社 類別 出版時間 價格 *n"); while(p!=NULL)if(p->time=a)printf(" * %d %s %s %s %s %d %.2f *n",p->num,p->bname,p->wname,p->press,p->sort,p->time,p->price);flag=1; p=p->next; printf(" =n");/修改圖書信息void xiugai(struct book *head)int a,b;cha

43、r c;struct book *p;printf("請輸入要修改的圖書編號:");scanf("%d",&a);p=head;while(p!=NULL)if(p->num=a)break;p=p->next;if(p=NULL)printf("沒有找到該編號的圖書!n");getchar();elseprintf(" =n");printf(" * 1-編號 2-圖書名 3-作者名 *n");printf(" * 4-出版社 5-類別 6-出版時間 *n");printf(" * 7-價格 8-修改全部 0-放棄修改 *n");printf(" =n");printf("請選擇你要修改的信息編號:");scanf("%d",&b);getchar();switch(b)case 1:printf("請輸入新編號:");scanf("%d",&p->num);printf("

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論