当前位置:文档之家› 图书管理系统 (c语言)

图书管理系统 (c语言)

程序设计方法课程设计问题提出:设计一个图书馆的图书管理,具体要求如下:1.录入新采购的图书信息,做成文件book.in,要求有良好的输入界面;2.查询各种图书的基本信息,设计出良好的查询界面;3.按图书编号进行排序,将排好序的图书信息输出到文件book.out中。

4.统计各类图书册数、图书馆的图书总册数。

5.统计图书的借阅情况。

1.需求分析对于图书馆的管理系统,应该满足以下的用户需求:○1用户能够增加和删除一些图书。

○2用户根据一些基本信息查询相应的图书,系统可以准确输出所有符合条件的图书信息。

○3用户可以借阅和归还图书馆中的图书。

○4系统可以对图书做出统计,包括图书总数,各类图书数目,图书的借阅情况。

2.设计分析定义图书结构体,其成员包括书名、作者、类型、出版社、价格、是否借阅(整型,1为借出,0为未借)、编号。

输入新书时,利用结构体储存书的信息,同时将新书信息以二进制写入book.in文件中,录入结束后,将新书按输入顺序写入book.out中,并且以原来存书的最大编号为基准对新书进行编号,这样可以保证book.out文件存储的图书都是按编号进行排序的。

查找图书时,获得所查书目基本信息后,以二进制依次读取文件中的每一本图书信息,并与输入信息相比较,输出符合信息的图书。

删除图书时,通过输入的图书编号(编号可通过查找图书获得)查找相应图书,删除相应数据并保存。

借阅或归还图书时,通过图书编号(编号可通过查找图书获得)查找相应图书,修改图书的属性(是否借阅)并保存。

实现统计功能时,以二进制依次读取文件中的每一本图书信息,设置变量统计图书类型、借阅情况和图书总数,到文件结尾时,输出各个变量的值。

3.系统设计(流程图表示)否否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(&standard,sizeof(struct book),1,fp);}fclose(fp);for(i=0;i<=14;i++) /*欢迎界面设置*/{ clrscr();gotoxy(18,25-i);printf("Welcome to Liberary Management System");delay(300000000000);}sleep(1);gotoxy(18,27-i);printf("Press any key to continue");getch();clrscr();while(a!=48) /*打印主菜单*/ { gotoxy(13,5);printf("***********************************************");gotoxy(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==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(ss[8]=='9'){ss[8]='0';ss[7]=ss[7]+1;}else ss[8]=ss[8]+1;for(i=7;i>=0;i--)if(ss[i]==58) {ss[i]='0';ss[i-1]++;} }void minusone(char ss[]) /*图书编号减一*/ {int i;if(ss[8]=='0'){ss[8]='9';ss[7]=ss[7]-1;}else ss[8]=ss[8]-1;for(i=7;i>=0;i--)if(ss[i]==47) {ss[i]='9';ss[i-1]--;} }函数save_books()将book.in的数据写入book.out中void save_books() /*保存图书*/ {FILE *fp1,*fp2;struct book oldbook,newbook;char ssl[10];int n,i;if((fp2=fopen("book.out","rb+"))==NULL){printf("wrong!");exit(0);}if((fp1=fopen("book.in","rb"))==NULL){printf("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);addone(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++;printf("New book %d:\n",n);printf("Name:");scanf("%s",);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(&newbook,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',ss[10];FILE *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("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 information of the book:\n");printf("Name:%s\n",);printf("Author:%s\n",sbook.author);printf("Number:%s\n",sbook.number);printf("Price:%-9.2f\n",sbook.price);printf("Press:%s\n",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(struct 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");}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);}fclose(fp1);fclose(fp2);}函数search_books实现对图书的查找void search_books() /*查找图书函数*/{int n,total=0;long int totalbook,i;char item[20];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);fseek(fp,100L,0);while(ss==121){clrscr();printf("Please select a way to search:\n"); /*查找方式设置*/ printf("1:name\n2:author\n3:number\n");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,fp);if(strcmp(,item)==0) /*打印图书信息*/ {printf("Book %d\n",total+1);printf("Name:%s\n",);printf("Author:%s\n",sbook.author);printf("Number:%s\n",sbook.number);printf("Price:%-9.2f\n",sbook.price);printf("Press:%s\n",sbook.press);printf("Type:%s\n",sbook.type);printf("Isborrowed:%d\n\n",sbook.isborrowed);total++;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(strcmp(sbook.author,item)==0) {printf("Book %d\n",total+1);printf("Name:%s\n",);printf("Author:%s\n",sbook.author);printf("Number:%s\n",sbook.number);printf("Price:%-9.2f\n",sbook.price);printf("Press:%s\n",sbook.press);printf("Isborrowed:%d\n\n",sbook.isborrowed);total++;continue;}}gotoxy(1,1);if(total==0) printf("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 %d\n",total+1);printf("Name:%s\n",);printf("Author:%s\n",sbook.author);printf("Number:%s\n",sbook.number);printf("Price:%-9.2f\n",sbook.price);printf("Press:%s\n",sbook.press);printf("Isborrowed:%d\n\n",sbook.isborrowed);total++;continue;}}gotoxy(1,1);if(total==0) printf("No such book!");else printf("We find %d 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',number[10];FILE *fp;long int i,totalbook;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);fseek(fp,100L,0);while(a==121){clrscr();printf("Please input a kind of service:\n");printf("1:Borrow a book\n2:Return a book\n"); /*选择相应服务*/ a=getch();if(a==49){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==0) {printf("The information of the book:\n");printf("Name:%s\n",);printf("Author:%s\n",sbook.author);printf("Number:%s\n",sbook.number);printf("Price:%-9.2f\n",sbook.price);printf("Press:%s\n",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(fp);}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 information of the book:\n");printf("Name:%s\n",);printf("Author:%s\n",sbook.author);printf("Number:%s\n",sbook.number);printf("Price:%-9.2f\n",sbook.price);printf("Press:%s\n",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(&sbook,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_numbers(){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:%ld\n",totalbook);rewind(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:%ld\n",typs);printf("literature:%ld\n",typl);printf("others:%ld\n\n",totalbook-typs-typl);printf("The number of books have borrowed:%ld\n",hasb);printf("The number of books have not been borrowed:%ld\n",totalbook-hasb);fclose(fp);getch();}5.系统测试系统主菜单视图效果如下:对于该系统输入三本书,基本信息如下:Book1:名称:math作者:ma价格:29.4出版社:xjtup类型:scienceBook2:名称:c作者:yuyan价格:13出版社:xjtup类型:scienceBook3:名称:physics作者:ren价格:25.7出版社:xjtup类型:science图书统计情况:查找名称为c的图书删除第一本书和借阅第二本书后的统计情况:总结:通过一系列的测试可发现该系统可以基本满足客户的各种需求,同时输入输出界面比较简洁,是一个比较成功的系统,但同时也存在一些问题需要改进,主要有以下几个方面:1.使用scanf输入不能输入带有空格的字符串2.在使用编号进行图书查询时会发生一些错误,可能会出现查找图书多余实际图书的情况。

相关主题