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

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quá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(); /創(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=1234;char a,d,b10,b110="yjk"struct book *head=null;while(y) system("cls");printf("nnnnnnn"

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

6、用戶名:");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");elseprintf(" 驗證通過!請按enter鍵進入!n");scanf("

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

8、6-修改圖書 |nn");printf(" | 7-錄入數(shù)據(jù) 0-退出系統(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"

9、;);getchar();break;elsehead=addbook(head);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&qu

10、ot;); getchar();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

11、;case 5:head=load();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();p

12、rintf("是否將輸入的信息保存到文件以覆蓋文件中已存在的信息?(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(" 您的輸入有誤! 請重新輸入!n"

13、;); 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("%s",bna

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

15、");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;elsetail->next=p;tail=

16、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",wname);getchar()

17、;printf("請輸入出版社:");scanf("%s",press);getchar();printf("請輸入類別:");scanf("%s",sort);getchar();printf("請輸入出版時間:");scanf("%d",&time);getchar();printf("請輸入價格:");scanf("%f",&price);getchar();return head; /插入結(jié)點,并且插入后仍按一定

18、順序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("您輸入的編號已存在,請重新輸入!n"

19、;); while(1);printf("請輸入圖書名:");scanf("%s",bname);getchar();printf("請輸入作者名:");scanf("%s",wname);getchar();printf("請輸入出版社:");scanf("%s",press);getchar();printf("請輸入類別:");scanf("%s",sort);getchar();printf("請輸入出版時間:&quo

20、t;);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->time=time;p->price=price;p2=h

21、ead;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 *head,int m)struct book *p;p=head;w

22、hile(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,fp);for(p1=head;p1;p1=p1->next) fpri

23、ntf(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","r")=null)printf("file o

24、pen 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->time,&p1->price);if(head=null)head=p1;elsetail->next=p1;tail=p1

25、;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");printf(" 編號 圖書名 作者名 出版社 類別 出版時間 價格n");for(ptr=head;ptr;ptr=ptr->

26、;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 *fp;printf("請輸入要刪除的圖書編號:");scanf("

27、;%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")=null)printf("file open error!n");exit(0);fclose(f

28、p);printf("文件已清空!n"); %delsewhile(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",&b); switch(b)case n:break;case y:fprint(head);printf("刪

29、除成功!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:break;case y:fprint(head);printf("刪除成功!n");getchar();break; elsep2->next=p1-&

30、gt;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(" * 1-按圖書編號查詢 2-按圖書名查詢 *n"); printf("

31、 * 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);break;case 3:sort_chaxun(head);break;case 4:wname_c

32、haxun(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!=null)if(p->num=a)break;p=p->next; if(p=null)prin

33、tf("沒有找到該編號的圖書!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->time,p->price);printf("=n");/按圖書名查詢

34、圖書信息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"); elseprintf(" 你所查詢的圖書信息如下n");printf("=n");prin

35、tf(" * 編號 圖書名 作者名 出版社 類別 出版時間 價格 *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("=n"); /按作者名查詢圖書信息void wname_chaxun(struct book *head

36、)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(" 你所查詢的圖書信息如下n");printf("=n");printf(" * 編號 圖書名 作者名 出版社 類別 出版時間 價格

37、 *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"); /按圖書類別查詢圖書信息void sort_chaxun(struct book *head)char a50;int flag=0;struct bo

38、ok *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("=n");printf(" * 編號 圖書名 作者名 出版社 類別 出版時間 價格 *n");while(p!=null)if(strc

39、mp(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(struct book *head)int a,flag=0;struct book *p;printf("請選擇您要查詢的圖書出版時間:"

40、;);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("=n");printf(" * 編號 圖書名 作者名 出版社 類別 出版時間 價格 *n");while(p!=null)if(p->time=a)printf(

41、"* %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 book *p;printf("請輸入要修改的圖書編號:");scanf("%d",&a);p=head;

42、while(p!=null)if(p->num=a)break;p=p->next;if(p=null)printf("沒有找到該編號的圖書!n");getchar();elseprintf("=n"); printf(" * 1-編號 2-圖書名 者名 *n");printf(" * 4-出版社 5-類別 版時間 *n");printf(" * 7-價格 8-修改全部 棄修改 *n");printf("=n"); printf("請選擇你要修改的信息編

43、號:");scanf("%d",&b);getchar();switch(b)case 1:printf("請輸入新編號:");scanf("%d",&p->num);printf("修改成功!n");getchar();break;case 2:printf("請輸入新圖書名:");gets(p->bname);printf("修改成功!n");break;case 3:printf("請輸入新作者名:");gets(p->wname);printf("修改成功!n");break;case 4:printf("請輸入新出版社:"); 3-作6-出0-放gets(p->press);printf(

溫馨提示

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

評論

0/150

提交評論