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

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(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(); /創(chuàng)建鏈表struct book *addbook(struct book *head); /添加圖書int yanzheng(struct book *head,int m); /驗(yàn)證

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); /按圖書編號(hào)查詢圖書void wname_chaxun(struct book *head); /按作者

3、名查詢圖書void sort_chaxun(struct book *head); /按類別查詢圖書void time_chaxun(struct book *head); /按出版時(shí)間查詢圖書void bname_chaxun(struct book *head); /按圖書名查詢圖書void xiugai(struct book *head); /修改圖書信息void paixu(struct book *head); /對(duì)圖書進(jìn)行排序void num_paixu(struct book *head); /按圖書編號(hào)排序void time_paixu(struct book *head);

4、 /按圖書出版時(shí)間排序void price_paixu(struct book *head); /按圖書價(jià)格排序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(" 請(qǐng)輸入您的選擇:");scanf("%d",&n);printf("n");getchar();switch(n)case 0:y=0;break;case 1: printf(" 請(qǐng)輸入您的

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

7、 scanf("%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")

8、; printf(" | 5-查詢圖書 6-修改圖書 |nn"); printf(" | 7-錄入數(shù)據(jù) 0-退出系統(tǒng) |nn"); printf(" *nn"); printf(" *nn"); printf("請(qǐng)輸入所選擇的序號(hào):"); scanf("%d",&choice); getchar(); system("cls"); switch(choice) case 0: x=0;break; case 1: head=load(); if(h

9、ead=NULL) printf("文件為空,請(qǐng)先錄入數(shù)據(jù)!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"); getchar(

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

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

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

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

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

15、ss);getchar();printf("請(qǐng)輸入類別:"); scanf("%s",sort);getchar(); printf("請(qǐng)輸入出版時(shí)間:"); scanf("%d",&time);getchar(); printf("請(qǐng)輸入價(jià)格:"); scanf("%f",&price);getchar();while(1)p=(struct book *)malloc(size);p->num=num;strcpy(p->bname,bname

16、);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("請(qǐng)輸入圖書編號(hào):"); scanf("%d",&num);n=yanzheng(head,num);if(n=0)break;elseprintf("您輸入的編號(hào)已存在,

17、請(qǐng)重新輸入!n");while(1);if(num=0)break;else printf("請(qǐng)輸入圖書名:");scanf("%s",bname);getchar();printf("請(qǐng)輸入作者名:");scanf("%s",wname);getchar();printf("請(qǐng)輸入出版社:");scanf("%s",press);getchar();printf("請(qǐng)輸入類別:");scanf("%s",sort);getc

18、har();printf("請(qǐng)輸入出版時(shí)間:");scanf("%d",&time);getchar();printf("請(qǐng)輸入價(jià)格:");scanf("%f",&price);getchar();return head;/插入結(jié)點(diǎn),并且插入后仍按一定順序struct book *addbook(struct book *head)struct book *ptr,*p1,*p2,*p; char bname50,wname20,press50,sort50;int size=sizeof(stru

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

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

21、);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->num<=p

22、2->num)if(head=p2)head=ptr;elsep1->next=ptr; p->next=p2;elsep2->next=ptr;p->next=NULL;return head;/驗(yàn)證添加的圖書編號(hào)是否已存在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

23、 *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->sort,p1

24、->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 *)malloc(sizeo

25、f(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;/將整個(gè)鏈表的信息輸出void print_book(

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

27、ame,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("請(qǐng)輸入要?jiǎng)h除的圖書編號(hào):");scanf("%d",&a); p1=head; if(p1->num=a&&p1->next=NULL) /對(duì)于文件

28、中只有一組數(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(fp);printf("文件已清空!n");else while(p1->num

29、!=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"); getchar()

30、; break; else printf("沒有找到要?jiǎng)h除的數(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; else p2-

31、>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-按圖書編號(hào)查詢 2-按圖書名查詢 *n");printf(" * 3-按圖書類別查詢 4-按作者名查詢 *n");printf(" * 5-按出版時(shí)間查詢 0-退出查詢 *n");printf(" =n");printf("請(qǐng)輸入所選擇的編號(hào):");scanf("%d",&a);getchar();switch(a)case 0:break;case 1:num_chaxun(head);break;case 2:bname_chaxun(head);break;case

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

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

35、,p->price);printf(" =n");/按圖書名查詢圖書信息void bname_chaxun(struct book *head)char a50;int flag=0;struct book *p; printf("請(qǐng)選擇您要查詢的圖書名:");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 printf("

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

37、uot;);/按作者名查詢圖書信息void wname_chaxun(struct book *head)char a50;int flag=0;struct book *p; printf("請(qǐng)選擇您要查詢的圖書作者名:");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");printf(&q

38、uot; =n");printf(" * 編號(hào) 圖書名 作者名 出版社 類別 出版時(shí)間 價(jià)格 *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

39、 sort_chaxun(struct book *head)char a50;int flag=0;struct book *p; printf("請(qǐng)選擇您要查詢的圖書類別:");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");printf(&q

40、uot; * 編號(hào) 圖書名 作者名 出版社 類別 出版時(shí)間 價(jià)格 *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");/按圖書出版時(shí)間查詢圖書信息void time_chaxun(struct boo

41、k *head)int a,flag=0;struct book *p; printf("請(qǐng)選擇您要查詢的圖書出版時(shí)間:");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、 編號(hào) 圖書名 作者名 出版社 類別 出版時(shí)間 價(jià)格 *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;struc

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

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論