图书馆系统问题
想做一个显示所有信息的函数(show)
不输入数据的话信息显示挺好
如果用bookin函数输入数据的话就乱套了
用delbook删除居然还能再显示(show)函数查到
请问这是咋回事
#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.card_num,card.student_name,book.book_num,book.book_name
struct Card
{
char card_num;
char student_name;
int book_totle;
};
struct Card card={{"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;
char book_name;
struct Date date;
int flag;
};
struct Book book={{"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;
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;
printf("请输入要借阅的书号");
scanf("%s",booknum);
for(i=0;i<5;i++)
{
if(strcmp(booknum,book.book_num)==0)
{
if(book.flag==0)
printf("这本书已经借出!\n");
else
{
book.flag=0;
printf("借出日期填写:(请按年/月/日的顺序,如2019/9/8)\n");
scanf("%d/%d/%d",&book.date.year,&book.date.month,&book.date.day);
break;
}
}
}
for(i=0;i<5;i++)
if(strcmp(num,card.card_num)==0)
{ card.book_totle=card.book_totle+1;
break;
}
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)menu();
else exit(0);
}
void returnbook()
{
int i;
char booknum;
printf("请输入要还的书号");
scanf("%s",booknum);
for(i=0;i<5;i++)
{
if(strcmp(booknum,book.book_num)==0)
{
book.flag=1;
break;
}
if(i>=5)
printf("卡号输入错误!");
for(i=0;i<5;i++)
if(strcmp(num,card.card_num)==0)
{
card.book_totle=card.book_totle+1;
break;
}
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)menu();
else exit(0);
}
}
void bookinquiry()
{
int i;
char booknum;
printf("请输入要查询的书号:");
scanf("%s",booknum);
for(i=0;i<5;i++)
if(strcmp(booknum,book.book_num)==0)
{
printf("编号书名借出日期状态\n");
printf("%s %s",book.book_num,book.book_name);
printf("%4d年%2d月%2d日",book.date.year,book.date.month,book.date.day);
if(book.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;
FILE *fp;
if((fp=fopen("data","ab+"))==NULL)
{
printf("不能打开文件!\n");
return;
}
while(!feof(fp))
{
if(fread(&book ,LEN,1,fp)==1)
m++;
}
fclose(fp);
if((fp=fopen("data","wb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
for(i=0;i<m;i++)
fwrite(&book ,LEN,1,fp);
printf("是否输入?(y/n):");
scanf("%s",ch);
while(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
{
printf("图书编号:");
scanf("%s",book.book_num);
for(i=0;i<m;i++)
if (strcmp(book.book_num, book.book_num) == 0)
{
printf("该记录已经存在\n");
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)
menu();
else
exit(0);
}
printf("书名:");
scanf("%s",book.book_name);
if(fwrite(&book,LEN,1,fp)!=1)
{
printf("不能保存!");
getch();
}
else
{
printf("%s 已经保存!\n",book.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;
char ch;
if ((fp = fopen("data", "ab+")) == NULL)
{
printf("不能打开文件\n");
return;
}
while (!feof(fp))
if (fread(&book, LEN, 1, fp) == 1)
m++;
fclose(fp);
printf("请输入编号:");
scanf("%s", snum);
for (i = 0; i<5; i++)
if (strcmp(snum, book.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 = book;
m--;
printf("成功删除!\n");
}
if ((fp = fopen("data", "wb")) == NULL)
{
printf("不能打开!\n");
return;
}
for (j = 0; j<m; j++)
if (fwrite(&book, 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;
printf("请输入要查询的读者");
scanf("%s",studentname);
for(i=0;i<5;i++)
if(strcmp(studentname,card.student_name)==0)
{
printf("借书证号姓名书名借出日期\n");
printf("%s %s %s",card.card_num,card.student_name,book.book_name);
printf("%4d年%2d月%2d日\n",book.date.year,book.date.month,book.date.day);
}
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)menu();
else exit(0);
}
void readerin()
{
int i,m=0;
char ch;
FILE *fp;
if((fp=fopen("data","ab+"))==NULL)
{
printf("不能打开文件!\n");
return;
}
while(!feof(fp))
{
if(fread(&card ,LEN,1,fp)==1)
m++;
}
fclose(fp);
if((fp=fopen("data","wb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
for(i=0;i<m;i++)
fwrite(&card ,LEN,1,fp);
printf("是否输入?(y/n):");
scanf("%s",ch);
while(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
{
printf("学号:");
scanf("%s",card.card_num);
for(i=0;i<m;i++)
if (strcmp(card.card_num, card.card_num) == 0)
{
printf("该记录已经存在\n");
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)
menu();
else
exit(0);
}
printf("姓名:");
scanf("%s",card.student_name);
if(fwrite(&card,LEN,1,fp)!=1)
{
printf("不能保存!");
getch();
}
else
{
printf("%s 已经保存!\n",card.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;
char ch;
if ((fp = fopen("data", "ab+")) == NULL)
{
printf("不能打开文件\n");
return;
}
while (!feof(fp))
if (fread(&card, LEN, 1, fp) == 1)
m++;
fclose(fp);
printf("请输入编号:");
scanf("%s", num);
for (i = 0; i<5; i++)
if (strcmp(num, card.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 = card;
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;
printf("请输入要续借的书号");
scanf("%s",booknum);
for(i=0;i<5;i++)
if(strcmp(booknum,book.book_num)==0)
{
printf("编号书名借出日期状态\n");
printf("%s %s",book.book_num,book.book_name);
printf("%4d年%2d月%2d日",book.date.year,book.date.month,book.date.day);
if(book.flag==1)
printf("不能续借!\n");
else
printf("可以续借!\n");
}
printf("1.确认续借 2.取消续借");
scanf("%d",&i);
if(i==1)
{
book.flag=0;
printf("续借日期填写:(请按年/月/日的顺序,如2019/9/8)\n");
scanf("%d/%d/%d",&book.date.year,&book.date.month,&book.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;
printf("请输入你的卡号");
scanf("%s",num);
for(i=0;i<5;i++)
if(strcmp(num,card.card_num)==0)
break;
if(i>=5)
{
printf("卡号不存在!");
exit(0);
}
else
menu();
}
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define DATA card.card_num,card.student_name,book.book_num,book.book_name
struct Card
{
char card_num;
char student_name;
int book_totle;
};
struct Card card={{"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;
char book_name;
struct Date date;
int flag;
};
struct Book book={{"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}};
int n=100;
char num;
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;
printf("请输入要借阅的书号");
scanf("%s",booknum);
for(i=0;i<n;i++)
{
if(strcmp(booknum,book.book_num)==0)
{
if(book.flag==0)
printf("这本书已经借出!\n");
else
{
book.flag=0;
printf("借出日期填写:(请按年/月/日的顺序,如2019/9/8)\n");
scanf("%d/%d/%d",&book.date.year,&book.date.month,&book.date.day);
printf("借阅成功\n");
break;
}
}
}
for(i=0;i<n;i++)
if(strcmp(num,card.card_num)==0)
{ card.book_totle=card.book_totle+1;
break;
}
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)menu();
else exit(0);
}
void returnbook()
{
int i;
char booknum;
printf("请输入要还的书号");
scanf("%s",booknum);
for(i=0;i<n;i++)
if(strcmp(booknum,book.book_num)==0)
{
book.flag=1;
book.date.year=0;
book.date.month=0;
book.date.day=0;
break;
}
if(i==n)
printf("卡号输入错误!");
for(i=0;i<n;i++)
if(strcmp(num,card.card_num)==0)
{
printf("还书成功\n");
card.book_totle=card.book_totle-1;
break;
}
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)menu();
else exit(0);
}
void bookinquiry()
{
int i;
char booknum;
printf("请输入要查询的书号:");
scanf("%s",booknum);
for(i=0;i<n;i++)
if(strcmp(booknum,book.book_num)==0)
{
printf("编号书名借出日期状态\n");
printf("%s %s",book.book_num,book.book_name);
printf("%4d年%2d月%2d日",book.date.year,book.date.month,book.date.day);
if(book.flag==1)
printf("未借出!\n");
else
printf("已借出!\n");
break;
}
if(i==n)
printf("书号不存在\n");
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)
menu();
else
exit(0);
}
void bookin()
{
char booknum;
char bookname;
printf("请输入新书的书号:");
scanf("%s",booknum);
getchar();
printf("请输入新书的书名:");
scanf("%s",bookname);
getchar();
strcpy(book.book_num,booknum);
strcpy(book.book_name,bookname);
book.flag=1;
n++;
printf("亲,新书上好了哦 !!\n");
menu();
}
void delbook()
{
char booknum;
int i,j;
printf("请输入下架的书号:");
scanf("%s",booknum);
getchar();
for(i=0;i<n;i++)
{
if(strcmp(book.book_num,booknum)==0)
{
break;
}
}
if(i==n)
printf("你要下架的书不存在\n");
else
{
for(j=i;j<n;j++)
{
strcpy(book.book_num,book.book_num);
strcpy(book.book_name,book.book_name);
}
n--;
printf("亲,书已经下架了\n");
}
menu();
}
void readerinquiry()
{
int i;
char studentname;
printf("请输入要查询的读者姓名");
scanf("%s",studentname);
for(i=0;i<n;i++)
if(strcmp(studentname,card.student_name)==0)
{
printf("借书证号姓名 书名借出日期\n");
printf("%s %s %s",card.card_num,card.student_name,book.book_name);
printf("%4d年%2d月%2d日\n",book.date.year,book.date.month,book.date.day);
}
if(i==n)
printf("查无此人\n");
printf("1.确认并返回菜单 2.确认信息并退出\n");
scanf("%d",&i);
if(i==1)
menu();
else
exit(0);
}
void readerin()
{
char cardnum;
char cardname;
printf("请输入的学生编号:");
scanf("%s",cardnum);
getchar();
printf("请输入学生姓名:");
scanf("%s",cardname);
getchar();
strcpy(card.card_num,cardnum);
strcpy(card.student_name,cardname);
n++;
printf("读者登记成功!!\n");
menu();
}
void delreader()
{
char readername;
int i,j;
printf("请输入要删除的读者姓名:");
scanf("%s",readername);
getchar();
for(i=0;i<n;i++)
{
if(strcmp(card.student_name,readername)==0)
{
break;
}
}
if(i==n)
printf("查无此人\n");
else
{
for(j=i;j<n;j++)
{
strcpy(card.card_num,card.card_num);
strcpy(card.student_name,card.student_name);
}
n--;
printf("读者已成功删除\n");
}
menu();
}
void reborrow()
{
int i;
char booknum;
printf("请输入要续借的书号");
scanf("%s",booknum);
for(i=0;i<n;i++)
if(strcmp(booknum,book.book_num)==0)
{
printf("编号书名借出日期状态\n");
printf("%s %s",book.book_num,book.book_name);
printf("%4d年%2d月%2d日",book.date.year,book.date.month,book.date.day);
if(book.flag==1)
printf("未借出!\n");
else
printf("已借出!\n");
break;
}
if(strcmp(booknum,book.book_num)==0&&book.flag==0)
{
printf("请按1续借\n" );
scanf("%d",&i);
printf("续借日期填写:(请按年/月/日的顺序,如2019/9/8)\n");
scanf("%d/%d/%d",&book.date.year,&book.date.month,&book.date.day);
printf("续借成功\n");
}
else
{if(i==n)
printf("查无此书\n");
else
printf("续借失败:该书并未借出\n请直接进行借书操作\n");
}
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("请选择以上选项:\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:exit(0);break;
default:printf("输入错误,退出\n");exit(0);break;
}
}
void main()
{
int i;
char num;
printf("请输入你的卡号");
scanf("%s",num);
for(i=0;i<n;i++)
if(strcmp(num,card.card_num)==0)
break;
if(i>=n)
{
printf("卡号不存在!");
exit(0);
}
else
menu();
}
页:
[1]