收藏 分销(赏)

图书管理系统--(c语言).doc

上传人:快乐****生活 文档编号:2493120 上传时间:2024-05-30 格式:DOC 页数:21 大小:326.04KB
下载 相关 举报
图书管理系统--(c语言).doc_第1页
第1页 / 共21页
图书管理系统--(c语言).doc_第2页
第2页 / 共21页
图书管理系统--(c语言).doc_第3页
第3页 / 共21页
图书管理系统--(c语言).doc_第4页
第4页 / 共21页
图书管理系统--(c语言).doc_第5页
第5页 / 共21页
点击查看更多>>
资源描述

1、图书管理系统 (c语言) 作者: 日期:2 个人收集整理 勿做商业用途程序设计方法课程设计 问题提出:设计一个图书馆的图书管理,具体要求如下:1录入新采购的图书信息,做成文件book.in,要求有良好的输入界面;2查询各种图书的基本信息,设计出良好的查询界面;3按图书编号进行排序,将排好序的图书信息输出到文件 book。out中。4统计各类图书册数、图书馆的图书总册数。5统计图书的借阅情况.1. 需求分析对于图书馆的管理系统,应该满足以下的用户需求:用户能够增加和删除一些图书。用户根据一些基本信息查询相应的图书,系统可以准确输出所有符合条件的图书信息。用户可以借阅和归还图书馆中的图书。系统可以

2、对图书做出统计,包括图书总数,各类图书数目,图书的借阅情况。2. 设计分析定义图书结构体,其成员包括书名、作者、类型、出版社、价格、是否借阅(整型,1为借出,0为未借)、编号。输入新书时,利用结构体储存书的信息,同时将新书信息以二进制写入book.in文件中,录入结束后,将新书按输入顺序写入book。out中,并且以原来存书的最大编号为基准对新书进行编号,这样可以保证book.out文件存储的图书都是按编号进行排序的。查找图书时,获得所查书目基本信息后,以二进制依次读取文件中的每一本图书信息,并与输入信息相比较,输出符合信息的图书。删除图书时,通过输入的图书编号(编号可通过查找图书获得)查找相

3、应图书,删除相应数据并保存.借阅或归还图书时,通过图书编号(编号可通过查找图书获得)查找相应图书,修改图书的属性(是否借阅)并保存.实现统计功能时,以二进制依次读取文件中的每一本图书信息,设置变量统计图书类型、借阅情况和图书总数,到文件结尾时,输出各个变量的值。3. 系统设计(流程图表示)开始欢迎界面打印主菜单输入选择nn=1是输入新书信息否将新书保存至book.in将book.in中图书按编号保存至book.outn=2是选择查找方式输入查找图书信息否在book.out中能找到相应图书否是输出图书信息出错提示n=3是输入删除图书信息(编号)否是否在book.out中能找到相应图书出错提示删除

4、相应图书否是n=4选择借书或还书操作输入图书信息(编号)否是否在book.out中能找到相应图书出错提示对图书相应属性进行修改n=5是统计book.out中总书数并输出统计各类型书数并输出否统计图书借阅情况并输出是n=0结束4. 程序代码主函数main()int i,a; Struct book standard=a,”a”,000000000,0,0,a,others; FILE fp; if((fp=fopen(book.out,”r))=NULL) if(fp=fopen(book.out”,wb”))=NULL) printf(”wrong!”); exit(0); fwrite(&s

5、tandard,sizeof(struct book),1,fp); fclose(fp); for(i=0;i=14;i+) /*欢迎界面设置*/ clrscr(); gotoxy(18,25i); printf(Welcome to Liberary Management System”); delay(300000000000); sleep(1); gotoxy(18,27i); printf(”Press any key to continue); getch(); clrscr();while(a!=48) /打印主菜单*/ gotoxy(13,5); printf(*”); go

6、toxy(25,8); printf(1:Add new books”); gotoxy(25,10); printf(2:Search books”); gotoxy(25,12); printf(3:Delete books”); gotoxy(25,14); printf(”4:Borrow or return books”); gotoxy(25,16); printf(5:Total numbers”); gotoxy(25,18); printf(”0:Exit); gotoxy(13,21); printf(”*); a=getch() /*调用不同函数实现不同功能*/if(a=

7、49) input_books();if(a=50) search_books();if(a=51) delete_book();if(a=53) total_numbers();if(a=52) br_books();clrscr();函数addone和minusone对图书编号进行操作void addone(char ss) /将编号增加一个/int i;if(ss8=9)ss8=0; ss7=ss7+1; else ss8=ss8+1; for(i=7;i=0;i-) if(ssi=58) ssi=0;ssi1+; void minusone(char ss) /图书编号减一*/int

8、i;if(ss8=0)ss8=9; ss7=ss71; else ss8=ss8-1; for(i=7;i=0;i) if(ssi=47) ssi=9;ssi1; 函数save_books()将book。in的数据写入book.out中void save_books() /*保存图书*/FILE *fp1,*fp2;struct book oldbook,newbook;char ssl10;int n,i;if((fp2=fopen(book.out”,rb+))=NULL)printf(wrong!);exit(0);if(fp1=fopen(”book.in”,”rb”))=NULL)p

9、rintf(”wrong!”);exit(0);fseek(fp2,100L,2);fread(oldbook,sizeof(struct book),1,fp2); /获得目前图书最大编号/strcpy(ssl,oldbook.number);fclose(fp2);fseek(fp1,1L,2);fscanf(fp1,”%d”,&n);rewind(fp1);if(fp2=fopen(”book。out”,ab”)=NULL)printf(”wrong!”);exit(0);for(i=1;i=n;i+)fread(newbook,sizeof(struct book),1,fp1);ad

10、done(ssl);strcpy(newbook。number,ssl); /为新图书编号*/fwrite(newbook,sizeof(struct book),1,fp2);fclose(fp1);fclose(fp2);函数input_book将新书的信息读入book.in中void input_books() /从键盘输入图书函数/FILE fp;struct book newbook;int a=121,n=0;if(fp=fopen(book。in,wb”))=NULL) printf(”wrong!); exit(0); while(a=121)clrscr(); n+; pri

11、ntf(New book %d:n,n); printf(”Name:”); scanf(”%s,newbook.name); printf(”Author:”); scanf(s,newbook.author); printf(Price:); scanf(f”,&newbook。price); printf(”Type:”); scanf(s,newbook.type); printf(Press:); scanf(%s”,newbook.press); newbook.isborrowed=0; strcpy(newbook。number,000000000”); fwrite(&new

12、book,sizeof(struct book),1,fp); /将图书保存到book.in中/ printf(”continue ? (y/n)”); a=getch(); clrscr(); fprintf(fp,d,n); printf(Successfully saved!nPress any key to back”); fclose(fp); save_books(); getch();函数delete_book实现对图书的删除操作void delete_book() /*删除图书函数/long int totalbook,i;int done=0;char a=y,ss10;FI

13、LE *fp1,*fp2;struct book sbook;clrscr();if((fp1=fopen(”book。out”,”rb”))=NULL)printf(”wrong!);exit(0);if(fp2=fopen(”book1。out”,wb”))=NULL)printf(wrong!); exit(0); fseek(fp1,-100L,2);fread(&sbook,sizeof(struct book),1,fp1);totalbook=atol(sbook.number); /*获得图书数目*/rewind(fp1);while(a=121)clrscr();printf

14、(Please input the number of the book:);scanf(”%s,ss);clrscr();fread(&sbook,sizeof(struct book),1,fp1);fwrite(sbook,sizeof(struct book),1,fp2);for(i=1;i=totalbook;i+=1)fread(&sbook,sizeof(struct book),1,fp1); /删除操作*/ if(strcmp(sbook。number,ss)=0) /*将删除过相应图书的数据写入 book1。out*/ done=1; printf(The informa

15、tion of the book:n); printf(”Name:%sn,sbook。name); printf(Author:%sn”,sbook。author); printf(Number:sn”,sbook。number); printf(Price:%-9。2fn”,sbook.price); printf(Press:%sn”,sbook。press); printf(”Are you sure to delete the book?(y/n) ; a=getch(); if(a!=121) break; else if(done=0) fwrite(sbook,sizeof(s

16、truct book),1,fp2); else minusone(sbook.number);fwrite(&sbook,sizeof(struct book),1,fp2);fclose(fp1);fclose(fp2);if(done=0) printf(”Wrong number!);if(done=1 & a=121) exchange(i,totalbook); clrscr(); printf(Successfully deleted!n);getch();clrscr();printf(Continue ? (y/n)”);a=getch();remove(”book1.out

17、”);void exchange(long int i,long int totalbook) /*将book1.out的数据写入book。out中*/FILE *fp1,*fp2;struct book sbook;if(fp1=fopen(book1。out,rb”)=NULL)printf(”wrong!”);exit(0);fp2=fopen(”book。out,”wb);for(i=1;i=totalbook;i+=1)fread(sbook,sizeof(struct book),1,fp1); fwrite(sbook,sizeof(struct book),1,fp2); fc

18、lose(fp1); fclose(fp2);函数search_books实现对图书的查找void search_books() /查找图书函数*/int n,total=0;long int totalbook,i;char item20;char ss=y;FILE fp;struct book sbook;if(fp=fopen(”book。out,rb)=NULL)printf(wrong!);exit(0);fseek(fp,-100L,2);fread(sbook,sizeof(struct book),1,fp);totalbook=atol(sbook。number);fsee

19、k(fp,100L,0);while(ss=121)clrscr();printf(”Please select a way to search:n”); /查找方式设置*/printf(”1:namen2:authorn3:numbern);n=getch();switch(n)case 49:clrscr();printf(”Please input the name of the book:n);scanf(%s”,item);clrscr();printf(”n);for(i=1;i=totalbook;i+=1)fread(&sbook,sizeof(struct book),1,f

20、p); if(strcmp(sbook。name,item)=0) /打印图书信息/ printf(”Book dn,total+1); printf(Name:sn,sbook。name); printf(Author:sn,sbook。author); printf(Number:sn,sbook.number); printf(Price:9。2fn”,sbook.price); printf(Press:sn”,sbook.press); printf(Type:sn,sbook。type); printf(Isborrowed:dnn”,sbook.isborrowed); tota

21、l+; continue; gotoxy(1,1);if(total=0) printf(No such book!); /没有对应图书/else printf(”We find %d books totally:,total);getch();break;case 50:clrscr();printf(”Please input the author of the book:n);scanf(s”,item);clrscr();printf(”n);for(i=1;i=totalbook;i+=1)fread(&sbook,sizeof(struct book),1,fp); if(strc

22、mp(sbook。author,item)=0) printf(”Book dn,total+1); printf(”Name:sn,sbook。name); printf(”Author:%sn,sbook.author); printf(”Number:sn”,sbook.number); printf(Price:%-9.2fn”,sbook。price); printf(Press:%sn,sbook.press); printf(”Isborrowed:dnn,sbook。isborrowed); total+; continue; gotoxy(1,1);if(total=0) p

23、rintf(”No such book!”);else printf(”We find %d books totally:”,total);getch();break;case 51:clrscr();printf(”Please input the number of the book:n);scanf(”%s”,item);clrscr();printf(”n”);for(i=1;i=totalbook;i+=1)fread(&sbook,sizeof(struct book),1,fp); if(strcmp(sbook.number,item)=0) printf(”Book %dn”

24、,total+1); printf(Name:sn,sbook。name); printf(”Author:%sn,sbook.author); printf(Number:%sn”,sbook.number); printf(Price:-9。2fn,sbook。price); printf(Press:%sn,sbook。press); printf(Isborrowed:%dnn,sbook。isborrowed); total+; continue; gotoxy(1,1);if(total=0) printf(No such book!);else printf(We find d

25、books totally:”,total);getch();break;total=0;clrscr();printf(”continue ? (y/n):n”);ss=getch(); fclose(fp);函数br_books实现对图书的借阅和归还void br_books() /*借阅或归还图书函数/int done=0;char a=y,number10;FILE *fp;long int i,totalbook;struct book sbook;if(fp=fopen(book。out,”rb+))=NULL)printf(”wrong!);exit(0);fseek(fp,10

26、0L,2);fread(&sbook,sizeof(struct book),1,fp);totalbook=atol(sbook.number);fseek(fp,100L,0);while(a=121)clrscr(); printf(”Please input a kind of service:n); printf(1:Borrow a bookn2:Return a bookn); /选择相应服务*/ a=getch(); if(a=49) clrscr(); printf(Please input the number of the book:); scanf(s,number);

27、 clrscr(); for(i=0;i=totalbook;i+=1) fread(&sbook,sizeof(struct book),1,fp); if(strcmp(sbook.number,number)=0) if(sbook.isborrowed=0) printf(The information of the book:n); printf(Name:sn,sbook.name); printf(Author:sn”,sbook。author); printf(”Number:sn,sbook。number); printf(Price:%-9。2fn,sbook。price)

28、; printf(Press:%sn,sbook。press); printf(Are you sure to borrow the book?(y/n)”) ; /*确认操作/ a=getch(); if(a=121) sbook.isborrowed=1; fseek(fp,-100L,1); fwrite(&sbook,sizeof(struct book),1,fp); fclose(fp); clrscr(); printf(”Successfully borrowed!”); else printf(”This book has been borrowed!”); fclose(f

29、p); done=1; break; if(done=0) printf(”Wrong number!);fclose(fp); if(a=50) clrscr(); printf(Please input the number of the book:”); scanf(%s,number); clrscr(); for(i=0;i=totalbook;i+=1) fread(sbook,sizeof(struct book),1,fp); if(strcmp(sbook.number,number)=0) if(sbook。isborrowed=1) printf(The informat

30、ion of the book:n”); printf(”Name:%sn”,sbook。name); printf(Author:sn”,sbook.author); printf(Number:sn”,sbook。number); printf(”Price:-9。2fn,sbook.price); printf(Press:sn,sbook。press); printf(Are you sure to return the book?(y/n)) ; a=getch(); if(a=121) sbook.isborrowed=0; fseek(fp,100L,1); fwrite(&sb

31、ook,sizeof(struct book),1,fp); fclose(fp); clrscr(); printf(Successfully returned!); else printf(”This book has not been borrowed!”); fclose(fp); done=1; break; if(done=0) printf(Wrong number!);fclose(fp); getch(); clrscr(); printf(”ncontinue ?(y/n) n); a=getch(); 函数total_numbers实现图书统计操作void total_n

32、umbers()FILE *fp;long int totalbook;long int hasb=0,typs=0,typl=0,i;struct book sbook;clrscr();if(fp=fopen(”book。out”,rb)=NULL)printf(wrong!”);exit(0);fseek(fp,-100L,2);fread(&sbook,sizeof(struct book),1,fp);totalbook=atol(sbook。number);printf(The total number of all books is:%ldn,totalbook);rewind(

33、fp);for(i=1;i=totalbook;i+=1) /统计图书类型*/fread(&sbook,sizeof(struct book),1,fp); if(strcmp(science,sbook.type)=0) typs+=1; else if(strcmp(literature,sbook。type)=0) typl+=1; if(sbook.isborrowed=1) hasb+=1; /统计借阅情况/printf(The different numbers of types:n);printf(”science:ldn”,typs);printf(”literature:%l

34、dn,typl);printf(”others:%ldnn”,totalbooktypstypl);printf(The number of books have borrowed:ldn”,hasb);printf(”The number of books have not been borrowed:%ldn”,totalbookhasb);fclose(fp);getch();5. 系统测试系统主菜单视图效果如下:对于该系统输入三本书,基本信息如下:Book1:名称:math作者:ma价格:29。4出版社:xjtup类型:scienceBook2:名称:c作者:yuyan价格:13出版社

35、:xjtup类型:scienceBook3:名称:physics作者:ren价格:25.7出版社:xjtup类型:science图书统计情况:查找名称为c的图书删除第一本书和借阅第二本书后的统计情况:总结:通过一系列的测试可发现该系统可以基本满足客户的各种需求,同时输入输出界面比较简洁,是一个比较成功的系统,但同时也存在一些问题需要改进,主要有以下几个方面:1. 使用scanf输入不能输入带有空格的字符串2. 在使用编号进行图书查询时会发生一些错误,可能会出现查找图书多余实际图书的情况。6. 结束语 C语言作为一门应用广泛的计算机语言,在今天依然有着极为重要的作用。因此,掌握C语言是一项基本的要求。本次的图书管理系统重点考查了文件的读写,结构体的应用以及各种基本语句的使用。本次综合性的实践使我在提高编程能力的同时得到了锻炼,多次的调试使我增加上机经验,进一步熟悉了C语言的语法和基本语句,在本次实践中,我得到了老师和同学的帮助,特在此表示感谢。参考书目梁力,原盛等.程序设计与C语言.第二版:西安交通大学出版社 2005

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服