2024年1月14日发(作者:)

程序设计方法课程设计

问题提出:

设计一个图书馆的图书管理,具体要求如下:

1.录入新采购的图书信息,做成文件book。in,要求有良好的输入界面;

2.查询各种图书的基本信息,设计出良好的查询界面;

3.按图书编号进行排序,将排好序的图书信息输出到文件

中.

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

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

1. 需求分析

对于图书馆的管理系统,应该满足以下的用户需求:

○,1用户能够增加和删除一些图书。

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

错误!用户可以借阅和归还图书馆中的图书。

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

2. 设计分析

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

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

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

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

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

实现统计功能时,以二进制依次读取文件中的每一本图书信

息,设置变量统计图书类型、借阅情况和图书总数,到文件结尾时,输出各个变量的值。

3. 系统设计(流程图表示)

开始

欢迎界面

打印主菜单

输入选择n

n==1

输入新书信息

将新书保存至

将中图书按编号保存至

n==2

选择查找方式

输入查找图书信息

在中能找到相应图书

输出图书信息 出错提示

n==3

输入删除图书信息(编号)

在中能找到相应图书

出错提示

删除相应图书

n==4

选择借书或还书操作

输入图书信息(编号)

在中能找到相应图书

对图书相应属性进行修改 出错提示

n==5

统计中总书数并输出

统计各类型书数并输出

统计图书借阅情况并输出

n==0

结束

4. 程序代码

主函数

main()

{int i,a;

Struct book standard={"a”,”a”,"000000000”,0,0,"a","others”};

FILE *fp;

if((fp=fopen("","r"))==NULL)

{if((fp=fopen("","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(3);

}

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(”",”ab”))==NULL)

{printf(”wrong!”);

exit(0);}

for(i=1;i<=n;i++)

{

fread(&newbook,sizeof(struct book),1,fp1);

addone(ssl);

strcpy(,ssl); /*为新图书编号*/

fwrite(&newbook,sizeof(struct book),1,fp2);

}

fclose(fp1);

fclose(fp2);

}

函数input_book将新书的信息读入中

void input_books() /*从键盘输入图书函数*/

{FILE *fp;

struct book newbook;

int a=121,n=0;

if((fp=fopen("","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",);

owed=0;

strcpy(,”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("","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(,ss)==0) /*将删除过相应图书的数据写入

*/

{done=1;

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",);

printf("Press:%sn",);

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) /*将的数据写入中*/

{FILE *fp1,*fp2;

struct book sbook;

if((fp1=fopen("”,"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(”",”rb"))==NULL)

{printf(”wrong!”);

exit(0);

fseek(fp,-100L,2);

fread(&sbook,sizeof(struct book),1,fp);

totalbook=atol();

fseek(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,fp);

if(strcmp(,item)==0) /*打印图书信息*/

{

printf(”Book %dn”,total+1);

printf(”Name:%sn”,sbook。name);

printf(”Author:%sn”,);

printf(”Number:%sn",sbook。number);

printf("Price:%—9.2fn",sbook。price);

printf(”Press:%sn",sbook。press);

printf("Type:%sn",);

printf("Isborrowed:%dnn",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(,item)==0) {

printf("Book %dn",total+1);

printf("Name:%sn",sbook。name);

printf(”Author:%sn",sbook。author);

printf("Number:%sn",);

printf(”Price:%—9.2fn”,);

printf("Press:%sn",sbook。press);

printf("Isborrowed:%dnn”,owed);

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(,item)==0) {

printf(”Book %dn”,total+1);

printf("Name:%sn",);

printf("Author:%sn”,);

printf(”Number:%sn”,);

printf("Price:%—9。2fn”,);

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 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 bookn2:Return a bookn”); /*选择相应服务*/

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(,number)==0)

{if(owed==0) {

printf(”The information of the book:n");

printf("Name:%sn”,sbook。name);

printf(”Author:%sn”,);

printf(”Number:%sn”,sbook。number);

printf("Price:%-9。2fn”,sbook。price);

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(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(,number)==0)

{if(sbook。isborrowed==1) {

printf("The information of the book:n");

printf("Name:%sn”,);

printf("Author:%sn",sbook。author);

printf(”Number:%sn”,sbook。number);

printf(”Price:%-9。2fn”,);

printf("Press:%sn”,);

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();

printf(”The total number of all books is:%ldn",totalbook);

rewind(fp);

for(i=1;i<=totalbook;i+=1) /*统计图书类型*/

{fread(&sbook,sizeof(struct book),1,fp);

if(strcmp(”science”,)==0)

typs+=1;

else if(strcmp("literature",sbook。type)==0)

typl+=1;

if(owed==1) hasb+=1;} /*统计借阅情况*/

printf("The different numbers of types:n”);

printf(”science:%ldn”,typs);

printf("literature:%ldn”,typl);

printf("others:%ldnn",totalbook—typs-typl);

printf("The number of books have borrowed:%ldn",hasb);

printf("The number of books have not been

borrowed:%ldn",totalbook-hasb);

fclose(fp);

getch();

}

5. 系统测试

系统主菜单视图效果如下:

对于该系统输入三本书,基本信息如下:

Book1:

名称:math

作者:ma

价格:29。4

出版社:xjtup

类型:science

Book2:

名称:c

作者:yuyan

价格:13

出版社:xjtup

类型:science

Book3:

名称:physics

作者:ren

价格:25.7

出版社:xjtup

类型:science

图书统计情况:

查找名称为c的图书

删除第一本书和借阅第二本书后的统计情况:

总结:通过一系列的测试可发现该系统可以基本满足客户的各种需求,同时输入输出界面比较简洁,是一个比较成功的系统,但同时也存在一些问题需要改进,主要有以下几个方面:

1. 使用scanf输入不能输入带有空格的字符串

2. 在使用编号进行图书查询时会发生一些错误,可能会出现查找图书多余实际图书的情况.

6. 结束语

C语言作为一门应用广泛的计算机语言,在今天依然有着极为重要的作用。因此,掌握C语言是一项基本的要求。本次的图书管理系统重点考查了文件的读写,结构体的应用以及各种基本语句的使用。

本次综合性的实践使我在提高编程能力的同时得到了锻炼,多次

的调试使我增加上机经验,进一步熟悉了C语言的语法和基本语句,在本次实践中,我得到了老师和同学的帮助,特在此表示感谢。

参考书目

梁力,原盛等。程序设计与C语言。第二版:西安交通大学出版社 2005