C語言圖書管理系統(tǒng)代碼_第1頁
C語言圖書管理系統(tǒng)代碼_第2頁
C語言圖書管理系統(tǒng)代碼_第3頁
C語言圖書管理系統(tǒng)代碼_第4頁
C語言圖書管理系統(tǒng)代碼_第5頁
已閱讀5頁,還剩25頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

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();struct book *addbook(struct book *head);int yanzheng(struct book *head,int m);void deletebook(s

2、truct 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);void sort_chaxun(struct book *head);void time_chaxun(struct book *head);void bname_chaxun(str

3、uct 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);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,

4、d,b10,b110="yjk"struct book *head=NULL;while(y)/ 創(chuàng)建鏈表/ 添加圖書/ 驗證新添加的圖書編碼是否已存在/ 刪除圖書/ 將鏈表寫入文件/ 從文件中讀取信息并建成鏈表/ 將鏈表信息輸出/ 查詢圖書信息/ 按圖書編號查詢圖書/ 按作者名查詢圖書/ 按類別查詢圖書/ 按出版時間查詢圖書/ 按圖書名查詢圖書/ 修改圖書信息/ 對圖書進行排序/ 按圖書編號排序/ 按圖書出版時間排序/ 按圖書價格排序/ 按圖書名排序/ 按作者名排序system("cls");printf("nnnnnnn");pr

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

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

7、char();x=1;while(x)system("cls");printf("n");*n");2-刪除圖書printf("*圖書信息管理系統(tǒng)printf("nn");printf("*nn");printf("*nn");printf("|1-添加圖書|nn");printf("|3-圖書列表4-圖書排序|nn");printf("|5-查詢圖書6-修改圖書|nn");printf("|7-錄入數(shù)據(jù)0

8、-退出系統(tǒng)|nn");printf("*nn");printf("*nn");:");printf(" 請輸入所選擇的序號scanf("%d",&choice);getchar(); system("cls");switch(choice) case 0:x=0;break;case 1:head=load();if(head=NULL)printf(" 文件為空,請先錄入數(shù)據(jù)!n");getchar();break;elsehead=addbook(hea

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

10、break;elsedeletebook(head);getchar();break;break;case 3:head=load();if(head=NULL)printf(" 文件為空,請先錄入數(shù)據(jù)!n");getchar();break;elseprint_book(head);getchar();break;case 4:head=load();if(head=NULL)printf(" 文件為空,請先錄入數(shù)據(jù)!n");getchar();break;elsepaixu(head);getchar();break;case 5:head=load

11、();if(head=NULL)printf(" 文件為空,請先錄入數(shù)據(jù)!n");getchar();break;elsechaxun(head);getchar();break;case 6:head=load();if(head=NULL)printf(" 文件為空,請先錄入數(shù)據(jù)!n");getchar();break;elsexiugai(head);getchar();break;break;case 7:printf(" 注意 :輸入圖書編碼為0時結(jié)束 !n");head=creatbook();printf("

12、是否將輸入的信息保存到文件以覆蓋文件中已存在的信息 ?(y/n)n");getchar();scanf("%c",&a);getchar();switch(a)case 'n':break;case 'y':fprint(head);printf(" 保存成功!n");getchar();break;break;default:printf(" 您的輸入有誤,請重新輸入!n");getchar();break; break; default:printf("您的輸入有誤! 請

13、重新輸入!n");getchar();break;/ 錄入數(shù)據(jù)并形成鏈表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",&num);printf(" 請輸入圖書名:");scanf("

14、;%s",bname);getchar();printf(" 請輸入作者名:");scanf("%s",wname);getchar();printf(" 請輸入出版社:");scanf("%s",press);getchar();printf(" 請輸入類別:");scanf("%s",sort);getchar();printf(" 請輸入出版時間:");scanf("%d",&time);getchar();pr

15、intf(" 請輸入價格:");scanf("%f",&price);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->price=price;p->next=NULL;if(head=NULL)head=p;elsetai

16、l->next=p;tail=p;doprintf(" 請輸入圖書編號:");scanf("%d",&num);n=yanzheng(head,num);if(n=0)break;elseprintf(" 您輸入的編號已存在,請重新輸入!n");while(1);if(num=0)break;elseprintf(" 請輸入圖書名:");scanf("%s",bname);getchar();printf(" 請輸入作者名:");scanf("%s&q

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

18、urn head;/ 插入結(jié)點,并且插入后仍按一定順序struct book *addbook(struct book *head)struct book *ptr,*p1,*p2,*p;char bname50,wname20,press50,sort50;int size=sizeof(struct book);int num,time,n=1;float price;doprintf(" 請輸入圖書編號:");scanf("%d",&num);n=yanzheng(head,num);if(n=0)break;elseprintf(&quo

19、t; 您輸入的編號已存在,請重新輸入!n");while(1);printf(" 請輸入圖書名:");scanf("%s",bname);getchar();printf(" 請輸入作者名:");scanf("%s",wname);getchar();printf(" 請輸入出版社:");scanf("%s",press);getchar();printf(" 請輸入類別:");scanf("%s",sort);getchar

20、();printf(" 請輸入出版時間:");scanf("%d",&time);getchar();printf(" 請輸入價格:");scanf("%f",&price);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->ti

21、me=time;p->price=price;p2=head;ptr=p;while(ptr->num>p2->num)&&(p2->next!=NULL) p1=p2;p2=p2->next;if(ptr->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 *hea

22、d,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;char ch='1'struct book *p1;if(fp=fopen("f1.txt","w")=NULL)printf("File open error!n");exit(0);fputc(ch

23、,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 *head,*tail,*p1;head=tail=NULL;if(fp=fopen("f1.txt"

24、;,"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",&p1->num,p1->bname,p1->wname,p1->press,p1->sort,&p1->t ime,&p1->price)

25、;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)printf("n 沒有信息!n");return;printf("圖書信息列表如下n");printf("n");價格%dprintf("編號 圖書名作者名出

26、版社類別 出版時間n");for(ptr=head;ptr;ptr=ptr->next)printf("%d%s%s%s %s%.2fn",ptr->num,ptr->bname,ptr->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 *

27、fp;printf(" 請輸入要刪除的圖書編號:");scanf("%d",&a);p1=head;if(p1->num=a&&p1->next=NULL)/ 對于文件中只有一組數(shù)據(jù)printf(" 是否清空文件!(y/n)n");getchar();scanf("%c",&b);getchar();switch(b)case 'n':break;case 'y':if(fp=fopen("f1.txt","w

28、")=NULL) printf("File open error!n");exit(0);fclose(fp);printf(" 文件已清空!n");elsewhile(p1->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"

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

30、#39;:break;case 'y':fprint(head);printf(" 刪除成功!n");getchar();break;elsep2->next=p1->next;printf(" 是否確定從文件中徹底刪除該圖書?(y/n)n");getchar();scanf("%c",&b);switch(b)case 'n':break;case 'y':fprint(head);printf(" 刪除成功!n");getchar();brea

31、k; / 圖書查詢void chaxun(struct book *head)int a;printf("printf(" printf(" printf(" printf("* * 1- 按圖書編號查詢* * 3-按圖書類別查詢* * 5- 按出版時間查詢2-按圖書名查詢*n");4-按作者名查詢*n");0-退出查詢*n");printf(" 請輸入所選擇的編號:");scanf("%d",&a);getchar();switch(a)case 0:break;c

32、ase 1:num_chaxun(head);break;case 2:bname_chaxun(head);break;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("請選擇您要查詢的圖書編號:");sca

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

34、p->wname,p->press,p->sort,p->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(&qu

35、ot;沒有找到該圖書名的圖書!n");elseprintf("你所查詢的圖書信息如下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)

36、;p=p->next;) printf("=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");)elseprintf(&q

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

38、n");)/按圖書類別查詢圖書信息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");)elseprintf("你所查詢的圖書信息如下n");printf(&

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

40、haxun(struct 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");)elseprintf("你所查詢的圖書信息如下n");printf("An");pr

41、intf(" * 編號圖書名作者名出版社類別出版時間價格*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;char c;struct

42、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-

43、價格8-修改全部。-放棄修改*n");printf(=n");printf("請選擇你要修改的信息編號:");scanf("%d",&b);getchar();switch(b)case 1:printf("請輸入新編號:");scanf("%d",&p->num);printf("修改成功!n");getchar();break;case 2:printf("請輸入新圖書名:");gets(p->bname);printf(&

44、quot; 修改成功!n");break;case 3:printf(" 請輸入新作者名:");gets(p->wname);printf(" 修改成功!n");break;case 4:printf(" 請輸入新出版社:");gets(p->press);printf(" 修改成功!n");break;case 5:printf(" 請輸入新類別:");gets(p->sort);printf(" 修改成功!n");break;case 6:pr

45、intf(" 請輸入新出版時間:");scanf("%d",&p->time);printf(" 修改成功!n");getchar();break;case 7:printf(" 請輸入新價格:");scanf("%f",&p->price);printf(" 修改成功!n");getchar();break;case 8:printf(" 請輸入新圖書編號:");scanf("%d",&p->

46、num);printf(" 請輸入新圖書名:");scanf("%s",p->bname);getchar();printf(" 請輸入新作者名:");scanf("%s",p->wname);getchar();printf(" 請輸入新出版社:");scanf("%s",p->press);getchar();printf(" 請輸入新類別:");scanf("%s",p->sort);getchar();p

47、rintf("請輸入新出版時間:");scanf("%d",&p->time);getchar();printf("請輸入新價格:");scanf("%f",&p->price);getchar();printf("修改成功!n");getchar();break;case 0:break;default :printf("您的輸入有誤!n");break;printf("是否將修改后的信息保存到文件中 ?(y/n)n");sc

48、anf("%c",&c);getchar();switch(c)case 'n':break;case 'y':fprint(head);printf("保存成功!n");getchar();break;/圖書排序void paixu(struct book *head) int a;printf(" =n");2-按出版時間排序4-按圖書名排序0-取消排序操作printf("*1-按圖書編號排序*n");printf("*3-按圖書價格排序*n");pr

49、intf("*5-按作者名排序*n");printf("=n");printf("請輸入您選擇的編號:");scanf("%d",&a);getchar();switch(a)case 0:break;case 1:num_paixu(head);break;case 2:time_paixu(head);break;case 3:price_paixu(head);break;case 4:bname_paixu(head);break;case 5:wname_paixu(head);break;def

50、ault:printf("您的輸入有誤!n");break;/按圖書編號排序void num_paixu(struct book *head)struct book *a1000,*p,*p1,*temp;int i,k,index,n=0;char b;p1=head;for(p=head;p;p=p->next)n+;for(i=0;i<n;i+)ai=p1;p1=p1->next;for(k=0;k<n-1;k+)index=k;for(i=k+1;i<n;i+) if(ai->num<aindex->num) inde

51、x=i;temp=aindex;aindex=ak;ak=temp;printf("排序成功!n");printf("是否顯示排序結(jié)果?(y/n)n"); scanf("%s",&b);getchar();switch(b)case 'n':break;case 'y':printf("=n");出版時間%.2fprintf("*編號 圖書名 作者名 出版社 類別價格 *n");for(i=0;i<n;i+) printf("* %d%s%

52、s%s%s%d*n",ai->num,ai->bname,ai->wname,ai->press,ai->sort,ai->time,ai->price);printf("=n");break;default:printf("您的輸入有誤!n");break;/按出版時間排序void time_paixu(struct book *head)struct book *a1000,*p,*p1,*temp;int i,k,index,n=0;char b;p1=head;for(p=head;p;p=p-

53、>next)n+;for(i=0;i<n;i+)ai=p1;p1=p1->next;)for(k=0;k<n-1;k+)index=k;for(i=k+1;i<n;i+)if(ai->time<aindex->time) index=i;)temp=aindex;aindex=ak;ak=temp;)printf("排序成功!n");printf("是否顯示排序結(jié)果?(y/n)n");scanf("%s",&b);getchar();switch(b)case 'n

54、9;:break;case 'y':printf("=n");printf("*編號 圖書名作者名出版社類別出版時間價格 *n");for(i=0;i<n;i+)printf("* %d%s%s%s%s%d%.2f*n",ai->num,ai->bname,ai->wname,ai->press,ai->sort,ai->time,ai->price); )printf("=n");break;default:printf("您的輸入有誤!n

55、"); break;)/按圖書價格排序void price_paixu(struct book *head)struct book *a1000,*p,*p1,*temp;int i,k,index,n=0;char b;p1=head;for(p=head;p;p=p->next)n+;for(i=0;i<n;i+)ai=p1;p1=p1->next;for(k=0;k<n-1;k+)index=k;for(i=k+1;i<n;i+)if(ai->price<aindex->price) index=i;temp=aindex;aindex=ak;ak=temp;printf("排序成功!n");printf("是否顯示排序結(jié)果?(y/n)n");scanf("%s",&b);getchar();

溫馨提示

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

評論

0/150

提交評論