C-关于C语言编简单图书信息管理程序

发布于 2017-01-07 06:54:39 字数 17265 浏览 1293 评论 1

我的程序不知道为什么在第一个自定义函数第二次以后打开文件的时候用不了,代码还没有改完,就先看下入库函数吧!
题目:图书管理
图书信息包括:登录号、书名、作者名、分类号、出版单位、出版时间、价格、库存量等。试设计一图书信息管理系统,使之能提供以下功能:
系统以菜单方式工作
(1)采编入库:新购入一本书,经分类和确定书号后登记到图书账目中去,若该书已存在,这库存量增加
(2)借阅:若 一种书的现存量大于0,则借出一本,登记借阅者的图书证号和归还期限
(3)归还:注销借阅者的登记,改变该书的现存量(过期罚款-选作)
(4)清除库存:某书已吴保留价值,将它从图书账目中注销
(5)查询功能:-- 按书名查询 按作者名查询

代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//时间结构自定义
struct time
{
int year;
int mon;
int day;
};

//图书信息的结构定义
struct book_information
{
char registry_number[20];//登录号
char book_name[40];//书名
char author[40];//作者名
char classification;//分类
char published_by[40];//出版单位
char publication[10];//出版时间
double book_price;//价格
int inventory;//库存
int lending_days;//可借阅天数
char reader_information[7];//读者信息
struct time borrowtime;//借书时间
struct time limretime;//规定还书时间
struct time returningtime;//实际还书时间
}newbook,book[10];

//读者信息自定义结构
struct reader_information
{
char readernumber[7];
struct book_information rebook;
}reader[10];

int n=0;//读者数量

void storage(struct book_information newbook,struct book_information book[]);
void lending(struct book_information book[],struct reader_information reader[]);
void returning(struct book_information book[],struct reader_information reader[]);
void remove(struct book_information book[]);
void finding(struct book_information book[]);

/*主函数*/
int main(void)
{
int choice,k;
char input;

for(k=0;k<10;k++)
{
book[k].inventory=0;
strcpy(reader[k].readernumber,"x00000");
}


/*打印提示,图形化界面,未处理*/
//显示选择操作
//(1-入库,2-借阅,3-归还,4-清除,5-查询,0-退出)
do
{
loop:printf("欢迎使用图书管理系统n");
printf("1-入库,2-借阅,3-归还,4-清除,5-查询,6-退出n");
printf("请输入选择操作:");//输入选择
scanf("%d",&choice);
if(choice!=1&&choice!=2&&choice!=3&&choice!=4&&choice!=5&&choice!=6)
{
printf("输入错误,请重新选择操作!n(任意键返回)n");
input=getchar();
}
}
while(choice!=1&&choice!=2&&choice!=3&&choice!=4&&choice!=5&&choice!=6);


switch(choice)
{
case 1:storage(newbook,book);goto loop;//函数的调用,未处理
case 2:lending(book,reader);goto loop;
case 3:returning(book,reader);goto loop;
case 4:remove(book);goto loop;
case 5:finding(book);goto loop;
case 6:break;
}

return 0;
}

/*图书入库函数*/
void storage(struct book_information newbook,struct book_information book[])
{
FILE *fpbook;
int choice1,k,i=0,j=0,flag;
if((fpbook=fopen("bookinformation.txt","r"))==NULL)
fpbook=fopen("bookinformation.txt","w");
else
{
fclose(fpbook);
fpbook=fopen("bookinformation.txt","a");
}

while(!feof(fpbook))
{
fscanf(fpbook,"%s%s%s%c%s%s%lf%d",book[i].registry_number,
book[i].book_name,
book[i].author,
book[i].classification,
book[i].published_by,
book[i].publication,
&book[i].book_price,
&book[i].lending_days);
i++;
}
//图形界面设置,出现选择项
printf("请选择操作:1-入库,0-返回n");
scanf("%d",&choice1);//选择操作
switch(choice1)
{
case 1:
while(choice1==1)
{


flag=0;
printf("请输入图书信息:n");
printf("登录号:");
scanf("%s",newbook.registry_number);
printf("书名:");
scanf("%s",newbook.book_name);
printf("作者名:");
scanf("%s",newbook.author);
printf("分类号:");
getchar();
newbook.classification=getchar();
printf("出版单位:");
scanf("%s",newbook.published_by);
printf("出版时间:");
scanf("%s",newbook.publication);
printf("价格:");
scanf("%lf",&newbook.book_price);
printf("可借阅天数:");
scanf("%d",&newbook.lending_days);

for(k=1;k<=i;k++)//判断书是否相同
if(strcmp(book[k].registry_number,newbook.registry_number)==0)//用登录号判断
{
flag=1;
break;
}
if(flag==1)
book[k].inventory++;
else
{
i++;
strcpy(book[i].registry_number,newbook.registry_number);
strcpy(book[i].book_name,newbook.book_name);
strcpy(book[i].author,newbook.author);
book[i].classification=newbook.classification;
strcpy(book[i].published_by,newbook.published_by);
strcpy(book[i].publication,newbook.publication);
book[i].book_price=newbook.book_price;
book[i].lending_days=newbook.lending_days;
book[i].inventory++;

}

printf("是否继续?1-是 0-否n");
scanf("%d",&choice1);
}
case 0:break;
}

for(j=1;j<i;j++)
{

fprintf(fpbook,"%sn",book[j].registry_number);
fprintf(fpbook,"%sn",book[j].book_name);
fprintf(fpbook,"%sn",book[j].author);
fprintf(fpbook,"%cn",book[j].classification);
fprintf(fpbook,"%sn",book[j].published_by);
fprintf(fpbook,"%sn",book[j].publication);
fprintf(fpbook,"%lfn",book[j].book_price);
fprintf(fpbook,"%dn",book[j].lending_days);
fprintf(fpbook,"%dnn",book[i].inventory++);

}
fclose(fpbook);
}




/*借阅函数*/
void lending(struct book_information book[],struct reader_information reader[])
{
int choice2,k,j=0,i=0;
char number[40];
FILE *fpreader;
FILE *fpbook;
if((fpbook=fopen("d://bookinformation.dat","wb+"))==NULL)
{
printf("File open error!n");
exit(0);
}
while(!feof(fpbook))
{
fread(&book[0],sizeof(struct book_information),1,fpbook);
i++;
}

if((fpreader=fopen("d://readerinformation.dat","wb+"))==NULL)
{
printf("File open error!n");
exit(0);
}
while(!feof(fpreader))
fread(&reader[0],sizeof(struct reader_information),1,fpreader);


//表头未实现
printf("选择操作:1-借阅 0-返回主界面n");//1-借阅 0-返回主界面
scanf("%d",&choice2);
switch(choice2)
{
case 1:
printf("输入图书登录号:");
scanf("%s",number);
for(k=0;k<i;k++)
{
if(strcmp(book[k].registry_number,number)==0)
{
if(book[k].inventory>0)//库存不为0
{
printf("可借!n输入读者信息:");
scanf("%s",reader[n].readernumber);
n++;
strcpy(reader[n].rebook.book_name,book[k].book_name);
book[k].inventory--;
printf("借出时间为(年 月 日):");
scanf("%d%d%d",&book[k].borrowtime.year,
&book[k].borrowtime.mon,
&book[k].borrowtime.day);
book[k].limretime.day=book[k].borrowtime.day+book[k].lending_days;
book[k].limretime.mon=book[k].borrowtime.mon;
book[k].limretime.year=book[k].borrowtime.year;
while(book[k].limretime.day>30)
{
book[k].limretime.day=book[k].limretime.day-30;
book[k].limretime.mon=book[k].limretime.mon+1;
while(book[k].limretime.mon>12)
{
book[k].limretime.mon=book[k].limretime.mon-12;
book[k].limretime.year=book[k].limretime.year+1;
}
}

printf("归还时间为:%d年%d月%d日n",book[k].limretime.year,
book[k].limretime.mon,
book[k].limretime.day);

break;
}
else
{
printf("书已借出,无库存!n");
break;
}
}
}
break;

case 0:break;
}
while(!feof(fpreader))
fwrite(&reader[0],sizeof(struct reader_information),1,fpreader);
while(!feof(fpbook))
fwrite(&book[0],sizeof(struct book_information),1,fpbook);
fclose(fpbook);
fclose(fpreader);
}

/*归还函数*/
void returning(struct book_information book[],struct reader_information reader[])
{
int choice3,k=0,j=0,i=0;
char booknumber[40];

FILE *fpreader;
FILE *fpbook;
if((fpbook=fopen("d://bookinformation.dat","ab+"))==NULL)
{
printf("File open error!n");
exit(0);
}
while(!feof(fpbook))
{
fread(&book[0],sizeof(struct book_information),1,fpbook);
i++;
}
if((fpreader=fopen("d://readerinformation.dat","ab+"))==NULL)
{
printf("File open error!n");
exit(0);
}
while(!feof(fpreader))
fread(&reader[0],sizeof(struct reader_information),1,fpreader);

printf("选择操作:1-归还 0-返回主菜单n");//(选择操作1-归还 0-返回主菜单)
scanf("%d",&choice3);
switch(choice3)
{
case 1:
printf("输入图书登录号:");
scanf("%s",booknumber);
for(k=0;k<=i;k++)
{
if(strcmp(booknumber,book[k].registry_number)==0)
{
book[k].inventory++;
for(j=1;j<=n;j++)
if(reader[j].rebook.registry_number,booknumber)
strcpy(reader[j].readernumber,"x00000");

}
printf("输入归还时间:(年 月 日)");
scanf("%d%d%d",&book[k].returningtime.year,
&book[k].returningtime.mon,
&book[k].returningtime.day);

break;
}
case 0:break;
}
while(!feof(fpreader))
fwrite(&reader[0],sizeof(struct reader_information),1,fpreader);
while(!feof(fpbook))
fwrite(&book[0],sizeof(struct book_information),1,fpbook);
fclose(fpbook);
fclose(fpreader);

}

/*清除函数*/
void remove(struct book_information book[])
{
FILE *fpbook;
int choice4,k=0,i=0,j=0;
char number[20];
if((fpbook=fopen("d://bookinformation.dat","ab+"))==NULL)
{
printf("File open error!n");
exit(0);
}
while(!feof(fpbook))
{
fread(&book[0],sizeof(struct book_information),1,fpbook);
i++;
}


//显示选择操作1-清除 0-返回主界面
printf("选择操作:1-清除 0-返回主界面n");
scanf("%d",&choice4);
switch(choice4)
{
case 1:
printf("输入图书登录号:");
scanf("%s",number);
for(k=0;k<=i;k++)
{
if(strcmp(book[k].registry_number,number)==0)
{
book[k].inventory--;
for(j=k;j<i;j++)
book[j]=book[j+1];
break;
}
}

case 0:break;
}
while(!feof(fpbook))
fwrite(&book[0],sizeof(struct book_information),1,fpbook);
fclose(fpbook);
}

/*查找函数*/
void finding(struct book_information book[])
{
FILE *fpbook;
int choice5,cchoice,k=0,i=0;
char bookname[40],authorname[20];
if((fpbook=fopen("d://bookinformation.dat","rb+"))==NULL)
{
printf("File open error!n");
exit(0);
}
while(!feof(fpbook))
{
fread(&book[0],sizeof(struct book_information),1,fpbook);
i++;
}

printf("请选择操作:1-查找 0-返回n");//1-查找 0-返回
scanf("%d",&choice5);
switch(choice5)
{
case 1:
printf("选择查找方式:1-按书名 2-按作者 0-返回n");//1-按书名 2-按作者 0-返回
scanf("%d",&cchoice);
switch(cchoice)
{
case 1:
printf("输入书名:");
scanf("%s",bookname);
printf("登录号 书名 作者名 分类号 出版单位 出版时间 库存 价格n");
for(k=0;k<i;k++)
{
if(strcmp(bookname,book[k].book_name)==0)
printf("%s %s %s %c %s %s %d %lfn",book[k].registry_number,
book[k].book_name,
book[k].author,
book[k].classification,
book[k].published_by,
book[k].publication,
book[k].inventory,
book[k].book_price);
}
break;

case 2:
printf("输入作者名:");
scanf("%s",authorname);
printf("登录号 书名 作者名 分类号 出版单位 出版时间 库存 价格n");
for(k=0;k<i;k++)
{
if(strcmp(authorname,book[k].author)==0)
printf("%s %s %s %c %s %s %d %lfn",book[k].registry_number,
book[k].book_name,
book[k].author,
book[k].classification,
book[k].published_by,
book[k].publication,
book[k].inventory,
book[k].book_price);
}
break;

case 0:break;
}
case 0:break;
}
if(fclose(fpbook))
{
printf("Can not close the file!n");
exit(0);
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灵芸 2017-04-04 04:06:22

我在这里说明两个问题,希望对你有所帮助:
1.你粘贴的代码太长,说实话没有多少人愿意一句一句的去读,就算是很简单的问题,我反正是很讨厌提问者贴很长的代码。
2.你问题自己已经定位了,为什么不自己跟踪一下呢,而且你没有吧问题充分的说明白。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文