/* Note:Your choice is C IDE */
#include
#include
#include
#include
#include
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
struct student /*定义学生用户类型*/
{
int cardnum;
char studentname[15];
int studentage;
char studentsex[2];
char lendbook[10];
}student[1000];
struct teacher /*定义教师用户类型*/
{
int teachernum;
char teachername[15];
int teacherage;
char teachersex[2];
}teacher[50];
struct book /*定义书的类型*/
{
int booknum;
char bookname[10];
char bookcreate[10];
int turefalse; /*用于借书和还书模块判断一本书是否借出的条件*/
}book[1000];
struct card /*借书卡的数据类型*/
{
int cardnum;
char studentname[10];
int studentclass;
}card[100];
huanbook() /*还书函数*/
{
FILE *fp,*fp2; /*定义两个文件指针,fp2用于修改数据时设立临时文件用,防止数据遭破坏*/
int i,n;
int cardnum;
char lendbook[10];
printf("请你输入你的卡号:\n");
scanf("%d",&cardnum);
fp=fopen("card.txt","r"); /*读取卡号记录*/
for(i=0;fread(&card[i],sizeof(struct card),1,fp)!=0;i++) /*for循环判断卡号是否存在*/
{
if(card[i].cardnum==cardnum) /*卡号存在,进入下一循环*/
{
n=i;
fclose(fp);
printf("请输入你要还的书的名字:\n");
scanf("%s",lendbook);
fp=fopen("record.txt","r");
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++) /*判断是否借阅了输入的书*/
{
if(strcmp(student[i].lendbook,lendbook)==0) /*借阅了该书,进入下一循环,否则出错显示*/
{
fclose(fp);
fp=fopen("record.txt","r");
fp2=fopen("bookl.txt","w");
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)
{
if(strcmp(student[i].lendbook,lendbook)==0)
{
continue; /*删除还掉的书的借书记录*/
}
fwrite(&student[i],sizeof(struct student),1,fp2); /*写入原来没还的书的记录*/
}
fclose(fp);
fclose(fp2);
fp=fopen("record.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&student[i],sizeof(struct student),1,fp2)!=0;i++)
{
fwrite(&student[i],sizeof(struct student),1,fp); /*将借书记录信息写回*/
}
fclose(fp);
fclose(fp2);
fopen("bookl.txt","w"); /*清临时文件的记录*/
fclose(fp2);
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++) /*将书的记录写入临时文件,防止因为修改信息破坏以前的记录*/
{
if(i==n)
{
book[i].turefalse=1;
fwrite(&book[i],sizeof(struct book),1,fp2); /*将还的书的原来状态设为无人借阅的*/
continue;
}
fwrite(&book[i],sizeof(struct book),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
{
fwrite(&book[i],sizeof(struct book),1,fp); /*将临时文件写回*/
}
fclose(fp);
fclose(fp2);
fopen("bookl.txt","w"); /*清临时文件*/
fclose(fp2);
printf("还书完毕,按任意键返回\n");
getch();
return 1;
}
}
printf("你没有借这样的书,任意键返回\n"); /*出错提示*/
fclose(fp);
getch();
return 0;
}
}
printf("系统没这样的卡,和管理员联系,按任意键返回\n"); /*出错提示*/
fclose(fp);
getch();
}
findbook()
{
FILE *fp;
char bookname[10];
int ture,i;
fp=fopen("book.txt","r");
printf("请输入你要查找的书名\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(bookname,book[i].bookname)==0)
{
if(book[i].turefalse==1)
{
printf("这本书的详细资料是:序号:〈%d〉 书名:《%s》 作者:(%s)此书现在无人借阅\n按任意键返回\n",book[i].booknum,book[i].bookname,book[i].bookcreate);
}
else
{
printf("这本书已经有人借出\n");
fclose(fp);
return 0;
}
fclose(fp);
return FALSE;
}
}
printf("没有你要查询的书籍!\n");
fclose(fp);
return FALSE;
}
findbook1()
{
FILE *fp;
char bookcreate[10];
int ture,i;
fp=fopen("book.txt","r");
printf("请输入你要查找的作者名\n");
scanf("%s",bookcreate);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(bookcreate,book[i].bookcreate)==0)
{
if(book[i].turefalse==1)
{
printf("这本书的详细资料是:序号:〈%d〉 书名:《%s》 作者:(%s) 此书现在无人借阅\n按任意键返回\n",book[i].booknum,book[i].bookname,book[i].bookcreate);
}
else
{
printf("这本书已经有人借出\n");
fclose(fp);
return 0;
}
fclose(fp);
return FALSE;
}
}
printf("没有你要查询的书籍\n");
fclose(fp);
return FALSE;
}
lendcount()
{
FILE *fp;
int i,n=0;
fp=fopen("record.txt","r");
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)
{
printf("卡号:%d 借出的书籍:%s \n",student[i].cardnum,student[i].lendbook);
n=n+1;
}
fclose(fp);
printf("目前共有%d本书借出\n",n);
printf("按任意键\n");
getch();
return n;
}
searchbook()
{
char ch5;
do
{
printf("---------------欢迎进入图书查询系统!--------------\n");
printf(" 1:<按书名查找>\n");
printf(" 2:<按作者查找>\n");
printf(" 0:<返回>\n");
printf("请输入0--2,其他输入非法!\n");
scanf("%s",&ch5);
switch(ch5)
{
case '1':findbook();getch();break;
case '2':findbook1();getch();break;
case '0':break;
default:printf("无此操作\n");getch();break;
}
}while(ch5!='0');
return FALSE;
}
lendbook()
{
FILE *fp,*fp2;
int i,n;
int cardnum;
printf("请你输入你的卡号:\n");
scanf("%d",&cardnum);
fp=fopen("card.txt","r");
for(i=0;fread(&card[i],sizeof(struct card),1,fp)!=0;i++)
{
if(card[i].cardnum==cardnum)
{
n=i;
fclose(fp);
printf("请输入你要借阅的书的名字:\n");
scanf("%s",student[n].lendbook);
fp=fopen("book.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(book[i].bookname,student[n].lendbook)==0)
{
if(book[i].turefalse==0)
{
printf("对不起,此书有人借出,请借其他书\n");
fclose(fp);
getch();
return;
}
else
fclose(fp);
fp=fopen("record.txt","a+");
student[n].cardnum=cardnum;
fwrite(&student[n],sizeof(struct student),1,fp);
fclose(fp);
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(book[i].bookname,student[n].lendbook)==0)
{
book[i].turefalse=0;
fwrite(&book[i],sizeof(struct book),1,fp2);
continue;
}
fwrite(&book[i],sizeof(struct book),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
{
fwrite(&book[i],sizeof(struct book),1,fp);
}
fclose(fp);
fclose(fp2);
fopen("bookl.txt","w");
fclose(fp2);
printf("借书完毕,按任意键返回\n");
getch();
return;
}
}
printf("不存在这样的书,任意键返回\n");
fclose(fp);
getch();
return;
}
}
printf("你的卡号不存在,请申请新卡,按任意键返回\n");
fclose(fp);
getch();
}
cardcount()
{
FILE *fp;
int i,n=0;
fp=fopen("card.txt","r");
for(i=0;fread(&card[i],sizeof(struct card),1,fp)!=0;i++)
{
printf("第%d张卡<卡号:%d 姓名:%s 班级:%d>\n",i+1,card[i].cardnum,card[i].studentname,card[i].studentclass);
n=n+1;
}
fclose(fp);
printf("目前共有%d本书\n",n);
printf("按任意键退出\n");
getch();
}
delcard()
{
FILE *fp,*fp2;
int i;
int cardnum;
char choice;
fp=fopen("card.txt","r");
fp2=fopen("bookl.txt","w");
printf("请输入你要删除的卡号\n");
printf("如果你输入的卡号存在,系统自动删除该信息!如果不存在,系统不做任何改动\n");
scanf("%d",&cardnum);
for(i=0;fread(&card[i],sizeof(struct card),1,fp)!=0;i++)
{
if(card[i].cardnum!=cardnum)
{
fwrite(&card[i],sizeof(struct card),1,fp2);
}
}
fclose(fp);
fclose(fp2);
printf("是否真的要删除该卡?删除后该书籍的所有信息将无法恢复(Y/N)\n");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
fp=fopen("card.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&card[i],sizeof(struct card),1,fp2)!=0;i++)
{
fwrite(&card[i],sizeof(struct card),1,fp);
}
fclose(fp);
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意键返回\n");
getch();
return;
}
else
{
printf("按任意键返回\n");
getch();
return;
}
}
addcard()
{
FILE *fp;
int i=0;
fp=fopen("card.txt","a+");
printf("请你输入卡号\n");
scanf("%d",&card[i].cardnum);
printf("请你输入学生姓名\n");
scanf("%s",card[i].studentname);
printf("请你输入班级\n");
scanf("%d",&card[i].studentclass);
fwrite(&card[i],sizeof(struct card),1,fp);
fclose(fp);
printf("输入完毕,任意键返回\n");
getch();
}
changeteacher()
{
FILE *fp,*fp2;
char teachername[10],choice;
int i;
fp=fopen("teacher.txt","r");
fp2=fopen("bookl.txt","w");
printf("请你输入要修改的教师用户的名字\n");
scanf("%s",teachername);
for(i=0;fread(&teacher[i],sizeof(struct teacher),1,fp)!=0;i++)
{
if(strcmp(teacher[i].teachername,teachername)==0)
{
printf("你所要修改的教师用户的资料如下,请选择你要修改的内容\n");
printf("<教师用户号:%d教师用户名:%s 年龄:%d 性别:%s >\n",teacher[i].teachernum,teacher[i].teachername,teacher[i].teacherage,teacher[i].teachersex);
printf("1:修改教师用户号\n");
printf("2:修改教师用户名\n");
printf("3:修改教师用户年龄\n");
printf("4:修改教师用户性别\n");
printf("请输入1-4:");
scanf("%s",&choice);
switch(choice)
{
case '1':
{
printf("请输入新的教师用户号\n");
scanf("%d",&teacher[i].teachernum);
fwrite(&teacher[i],sizeof(struct teacher),1,fp2);
}break;
case '2':
{
printf("请输入新的教师用户姓名\n");
scanf("%s",teacher[i].teachername);
fwrite(&teacher[i],sizeof(struct teacher),1,fp2);
}break;
case '3':
{
printf("请输入新的教师用户年龄\n");
scanf("%d",&teacher[i].teacherage);
fwrite(&teacher[i],sizeof(struct teacher),1,fp2);
}break;
case '4':
{
printf("请输入新的教师用户性别\n");
scanf("%d",&teacher[i].teachersex);
fwrite(&teacher[i],sizeof(struct teacher),1,fp2);
}break;
default:printf("没有这样的操作");break;
}
continue;
}
fwrite(&teacher[i],sizeof(struct teacher),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("teacher.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&teacher[i],sizeof(struct teacher),1,fp2)!=0;i++)
{
fwrite(&teacher[i],sizeof(struct teacher),1,fp);
}
fclose(fp);
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意键返回\n");
getchar();
return;
}
delteacher()
{
FILE *fp,*fp2;
int i;
char teachername[10],choice;
fp=fopen("teacher.txt","r");
fp2=fopen("bookl.txt","w");
printf("请输入你要删除的教师用户名\n");
printf("如果你输入的教师用户存在,系统自动删除该信息!如果不存在,系统不做任何改动\n");
scanf("%s",teachername);
for(i=0;fread(&teacher[i],sizeof(struct teacher),1,fp)!=0;i++)
{
if(strcmp(teachername,teacher[i].teachername)!=0)
{
fwrite(&teacher[i],sizeof(struct teacher),1,fp2);
}
}
fclose(fp);
fclose(fp2);
printf("是否真的要删除该教师用户信息?删除后的所有信息将无法恢复(Y/N)\n");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
fp=fopen("teacher.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&teacher[i],sizeof(struct teacher),1,fp2)!=0;i++)
{
fwrite(&teacher[i],sizeof(struct teacher),1,fp);
}
fclose(fp);
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意键返回\n");
getch();
return TRUE;
}
else
{
printf("按任意键返回\n");
getch();
return FALSE;
}
}
teachercount()
{
FILE *fp;
int i,n=0;
fp=fopen("teacher.txt","r");
for(i=0;fread(&teacher[i],sizeof(struct teacher),1,fp)!=0;i++)
{
printf("第%d个教师用户的信息如下:\n<教师用户号:%d 教师用户名:%s 年龄:%d 性别:%s >\n",n+1,teacher[i].teachernum,teacher[i].teachername,teacher[i].teacherage,teacher[i].teachersex);
n++;
}
fclose(fp);
printf("目前共有%d个教师用户\n",n);
printf("按任意键返回\n");
}
addteacher()
{
FILE *fp;
char choice='y';
int i=1;
fp=fopen("teacher.txt","a+");
while(choice=='y'||choice=='Y')
{
printf("请你输入教师用户号码\n");
scanf("%d",&teacher[i].teachernum);
printf("请你输入教师用户名\n");
scanf("%s",teacher[i].teachername);
printf("请输入教师用户年龄\n");
scanf("%d",&teacher[i].teacherage);
printf("请你输入教师用户性别\n");
scanf("%s",teacher[i].teachersex);
printf("是否要输入下个教师用户信息?\n");
scanf("%s",&choice);
}
printf("按任意键返回\n");
fclose(fp);
}
addbook()
{
FILE *fp;
int i=0;
char choice='y';
fp=fopen("book.txt","a+");
while(choice=='y'||choice=='Y')
{
printf("请你输入第%d本书的序号\n",i+1);
scanf("%d",&book[i].booknum);
printf("请你输入书名\n");
scanf("%s",book[i].bookname);
printf("请你输入书的作者\n");
scanf("%s",book[i].bookcreate);
printf("请设为1或0,1代表书还没人借,0表示书已经借出\n");
printf("请你设定书的状态\n");
scanf("%d",&book[i].turefalse);
fwrite(&book[i],sizeof(struct book),1,fp);
printf("是否要输入下本书(Y/N)\n");
scanf("%s",&choice);
}
fclose(fp);
}
bookcount()
{
FILE *fp;
int i,n=0;
fp=fopen("book.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(book[i].booknum!=0&&strlen(book[i].bookname)!=0&&strlen(book[i].bookcreate)!=0)
{
printf("第%d本书<序号:%d 书名:%s 作者:%s 状态:%d>\n",i+1,book[i].booknum,book[i].bookname,book[i].bookcreate,book[i].turefalse);
n=n+1;
}
}
fclose(fp);
printf("目前共有%d本书\n",n);
printf("按任意键\n");
}
delbook()
{
FILE *fp,*fp2;
int i;
char bookname[10],choice;
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
printf("请输入你要删除的书名\n");
printf("如果你输入的书名存在,系统自动删除该信息!如果不存在,系统不做任何改动\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(bookname,book[i].bookname)!=0)
{
fwrite(&book[i],sizeof(struct book),1,fp2);
}
}
fclose(fp);
fclose(fp2);
printf("是否真的要删除该书籍?删除后该书籍的所有信息将无法恢复(Y/N)\n");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
{
fwrite(&book[i],sizeof(struct book),1,fp);
}
fclose(fp);
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意键返回\n");
getch();
return;
}
else
{
printf("按任意键返回\n");
getch();
return;
}
}
changebook()
{
FILE *fp,*fp2;
char bookname[10],choice;
int i;
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
printf("请你输入要修改的书籍的书\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(book[i].bookname,bookname)==0)
{
printf("你所要修改的书的资料如下,请选择你要修改的内容\n");
printf("序号:〈%d〉 书名:《%s》 作者:(%s) \n",book[i].booknum,book[i].bookname,book[i].bookcreate);
printf("1:修改书的序号\n");
printf("2:修改书名\n");
printf("3:修改作者\n");
printf("请输入1-3:");
scanf("%s",&choice);
switch(choice)
{
case '1':
{
printf("请输入新的序号\n");
scanf("%d",&book[i].booknum);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '2':
{
printf("请输入新的书名\n");
scanf("%s",book[i].bookname);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '3':
{
printf("请输入新的作者\n");
scanf("%s",book[i].bookcreate);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
default:printf("没有这样的操作");break;
}
continue;
}
fwrite(&book[i],sizeof(struct book),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
{
fwrite(&book[i],sizeof(struct book),1,fp);
}
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意键返回\n");
getchar();
return;
}
changecard()
{
FILE *fp,*fp2;
char studentname[10],choice;
int i;
fp=fopen("card.txt","r");
fp2=fopen("bookl.txt","w");
printf("请你输入要修改的学生用户的名字\n");
scanf("%s",studentname);
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)
{
if(strcmp(student[i].studentname,studentname)==0)
{
printf("你所要修改的学生用户的资料如下,请选择你要修改的内容\n");
printf("<学生用户卡号:%d 学生用户名:%s 年龄:%d 性别:%s >\n",student[i].cardnum,student[i].studentname,student[i].studentage,student[i].studentsex);
printf("1:修改学生用户卡号\n");
printf("2:修改学生用户名\n");
printf("3:修改学生用户年龄\n");
printf("4:修改学生用户性别\n");
printf("请输入1--4:");
scanf("%s",&choice);
switch(choice)
{
case '1':
{
printf("请输入新的学生用户卡号\n");
scanf("%d",&student[i].cardnum);
fwrite(&student[i],sizeof(struct student),1,fp2);
}break;
case '2':
{
printf("请输入新的学生用户姓名\n");
scanf("%s",student[i].studentname);
fwrite(&student[i],sizeof(struct student),1,fp2);
}break;
case '3':
{
printf("请输入新的学生用户年龄\n");
scanf("%d",&student[i].studentage);
fwrite(&student[i],sizeof(struct student),1,fp2);
}break;
case '4':
{
printf("请输入新的学生用户性别\n");
scanf("%d",&student[i].studentsex);
fwrite(&student[i],sizeof(struct student),1,fp2);
}break;
default:printf("没有这样的操作");break;
}
continue;
}
fwrite(&student[i],sizeof(struct student),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("card.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&student[i],sizeof(struct student),1,fp2)!=0;i++)
{
fwrite(&student[i],sizeof(struct student),1,fp);
}
fclose(fp);
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意键返回\n");
getchar();
return;
}
main()
{
char ch1,ch2,ch3,ch4,ch5;
do
{
printf("\t\t请你选择操作类型:\n");
printf(" 1:<查阅图书>\n");
printf(" 2:<借阅图书>\n");
printf(" 3:<管理系统(管理员)>\n");
printf(" 4:<还书>\n");
printf(" 0:<退出>\n");
printf("请输入0--4\n");
scanf("%s",&ch1);
switch(ch1)
{
case '1':searchbook();break;
case '2':lendbook();;break;
case '3':{
do
{
printf("---------------欢迎进入管理系统!--------------\n");
printf(" 1:<增加图书>\n");
printf(" 2:<删除图书>\n");
printf(" 3:<修改图书资料>\n");
printf(" 4:<书籍统计>\n");
printf(" 5:<教师用户管理系统>\n");
printf(" 6:<学生用户管理系统>\n");
printf(" 0:<返回>\n");
printf("请输入0--6,其他输入非法!\n");
scanf("%s",&ch2);
switch(ch2)
{
case '1':addbook();break;
case '2':delbook();break;
case '3':changebook();break;
case '4':bookcount();getch();break;
case '5':{
do
{
printf("---------------欢迎进入教师用户管理系统!--------------\n");
printf(" 1:<增加教师用户>\n");
printf(" 2:<删除教师用户>\n");
printf(" 3:<修改教师用户资料>\n");
printf(" 4:<教师用户统计>\n");
printf(" 0:<返回>\n");
printf("请输入0--4,其他输入非法!\n");
scanf("%s",&ch3);
switch(ch3)
{
case '1':addteacher();getch();break;
case '2':delteacher();break;
case '3':changeteacher();break;
case '4':teachercount();getch();break;
case '0':break;
default:printf("无此操作\n");getch();break;
}
}while(ch3!='0');
}break;
case '6':{
do
{
printf("---------------欢迎进入学生用户管理系统!--------------\n");
printf(" 1:<申请增添新卡>\n");
printf(" 2:<删除卡号>\n");
printf(" 3:<借书统计>\n");
printf(" 4:<卡号统计>\n");
printf(" 5:<修改卡号>\n");
printf(" 0:<返回>\n");
printf("请输入0--5,其他输入非法!\n");
scanf("%s",&ch4);
switch(ch4)
{
case '1':addcard();break;
case '2':delcard();break;
case '3':lendcount();break;
case '4':cardcount();break;
case '5':changecard();break;
case '0':break;
default:printf("无此操作\n");getch();break;
}
}while(ch4!='0');
}break;
case '0':break;
default:printf("无此操作\n");getch();break;
}
}while(ch2!='0');}break;
case '4':huanbook();break;
case '0':break;
default:printf("无此操作\n");getch();break;
}
}while(ch1!='0');
}