c语言图书管理系统结课作品.docx
《c语言图书管理系统结课作品.docx》由会员分享,可在线阅读,更多相关《c语言图书管理系统结课作品.docx(24页珍藏版)》请在冰豆网上搜索。
c语言图书管理系统结课作品
#include
#include
#include
#include//控制台输入输出函数,这个程序中主要使用了getch()函数
typedefstructtushu//给结构体起别名
{
charname[20];//书本名称
charnumber[20];//书本编号
charwriter[10];//书本作者
charcategory[10];//所属类别
charpublisher[20];//出版社
chartime[20];//出版时间
doubleprice;//书本价格
}BOOK;
BOOKa;//定义一个全局变量,一般情况下把全局变量定义在第一个函数前面,这里主要用于录入和追加
intbooknum;
voidinput()//定义录入函数
{
inti,n;
printf("请输入要输入的书籍数目!
\n");
scanf("%d",&n);
FILE*fp;
FILE*num_fp;
system("cls");
if((fp=fopen("c:
\\file","w"))==NULL)
{
printf("error!
\n");
exit
(1);
}
if((num_fp=fopen("c:
\\booknum","w"))==NULL)
{
printf("error!
\n");
exit
(1);
}
booknum=0;
for(i=1;i<=n;i++)
{
printf("请输入第%d本书名:
",i);
scanf("%s",&a.name);
printf("请输入第%d本图书编号:
",i);
scanf("%s",&a.number);
printf("请输入第%d本作者名:
",i);
scanf("%s",&a.writer);
printf("请输入第%d本图书的所属类别:
",i);
scanf("%s",&a.category);
printf("请输入第%d本出版社:
",i);
scanf("%s",&a.publisher);
printf("请输入第%d本出版时间:
",i);
scanf("%s",&a.time);
printf("请输入第%d本书的价格:
",i);
scanf("%lf",&a.price);
fwrite(&a,sizeof(BOOK),1,fp);
booknum++;
}
fwrite(&booknum,sizeof(int),1,num_fp);
fclose(num_fp);
fclose(fp);
printf("\t\t祝贺你信息录入成功!
\n");
printf("\t\t按任意键返回主界面\n");
getch();//位于conio.h头文件里,会等待你按下任意键,再继续执行下面的语句
}
voidlist()//定义浏览函数,按价格排序输出
{
FILE*fp;
inti,j,index;
BOOKb[888];
BOOKc;
system("cls");
if((fp=fopen("c:
\\file","r"))==NULL)
{
printf("error!
\n");
getch();
exit
(1);
}
printf("\n\n\n\n\n\n书名\t图书编号\t作者名\t所属类别\t出版社\t出版时间\t价格\n\n");
FILE*num_fp;
if((num_fp=fopen("c:
\\booknum","r"))==NULL)
{
printf("error!
\n");
getch();
exit
(1);
}
fread(&booknum,sizeof(int),1,num_fp);
fclose(num_fp);
for(i=0;ifread(&b[i],sizeof(BOOK),1,fp);
for(i=0;iindex=i;
for(j=i+1;jif(b[j].price
index=j;
c=b[index];
b[index]=b[i];
b[i]=c;}
for(i=0;iprintf("%s\t%s\t%s\t%s\t%s\t%s\t%f\n",b[i].name,b[i].number,b[i].writer,b[i].category,b[i].publisher,b[i].time,b[i].price);
fclose(fp);
printf("按任意键返回主界面");
getch();
}
voidsearch_by_name()/*定义按书名查询*/
{
inti;
intflag=0;
chars2[20];
FILE*fp;
FILE*num_fp;
if((num_fp=fopen("c:
\\booknum","r"))==NULL)
{
printf("error!
\n");
getch();
exit
(1);
}
fread(&booknum,sizeof(int),1,num_fp);
BOOKb[888];
if((fp=fopen("c:
\\file","r"))==NULL)
{
printf("fileopenerror!
!
\n");
exit
(1);
}
for(i=0;ifread(&b[i],sizeof(BOOK),1,fp);
printf("请输入要查询的书名:
");
scanf("%s",s2);
for(i=0;i{
if(strcmp(s2,b[i].name)==0)
{
flag=1;
printf("书名编号作者类别出版社出版时间价格\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\t%lf\t\n\n",b[i].name,b[i].number,b[i].writer,b[i].category,b[i].publisher,b[i].time,b[i].price);
}
}
if(flag==0)
{
printf("对不起,您查询的图书不存在!
");
getch();
}
printf("按任意键返回主界面");
getch();
fclose(num_fp);
fclose(fp);
}
voidsearch_by_writer()/*定义按作者名查询*/
{
inti;
intflag=0;
chars2[20];
FILE*fp;
FILE*num_fp;
if((num_fp=fopen("c:
\\booknum","r"))==NULL)
{
printf("error!
\n");
getch();
exit
(1);
}
fread(&booknum,sizeof(int),1,num_fp);
BOOKb[888];
if((fp=fopen("c:
\\file","r"))==NULL)
{
printf("fileopenerror!
!
\n");
exit
(1);
}
for(i=0;ifread(&b[i],sizeof(BOOK),1,fp);
printf("请输入要查询的书的作者:
");
scanf("%s",s2);
for(i=0;i{
if(strcmp(s2,b[i].writer)==0)
{
flag=1;
printf("书名编号作者类别出版社出版时间价格\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\t%lf\t\n\n",b[i].name,b[i].number,b[i].writer,b[i].category,b[i].publisher,b[i].time,b[i].price);
}
}
if(flag==0)
{
printf("对不起,您查询的图书不存在!
");
getch();
}
printf("按任意键返回主界面");
getch();
fclose(num_fp);
fclose(fp);
}
voidsearch_by_publisher()/*定义按出版社查询*/
{
inti;
intflag=0;
chars2[20];
FILE*fp;
FILE*num_fp;
if((num_fp=fopen("c:
\\booknum","r"))==NULL)
{
printf("error!
\n");
getch();
exit
(1);
}
fread(&booknum,sizeof(int),1,num_fp);
BOOKb[888];
if((fp=fopen("c:
\\file","r"))==NULL)
{
printf("fileopenerror!
!
\n");
exit
(1);
}
for(i=0;ifread(&b[i],sizeof(BOOK),1,fp);
printf("请输入要查询的书的出版社:
");
scanf("%s",s2);
for(i=0;i{
if(strcmp(s2,b[i].publisher)==0)
{
flag=1;
printf("书名编号作者类别出版社出版时间价格\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\t%lf\t\n\n",b[i].name,b[i].number,b[i].writer,b[i].category,b[i].publisher,b[i].time,b[i].price);
}
}
if(flag==0)
{
printf("对不起,您查询的图书不存在!
\n");
getch();
}
printf("按任意键返回主界面");
getch();
fclose(num_fp);
fclose(fp);
}
voidsearch_by_category()/*定义按图书类别查询*/
{
inti;
intflag=0;
chars2[20];
FILE*fp;
FILE*num_fp;
if((num_fp=fopen("c:
\\booknum","r"))==NULL)
{
printf("error!
\n");
getch();
exit
(1);
}
fread(&booknum,sizeof(int),1,num_fp);
BOOKb[888];
if((fp=fopen("c:
\\file","r"))==NULL)
{
printf("fileopenerror!
!
\n");
exit
(1);
}
for(i=0;ifread(&b[i],sizeof(BOOK),1,fp);
printf("请输入要查询的图书类别:
");
scanf("%s",s2);
for(i=0;i{
if(strcmp(s2,b[i].category)==0)
{
flag=1;
printf("书名编号作者类别出版社出版时间价格\n");
printf("%s\t%s\t%s\t%s\t%s\t%s\t%lf\t\n\n",b[i].name,b[i].number,b[i].writer,b[i].category,b[i].publisher,b[i].time,b[i].price);
}
}
if(flag==0)
{
printf("对不起,您查询的图书不存在!
");
getch();
}
printf("按任意键返回主界面");
getch();
fclose(num_fp);
fclose(fp);
}
voidsearch()/*定义查询函数*/
{
intchax;
system("cls");/*进行清屏*/
printf("※※※※※※※※※※※※※※※※※※※※\n");
printf("※★开始查询★※\n");
printf("※★1--按书名查询★※\n");
printf("※★2--按作者名查询★※\n");
printf("※★3--按出版社查询★※\n");
printf("※★4--按图书类别查询★※\n");
printf("※★0--退出★※\n");
printf("※※※※※※※※※※※※※※※※※※※※\n");
printf("请选择你想要的查询方式:
");
scanf("%d",&chax);
switch(chax)
{
case1:
search_by_name();break;
case2:
search_by_writer();break;
case3:
search_by_publisher();break;
case4:
search_by_category();break;
case0:
return;
default:
printf("\n对不起,您输入的序号不对\n");
printf("按任意键返回主界面");
getch();
}
}
voidrevise()/*定义修改函数*/
{
inti;
chars1[20];
FILE*fp;
FILE*num_fp;
if((num_fp=fopen("c:
\\booknum","r"))==NULL)
{
printf("error!
\n");
getch();
exit
(1);
}
fread(&booknum,sizeof(int),1,num_fp);
if((fp=fopen("c:
\\file","r"))==NULL)
{
printf("error!
");
exit
(1);
}
system("cls");
BOOKb[888];
i=0;
for(i=0;i{
fread(&b[i],sizeof(BOOK),1,fp);
}
fclose(fp);
fclose(num_fp);
printf("※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("※※\n");
printf("※欢迎进入修改界面※\n");
printf("※※\n");
printf("※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("图书情况如下:
\n");
printf("※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("书名编号作者类别出版社出版时间价格\n");
for(i=0;iprintf("%s\t%s\t%s\t%s\t%s\t%s\t%lf\t\n\n",b[i].name,b[i].number,b[i].writer,b[i].category,b[i].publisher,b[i].time,b[i].price);
printf("请输入你要修改的图书编号:
");
getchar();
gets(s1);
for(i=0;i{
if(strcmp(s1,b[i].number)==0)
{
break;
}
}
if(i>=booknum)
{
printf("\t\t未找到!
");
system("PAUSE");
}
else
{
printf("书籍%s的相关信息如下:
\n",b[i].number);
printf("%s\t%s\t%s\t%s\t%s\t%s\t%lf\t\n\n",b[i].name,b[i].number,b[i].writer,b[i].category,b[i].publisher,b[i].time,b[i].price);
printf("请输入修改后的图书信息\n");
printf("书名编号作者类别出版社出版时间价格\n");
scanf("%s",&b[i].name);
scanf("%s",&b[i].number);
scanf("%s",&b[i].writer);
scanf("%s",&b[i].category);
scanf("%s",&b[i].publisher);
scanf("%s",&b[i].time);
scanf("%lf",&b[i].price);
if((fp=fopen("c:
\\file","w+"))==NULL)
{
printf("\t\terror!
");
exit
(1);
}
for(i=0;ifwrite(&b[i],sizeof(BOOK),1,fp);
printf("\t\t恭喜你修改成功!
");
fclose(fp);
}
getch();
printf("按任意键返回主界面");
}
voidappend()/*定义追加函数*/
{
inti,n;
FILE*fp;
FILE*num_fp;
if((num_fp=fopen("c:
\\booknum","r"))==NULL)
{
printf("terror!
\n");
getch();
exit
(1);
}
fread(&booknum,sizeof(int),1,num_fp);
fclose(num_fp);
if((fp=fopen("c:
\\file","a"))==NULL)
{
printf("error!
\n");
exit
(1);
}
system("cls");
printf("请输入追加图书的个数:
");
scanf("%d",&n);
for(i=0;i{
printf("请输入追加图书书名:
");
scanf("%s",&a.name);
printf("请输入追加图书的编号:
");
scanf("%s",&a.number);
printf("请输入追加图书的作者名:
");
scanf("%s",&a.writer);
printf("请输入追加图书的所属类别:
");
scanf("%s",&a.category);
printf("请输入追加图书出版社:
");
scanf("%s",&a.publisher);
printf("请输入追加图书出版时间:
");
scanf("%s",&a.time);
printf("请输入追加图书的价格:
");
scanf("%lf",&a.price);
fwrite(&a,sizeof(BOOK),1,fp);
booknum++;
}
if((num_fp=fopen("c:
\\booknum","w"))==NULL)
{
printf("error!
\n");
getch();
exit
(1);
}
fwrite(&booknum,sizeof(int),1,num_fp);
fclose(num_fp);
fclose(fp);
printf("追加成功!
\n");
getch();
printf("按任意键返回主界面");
}
voidshanchu()
{
inti,t;
charn[20];
FILE*fp,*fp1;
FILE*num_fp;
if((num_fp=fopen("c:
\\booknum","r"))==NULL)
{
printf("error!
\n");
getch();
exit
(1);
}
fread(&booknum,sizeof(int),1,num_fp);
if((fp=fopen("c:
\\file","r"))==NULL)
{
printf("error!
");
exit
(1);
}
if((fp1=fopen("c:
\\file1","w"))==NULL)
{
printf("error!
");
exit
(1);
}
system("cls");
BOOKb[888];
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("※书名编号作者类别出版社出版时间价格※\n");
printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
for(i=0;i{
fread(&b[i],sizeof(BOOK),1,fp);
printf("%s\t%s\t%s\t%s\t%s\t%s\t%lf\t\n\n",b[i].name,b[i].number,b[i].writer,b[i].category,b[i].publisher,b[i].time,b[i].price);
}
printf("欢迎来到图书删除界面,请输入你想删除的图书编号:
\n");
scanf("%s",n);
t=0;
for(i=0;i{
if(strcmp(b[i].number,n)!
=0)
fwrite(&b[i],sizeof(BOO