#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct
{
char num[20];
char tit[20];
char aut[20];
char pubtime[20];
int quant;
double price;
}mbook;
typedef struct
{
mbook book[100];
int count;
}mlibrary;
mlibrary library;
void save()
{
FILE *fp;
if((fp=fopen("library.doc","w"))==NULL)
{
printf("\n不能保存图书信息\n");
return;
}
fwrite(&library.count,sizeof(int),1,fp);
fwrite(library.book,sizeof(library.book[0]),library.count,fp);
}
void bookEnter()
{
int y=1;
system("cls");
while(y)
{
printf("\t请输入书号:");
scanf("%s",library.book[library.count].num);
printf("\t请输入书名:");
scanf("%s",library.book[library.count].tit);
printf("\t请输入作者名:");
scanf("%s",library.book[library.count].aut);
printf("\t请输入出版时间:");
scanf("%s",library.book[library.count].pubtime);
printf("\t请输入该书的价格:");
scanf("%lf",&library.book[library.count].price);
library.count++;
printf("\n是否继续录入(1.继续; 0.退出录入):");
fflush(stdin);
scanf("%d",&y);
if (y!=1)
{
y=0;
break;
}
}
save();
}
void bookVist()
{
int i;
system("cls");
for (i=0;i<library.count;i++)
{
printf("书号: \n");
printf("%s\n",library.book[i].num);
printf("图书名: \n");
printf("%s\n",library.book[i].tit);
printf("作者名: \n");
printf("%s\n",library.book[i].aut);
printf("出版时间: \n");
printf("%s\n",library.book[i].pubtime);
printf("价格: \n");
printf("%6.2lf\n",library.book[i].price);
}
printf("已到达文档尾,请按任意键返回\n") ;
fflush(stdin);
getchar();
}
void find_by_tit()
{
char s2[20];
int flag=1;
int i;
printf("\t\t请输入要查询的书名: ");
scanf("%s",s2);
for(i=0;i<library.count;i++)
if(strcmp(s2,library.book[i].tit)==0)
{
flag=0;
printf("书号: \n");
printf("%s\n",library.book[i].num);
printf("书名: \n");
printf("%s\n",library.book[i].tit);
printf("作者名: \n");
printf("%s\n",library.book[i].aut);
printf("出版时间: \n");
printf("%s\n",library.book[i].pubtime);
printf("价格: \n");
printf("%6.2lf\n",library.book[i].price);
getchar();
break;
}
if (flag)
{
printf("不能找到信息,请按任意键返回\n");
}
getchar();
}
void find_by_aut()
{
char s3[20];
int flag=1;
int i;
printf("\t\t请输入要查询的书的作者: ");
scanf("%s",s3);
for( i=0;i<library.count;i++ )
if(strcmp(s3,library.book[i].aut)==0)
{
flag=0;
printf("书号: \n");
printf("%s\n",library.book[i].num);
printf("书名: \n");
printf("%s\n",library.book[i].tit);
printf("作者名: \n");
printf("%s\n",library.book[i].aut);
printf("出版时间: \n");
printf("%s\n",library.book[i].pubtime);
printf("价格: \n");
printf("%6.2lf\n",library.book[i].price);
getchar();
break;
}
if (flag)
{
printf("不能找到信息,请按任意键返回");
}
getchar();
}
void menu()
{
printf("\n\n\n\n\n\t\t\t\t\t *图书管理系统* \n");
printf("\t\t\t\t\t1. --------图书信息录入--------\n");
printf("\t\t\t\t\t2. --------图书信息浏览--------\n");
printf("\t\t\t\t\t3. --------图书信息查询--------\n");
printf("\t\t\t\t\t4. --------图书信息删除--------\n");
printf("\t\t\t\t\t5. --------图书信息修改--------\n");
printf("\t\t\t\t\t0. --------退出图书信息系统----\n\n\n");
}
void bookFind()
{
int a;
system("cls");
printf("\t\t******************************\n");
printf("\t\t******************************\n");
printf("\t\t开始查询\n");
printf("\t\t\t1.按书名查询\n");
printf("\t\t\t2.按作者名查询\n");
printf("\t\t\t0.返回\n");
printf("\t\t******************************\n") ;
printf("\t\t请选择你想要的查询方式:");
scanf("%d",&a);
switch(a)
{
case 1:
find_by_tit();
break;
case 2:
find_by_aut();
break;
case 0: break;
}
getchar();
}
void bookDel()
{
char number[10];
int i,in;
system("cls"); /*清屏*/
printf("\n 请输入你想删除的图书号:");
scanf("%s",number);
printf("\t你确定删除此条信息吗?\n");
printf("\t1.是 2.否\n");
scanf("%d",&in);
if(in==1)
{
for (i=0;i<library.count;i++)
{
if(strcmp(number,library.book[i].num)==0)
break;
}
if (i!=library.count)
{
for(;i<library.count-1;i++)
{
strcpy(library.book[i].tit,library.book[i+1].tit);
strcpy(library.book[i].num,library.book[i+1].num);
strcpy(library.book[i].aut,library.book[i+1].aut);
strcpy(library.book[i].pubtime,library.book[i+1].pubtime);
library.book[i].price=library.book[i+1].price;
}
library.count--;
}
else
printf("\n 不能找到图书信息,请按按任意键重新输入\n");
save();
printf("图书信息已删除,请按任意键继续\n");
getchar();
}
else
{
printf("请按任意键返回\n");
getchar();
}
save();
}
void bookChange()
{
char number[10];
int i,in;
system("cls"); /*清屏*/
printf("\n 请输入你想要修改的图书的书号 :");
scanf("%s",number);
printf("你确定修改此条信息吗?\n");
printf("1.是 2.否\n");
scanf("%d",&in);
if(in==1)
{
for (i=0;i<library.count;i++)
{
if(strcmp(number,library.book[i].num)==0)
{
printf("%s\n",library.book[i].num);
printf("%s\n",library.book[i].tit);
printf("%s\n",library.book[i].aut);
printf("%s\n",library.book[i].pubtime);
printf("%6.2lf\n",library.book[i].price);
break;
}
}
if (i!=library.count)
{
mbook book;
printf("\n\t\t请输入修改后此图书的信息\n");
printf("\t请输入图书号:");
scanf("%s",book.num);
printf("\t请输入图书名:");
scanf("%s",book.tit);
printf("\t请输入作者名:");
scanf("%s",book.aut);
printf("\t请输入出版时间:");
scanf("%s",book.pubtime);
printf("\t请输入该书的价格:");
scanf("%f",&book.price);
strcpy(library.book[i].tit,book.tit);
strcpy(library.book[i].num,book.num);
strcpy(library.book[i].aut,book.aut);
strcpy(library.book[i].pubtime,book.pubtime);
library.book[i].price=book.price;
save();
}
else
printf("\n 不能找到图书信息,请按任意键重新输入\n");
getchar();
}
else
{
printf("请按任意键重新选择\n");
getchar();
}
save();
}
int main()
{
FILE *fp;
int a;
if(1)
{
if((fp=fopen("library.doc","r"))==NULL)
{
printf("\n不能打开图书信息库\n");
fp=fopen("library.doc","w+");
printf("\n初始化图书信息库,初始化之后要重启程序\n");
library.count=0;
fwrite(&library.count,sizeof(int),1,fp);
fclose(fp);
return 0 ;
}
fread(&library.count,sizeof(int),1,fp);
fread(library.book,sizeof(library.book[0]),library.count,fp);
fclose(fp);
do
{
system("cls");
menu();
scanf("%d",&a);
switch(a)
{
case 1:
bookEnter();
break;
case 2:
bookVist();
break;
case 3:
bookFind();
break;
case 4:
bookDel();
break;
case 5:
bookChange();
break;
case 0:
printf("\n\n退出\n\n");
break;
default:
printf("\n没有此项功能,请重新输入\n");
}
}while(a!=0);
save();
}
}