📄 library.c
字号:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#define STACK_INIT_SIZE 10000
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
returnbook();
lendbook();
addbook();
bookcount();
changebook();
delbook();
addcard();
delcard();
changecard();
cardcount();
struct card /*借书卡数据库*/
{
int cardnum; /*卡号*/
char studentname[10]; /*借书学生姓名*/
char studentclass[15]; /*学生所在班级*/
}card[100];
struct student /*定义学生类型,用于存放借出的书籍*/
{
int cardnum; /*借书卡号*/
char lendbook[10]; /*所借图书*/
}student[100];
struct book /*建立书籍数据库*/
{
int booknum; /*ISBN书号*/
char bookname[10]; /*书名*/
char bookcreat[10]; /*作者*/
int turefalse; /*管藏剩余数*/
int total; /*书籍总数*/
char publi[10]; /*出版社*/
}book[100];
clr() /*定义一个清屏函数*/
{
window(1,2,80,24);
textbackground(BLUE);
textcolor(WHITE);
clrscr();
}
returnbook() /*还书的函数*/
{
FILE *fp,*fp2,*fp3; /*定义两个文件指针,fp2为临时文件数据指针,打开ExFile数据库。写入均是先写入到ExFile,然后再从ExFile写回到原来的数据库文件中。防止意外操作造成错误数据*/
int i,n,p,q,r;
int cardnum;
char lendbook[10];
int lendnum;
char choise=0;
ind3: /*验证卡号工作*/
n=0;
printf("\nPlease input your card number:\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) /*卡号存在*/
{
fclose(fp);
printf("\nCARD number:%d Student Name :%s Class :%s \n\n",card[i].cardnum,card[i].studentname,card[i].studentclass);
fp=fopen("record.txt","r"); /*打开借书操作数据库,调出借书的数据*/
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)
{
if(student[i].cardnum==cardnum)
{
printf("The book you borrowed: %s \n",student[i].lendbook);
n++;
}
else continue;
}
fclose(fp);
printf("\nYou has borrowed %d books.\n",n);
printf("\nPress any key to continue.\n");
getch();
goto ind1;
}
else ;
}
{
fclose(fp);
printf("\nYou have input a WRONG number,please CHECK it and input again.\n");
printf("\nDo you want to input your number again? (Y/N)\n");
scanf("%s",&choise);
if(choise=='y'||choise=='Y')
{
clr();
choise=0;
goto ind3;
}
else
{
return;
}
}
clr();
ind1:
clr();
fp=fopen("card.txt","r"); /*从存储文件中读取数据进行操作*/
for(i=0;fread(&card[i],sizeof(struct card),1,fp)!=0;i++) /*判断卡号是否存在,从fp指向的card.txt中读取1个数据,放入到card[i]中*/
{
if(card[i].cardnum==cardnum) /*卡号存在的情况,读取的和输入的等价*/
{
ind2:
n=i;
fclose(fp);
printf("\nPlease input the ISBN NUMBER of the book that you return:\n");
scanf("%d",&lendnum);
fp3=fopen("book.txt","r"); /*打开BOOK数据库,调出数据,寻找ISBN对应的书名*/
for(q=0;fread(&book[q],sizeof(struct book),1,fp3)!=0;q++)
{
if(book[q].booknum==lendnum) /*找到对应资料*/
{
printf("\nISBN number : %d book name: %s author: %s public : %s\nNow we have [ %d ] These Books.",book[q].booknum,book[q].bookname,book[q].bookcreat,book[q].publi,book[q].turefalse);
for(r=0;r<=10;r++) /*把资料里面的书名放到借书函数书名中去*/
{
lendbook[r]=book[q].bookname[r];
}
fclose(fp3);
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) /*借阅了该书的情况,比较两个字符串,误差为0,即相同*/
{
fclose(fp);
fp=fopen("record.txt","r");
fp2=fopen("ExFile.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("ExFile.txt","r"); /*再写回到record数据库中*/
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("ExFile.txt","w"); /*清临时文件的记录*/
fclose(fp2);
fp=fopen("book.txt","r");
fp2=fopen("ExFile.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("ExFile.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("ExFile.txt","w"); /*清临时文件*/
fclose(fp2);
printf("\nThe record of the book you borrowed has been sccessed cleared,\n"
"press any key to continue.\n");
printf("\nDo you want to return another one? (Y/N)\n");
scanf("%s",&choise);
if(choise=='y'||choise=='Y')
{
clr();
choise=0;
goto ind1;
}
else
{
return;
}
}
else continue;
}
}
else continue;
} /*检测ISBN的FOR 循环的结束*/
{
printf("\nWARNING!The data of the ISBN NUMBER you input CANNOT been found in record.\n"); /*出错提示*/
printf("\nDo you want to return another one? (Y/N)\n");
scanf("%s",&choise);
if(choise=='y'||choise=='Y')
{
fclose(fp);
choise=0;
clr();
goto ind1;
}
else
{
fclose(fp);
return;
}
getch();
return;
}
}
}
printf("\nWRONG Card number!Please check it! \n"); /*出错提示*/
printf("\nDo you want to input your number again? (Y/N)\n");
scanf("%s",&choise);
if(choise=='y'||choise=='Y')
{
fclose(fp);
clr();
choise=0;
goto ind3;
}
else
{
fclose(fp);
return;
}
printf("\nPress any Key to exit this mode.");
getch();
return;
}
lendbook() /*借书的函数,与还书函数类似*/
{
FILE *fp,*fp2,*fp3;
int i,n,p,q;
static int carnum;
static int lendnum;
char choise;
char choi='0';
ind3:
n=0;
choise=0;
printf("\nInput your Card Number:\n");
scanf("%d",&carnum);
fp=fopen("card.txt","r"); /*与数据库对照*/
for(i=0;fread(&card[i],sizeof(struct card),1,fp)!=0;i++)
{
if(card[i].cardnum==carnum) /*卡号存在*/
{
fclose(fp); /*关闭card*/
printf("\nCARD number:%d Student Name :%s Class :%s \n\n",card[i].cardnum,card[i].studentname,card[i].studentclass);
fp=fopen("record.txt","r"); /*打开借书操作数据库,调出借书的数据*/
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)
{
if(student[i].cardnum==carnum)
{
printf("The book you borrowed: %s \n",student[i].lendbook);
n++;
}
else continue;
}
fclose(fp); /*关闭record*/
printf("\nYou has borrowed %d books.\n",n);
printf("\nPress any key to continue.\n");
getch();
clr();
goto ind1;
}
else ;
}
{
printf("\nYou have input a WRONG number,please CHECK it and input again.\n");
printf("\nDo you want to input your number again? (Y/N)\n");
scanf("%s",&choise);
if(choise=='y'||choise=='Y')
{
clr();
choise=0;
goto ind3;
}
else
{
return;
}
}
clr();
ind1:
clr();
choise=0;
n=0;
if(0) /*原本想在这里加入一个搜索索引页,但是实际编译后,调用索引之后会出现问题,故先将其屏蔽*/
{printf("\nDo you want to use the BOOK SEARCH ENGINE? (Y/N)\n");
scanf("%s",&choise);
if(choise=='y'||choise=='Y')
{
clr();
chabook();
choise=0;
}
else
{
clr();
}
}
ind2:
choise=0;
n=0;
fp=fopen("card.txt","r"); /*与数据库对照*/
for(i=0;fread(&card[i],sizeof(struct card),1,fp)!=0;i++)
{
if(card[i].cardnum==carnum) /*卡号存在*/
{
fclose(fp); /*close card*/
n=i;
printf("\nInput the ISBN NUMBER of the book you want to borrow:\n");
scanf("%d",&lendnum);
fp3=fopen("book.txt","r"); /*打开BOOK数据库,调出数据,寻找ISBN对应的书名*/
for(p=0;fread(&book[p],sizeof(struct book),1,fp3)!=0;p++)
{
if(book[p].booknum==lendnum) /*找到对应资料*/
{
printf("\nISBN number : %d book name: %s author: %s public : %s\nNow we have [ %d ] These Books.\n\n",book[p].booknum,book[p].bookname,book[p].bookcreat,book[p].publi,book[p].turefalse);
for(q=0;q<=10;q++) /*把资料里面的书名放到借书函数书名中去*/
{
student[n].lendbook[q]=book[p].bookname[q];
}
fclose(fp3);
fp=fopen("book.txt","r"); /*打开书籍数据库*/
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(book[i].booknum==lendnum)
{
if(book[i].turefalse==0) /*判断是否能借阅,0为没有剩余.*/
{
printf("\nSORRY!All of these books had been borrowed OUT!\nPress any key to continue.\n");
printf("\nDo you want to borrow another one? (Y/N)\n");
scanf("%s",&choise);
if(choise=='y'||choise=='Y')
{
fclose(fp); /*close book*/
clr();
goto ind1;
}
else
{
fclose(fp);
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -