#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define LEN sizeof(struct Book)
#define FORMAT "%-15s%-15s%-15s%-15s\n"
#define DATA card[i].card_num,card[i].student_name,book[i].book_num,book[i].book_name
struct Card
{
char card_num[99];
char student_name[20];
int book_totle;
};
struct Card card[10]={{"20111101","Zhanjun",3},{"20111103","YangKai",1},{"20111113","WuGang",4},{"20111123","Shanglei",2},{"20111105","ZhangKun",5}};
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char book_num[10];
char book_name[20];
struct Date date;
int flag;
};
struct Book book[10]={{"305032","History",{0,0,0},1},{"409812","Geogrophy",{0,0,0},1},{"213401","English",{0,0,0},1},{"3287463","Maths",{0,0,0},1},{"4102102","Physicyl",{0,0,0},1}};
char num[10];
void menu();//函数的声明
void borrowbook();
void returnbook();
void bookinquiry();
void bookin();
void readerinquiry();
void readerin();
void reborrow();
void delbook();
void delreader();
void show();
void borrowbook()
{
int i;
char booknum[10];
printf("请输入要借阅的书号");
scanf("%s",booknum);
for(i=0;i<5;i++)
{
if(strcmp(booknum,book[i].book_num)==0)
{
if(book[i].flag==0)
printf("这本书已经借出!\n");
else
{
book[i].flag=0;
printf("借出日期填写:(请按年/月/日的顺序,如2019/9/8)\n");
scanf("%d/%d/%d",&book[i].date.year,&book[i].date.month,&book[i].date.day);
break;
}
}
}
for(i=0;i<5;i++)
if(strcmp(num,card[i].card_num)==0)
{ card[i].book_totle=card[i].book_totle+1;
break;
}
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)menu();
else exit(0);
}
void returnbook()
{
int i;
char booknum[10];
printf("请输入要还的书号");
scanf("%s",booknum);
for(i=0;i<5;i++)
{
if(strcmp(booknum,book[i].book_num)==0)
{
book[i].flag=1;
break;
}
if(i>=5)
printf("卡号输入错误!");
for(i=0;i<5;i++)
if(strcmp(num,card[i].card_num)==0)
{
card[i].book_totle=card[i].book_totle+1;
break;
}
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)menu();
else exit(0);
}
}
void bookinquiry()
{
int i;
char booknum[10];
printf("请输入要查询的书号:");
scanf("%s",booknum);
for(i=0;i<5;i++)
if(strcmp(booknum,book[i].book_num)==0)
{
printf("编号 书名 借出日期 状态\n");
printf("%s %s",book[i].book_num,book[i].book_name);
printf("%4d年%2d月%2d日",book[i].date.year,book[i].date.month,book[i].date.day);
if(book[i].flag==1)
printf("未借出!\n");
else
printf("已借出!\n");
}
printf("书号不存在\n");
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)
menu();
else
exit(0);
}
void bookin()
{
int i,m=0;
char ch[2];
FILE *fp;
if((fp=fopen("data","ab+"))==NULL)
{
printf("不能打开文件!\n");
return;
}
while(!feof(fp))
{
if(fread(&book[m] ,LEN,1,fp)==1)
m++;
}
fclose(fp);
if((fp=fopen("data","wb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
for(i=0;i<m;i++)
fwrite(&book[i] ,LEN,1,fp);
printf("是否输入?(y/n):");
scanf("%s",ch);
while(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
{
printf("图书编号:");
scanf("%s",book[m].book_num);
for(i=0;i<m;i++)
if (strcmp(book[m].book_num, book[i].book_num) == 0)
{
printf("该记录已经存在\n");
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)
menu();
else
exit(0);
}
printf("书名:");
scanf("%s",book[m].book_name);
if(fwrite(&book[m],LEN,1,fp)!=1)
{
printf("不能保存!");
getch();
}
else
{
printf("%s 已经保存!\n",book[m].book_name);
m++;
}
printf("是否继续?(y/n):");
scanf("%s",ch);
}
fclose(fp);
printf("OK!\n");
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)
menu();
else
exit(0);
}
void delbook()
{
FILE *fp;
int i, j, m = 0;
char snum[10];
char ch[2];
if ((fp = fopen("data", "ab+")) == NULL)
{
printf("不能打开文件\n");
return;
}
while (!feof(fp))
if (fread(&book[m], LEN, 1, fp) == 1)
m++;
fclose(fp);
printf("请输入编号:");
scanf("%s", snum);
for (i = 0; i<5; i++)
if (strcmp(snum, book[i].book_num) == 0)
{
printf("已经找到该记录,是否删除?(y/n)");
break;
}
scanf("%s", ch);
if (strcmp(ch, "Y") == 0 || strcmp(ch, "y") == 0)
{
for (j = i; j<m; j++)
book[j] = book[j + 1];
m--;
printf("成功删除!\n");
}
if ((fp = fopen("data", "wb")) == NULL)
{
printf("不能打开!\n");
return;
}
for (j = 0; j<m; j++)
if (fwrite(&book[j], LEN, 1, fp) != 1)
{
printf("不能保存!\n");
getch();
}
fclose(fp);
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d", &i);
if (i == 1)
menu();
else
exit(0);
}
void readerinquiry()
{
int i;
char studentname[20];
printf("请输入要查询的读者");
scanf("%s",studentname);
for(i=0;i<5;i++)
if(strcmp(studentname,card[i].student_name)==0)
{
printf("借书证号 姓名 书名 借出日期 \n");
printf("%s %s %s",card[i].card_num,card[i].student_name,book[i].book_name);
printf("%4d年%2d月%2d日\n",book[i].date.year,book[i].date.month,book[i].date.day);
}
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)menu();
else exit(0);
}
void readerin()
{
int i,m=0;
char ch[2];
FILE *fp;
if((fp=fopen("data","ab+"))==NULL)
{
printf("不能打开文件!\n");
return;
}
while(!feof(fp))
{
if(fread(&card[m] ,LEN,1,fp)==1)
m++;
}
fclose(fp);
if((fp=fopen("data","wb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
for(i=0;i<m;i++)
fwrite(&card[i] ,LEN,1,fp);
printf("是否输入?(y/n):");
scanf("%s",ch);
while(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
{
printf("学号:");
scanf("%s",card[m].card_num);
for(i=0;i<m;i++)
if (strcmp(card[m].card_num, card[i].card_num) == 0)
{
printf("该记录已经存在\n");
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)
menu();
else
exit(0);
}
printf("姓名:");
scanf("%s",card[m].student_name);
if(fwrite(&card[m],LEN,1,fp)!=1)
{
printf("不能保存!");
getch();
}
else
{
printf("%s 已经保存!\n",card[m].student_name);
m++;
}
printf("是否继续?(y/n):");
scanf("%s",ch);
}
fclose(fp);
printf("OK!\n");
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)
menu();
else
exit(0);
}
void delreader()
{
FILE *fp;
int i, j, m = 0;
char num[10];
char ch[2];
if ((fp = fopen("data", "ab+")) == NULL)
{
printf("不能打开文件\n");
return;
}
while (!feof(fp))
if (fread(&card[m], LEN, 1, fp) == 1)
m++;
fclose(fp);
printf("请输入编号:");
scanf("%s", num);
for (i = 0; i<5; i++)
if (strcmp(num, card[i].card_num) == 0)
{
printf("已经找到该记录,是否删除?(y/n)");
break;
}
scanf("%s", ch);
if (strcmp(ch, "Y") == 0 || strcmp(ch, "y") == 0)
{
for (j = i; j<m; j++)
card[j] = card[j + 1];
m--;
printf("成功删除!\n");
}
if ((fp = fopen("data", "wb")) == NULL)
{
printf("不能打开!\n");
return;
}
fclose(fp);
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d", &i);
if (i == 1)
menu();
else
exit(0);
}
void reborrow()
{
int i;
char booknum[10];
printf("请输入要续借的书号");
scanf("%s",booknum);
for(i=0;i<5;i++)
if(strcmp(booknum,book[i].book_num)==0)
{
printf("编号 书名 借出日期 状态\n");
printf("%s %s",book[i].book_num,book[i].book_name);
printf("%4d年%2d月%2d日",book[i].date.year,book[i].date.month,book[i].date.day);
if(book[i].flag==1)
printf("不能续借!\n");
else
printf("可以续借!\n");
}
printf("1.确认续借 2.取消续借");
scanf("%d",&i);
if(i==1)
{
book[i].flag=0;
printf("续借日期填写:(请按年/月/日的顺序,如2019/9/8)\n");
scanf("%d/%d/%d",&book[i].date.year,&book[i].date.month,&book[i].date.day);
}
else
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)menu();
else exit(0);
}
void show()
{
int i;
printf("学号 姓名 书号 书名\t\n");
for(i=0;i<10;i++)
printf(FORMAT,DATA);
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)
menu();
else
exit(0);
}
void menu()
{
int n;
printf("~~~欢迎使用本校图书管理系统~~~\n");
printf("*******************************\n");
printf("1.借书\n");
printf("2.还书\n");
printf("3.图书查询\n");
printf("4.新书上架\n");
printf("5.旧书下架\n");
printf("6.读者查询\n");
printf("7.添加读者\n");
printf("8.删除读者\n");
printf("9.续借\n");
printf("10.显示\n");
printf("11.退出\n");
printf("请选择以上选项:\n");
printf("*******************************\n");
scanf("%d",&n);
switch(n)
{
case 1:borrowbook();break;
case 2:returnbook();break;
case 3:bookinquiry();break;
case 4:bookin();break;
case 5:delbook();break;
case 6:readerinquiry();break;
case 7:readerin();break;
case 8:delreader();break;
case 9:reborrow();break;
case 10:show();break;
case 11:exit(0);break;
default:printf("输入错误,退出\n");exit(0);break;
}
}
void main()
{
int i;
char num[10];
printf("请输入你的卡号");
scanf("%s",num);
for(i=0;i<5;i++)
if(strcmp(num,card[i].card_num)==0)
break;
if(i>=5)
{
printf("卡号不存在!");
exit(0);
}
else
menu();
}