📄 library.c
字号:
printf("select what you want to change:(input 0-3)\n\n");
printf("1 : change the CARD number\n");
printf("2 : change the name\n");
printf("3 : change the class\n");
printf("0 : <EXIT>\n");
printf("Please slect one:\n");
scanf("%s",&choice);
clr();
switch(choice)
{
case '1':
{
printf("input new CARD number:\n");
scanf("%d",&card[i].cardnum);
fwrite(&card[i],sizeof(struct card),1,fp2);
}break;
case '2':
{
printf("input new name:\n");
scanf("%s",card[i].studentname);
fwrite(&card[i],sizeof(struct card),1,fp2);
}break;
case '3':
{
printf("input new class:\n");
scanf("%s",card[i].studentclass);
fwrite(&card[i],sizeof(struct card),1,fp2);
}break;
case '0':
{
printf("\nPress any key to EXIT this mode.\n");
getch();
};break;
default:printf("WRONG select!");break;
}
continue;
} /*修改结束*/
fwrite(&card[i],sizeof(struct card),1,fp2); /*写入修改后的结果*/
}
fclose(fp);
fclose(fp2);
fp=fopen("card.txt","w");
fp2=fopen("ExFile.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("ExFile.txt","w");
fclose(fp2);
printf("Press any key to continue.\n");
getchar();
printf("Press any Key to exit this mode.");
getch();
return;
}
chabook() /*图书查阅索引页*/
{
int ch5=1;
ind1:
ch5=1;
while(ch5!=0)
{
clr();
printf("\n--------------- SEARCH INDEX --------------\n");
printf(" (Input 0-4 to slect search mode)\n\n");
printf(" 1 : <BY book name>\n");
printf(" 2 : <BY author>\n");
printf(" 3 : <BY public>\n");
printf(" 4 : <BY ISBN number>\n");
printf(" 0 : <BACK>\n");
scanf("%d",&ch5); /*读入上面的选择*/
switch(ch5) /*选择的判断*/
{
case 1:{clr();findbook();goto ind1;};
case 2:{clr();bookaut();goto ind1;};
case 3:{clr();bookpub();goto ind1;};
case 4:{clr();booknum();goto ind1;};
case 0:{clr();return;};
default:{clr();printf("WRONG select!\n");getch();goto ind1;};
}
}
return;
}
showbook() /*显示所有的书籍,包括书目数量和书本数量的统计*/
{
FILE *fp;
int i=0;
int n=0;
int p=0;
fp=fopen("book.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(i%5==4)
{
printf("ISBN number : %d book name : %s author : %s public : %s\nNow we have [ %d ] These Books.\n",book[i].booknum,book[i].bookname,book[i].bookcreat,book[i].publi,book[i].turefalse);
n=n+1;
p=p+book[i].turefalse;
printf("\n\nPress Any Key to continue...\n");
getch();
clr();
}
else
{printf("ISBN number : %d book name : %s author : %s public : %s\nNow we have [ %d ] These Books.\n",book[i].booknum,book[i].bookname,book[i].bookcreat,book[i].publi,book[i].turefalse);
n=n+1;
p=p+book[i].turefalse;
}
}
fclose(fp);
printf("\nTotally has %d books.\n",n);
printf("The total number of books is : [ %d ]\n\n",p);
printf("Press any key to continue.\n");
printf("Press any Key to exit this mode.");
getch();
return;
}
addbook() /*添加书籍*/
{
FILE *fp;
int i=0;
char choice='y';
fp=fopen("book.txt","a+");
while(choice=='y'||choice=='Y')
{
clr();
i++;
printf("Input the ISBN number:\n");
scanf("%d",&book[i].booknum);
printf("input the book name:\n");
scanf("%s",book[i].bookname);
printf("input the author:\n");
scanf("%s",book[i].bookcreat);
printf("input the public:\n");
scanf("%s",book[i].publi);
printf("Input the number of the book we have:\n");
scanf("%d",&book[i].total);
book[i].turefalse=book[i].total;
fwrite(&book[i],sizeof(struct book),1,fp);
printf("Do you want to create another one?(Y/N)\n");
scanf("%s",&choice);
}
fclose(fp);
printf("Press any Key to exit this mode.");
getch();
return;
}
findbook() /*书籍查找,按书名*/
{
FILE *fp,*fp2; /*fp为书名对照打开数据库文件指针,fp2为打开借书记录数据库文件指针*/
char bookname[10]; /*存放书名*/
int ture,i,n=0; /*ture为是否查到结果的计数器,n为统计被借多少次的计数器*/
char choise=0;
ind1:
n=0;
ture=0;
fp=fopen("book.txt","r");
printf("\nInput the book name:\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(bookname,book[i].bookname)==0) /*对照书名*/
{
ture++; /*找到相应结果,查找结果计数器+1*/
clr();
if(book[i].turefalse!=0) /*书籍剩余册数判断*/
{
printf("\nISBN number : %d book name: %s author: %s public : %s\nNow we have [ %d ] These Books,you can borrow it.\n\n",book[i].booknum,book[i].bookname,book[i].bookcreat,book[i].publi,book[i].turefalse);
fp2=fopen("record.txt","r"); /*打开借书操作数据库,调出借书的数据,查看此书被哪些人借阅*/
for(i=0;fread(&student[i],sizeof(struct student),1,fp2)!=0;i++)
{
if(strcmp(student[i].lendbook,bookname)==0)
{
printf("These books had been borrowed by this card number: %d \n",student[i].cardnum); /*出于保护隐私,这里不把卡号对应到名字上去。对应的话,用for循环再调用一次card.txt,然后寻找对应即可。*/
n++; /*找到一个记录,借出记录计数器+1*/
}
else continue;
}
fclose(fp2);
printf("\nThis book already be borrowed by [ %d ] peoples.\n",n);
getch();
}
else
{
printf("\nISBN number : %d book name: %s author: %s public : %s\nNow we have [ %d ] These Books.",book[i].booknum,book[i].bookname,book[i].bookcreat,book[i].publi,book[i].turefalse);
printf("\nSORRY ! All of these books had been borrowed.\n");
getch();
}
}
else continue;
}
fclose(fp); /*关闭BOOK数据库*/
if(ture==0) /*查找结果计数器为0,即没找到结果*/
{
printf("\nWe don't have this book,please check it.\n");
getch();
}
{
clr();
printf("\nDo you want to input the book name once? (Y/N)\n");
scanf("%s",&choise);
if(choise=='y'||choise=='Y')
{
clr();
goto ind1;
}
else
{
clr();
return;
}
}
return;
}
booknum() /*书籍查找,按ISBN号*/
{
FILE *fp,*fp2;
int ISBN;
int ture,i,n=0;
char choise=0;
char bookname[10];
ind1:
n=0;
ture=0;
fp=fopen("book.txt","r");
printf("\ninput the ISBN number:\n");
scanf("%d",&ISBN);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(book[i].booknum==ISBN)
{
ture++;
clr();
*bookname=*book[i].bookname;
if(book[i].turefalse!=0) /*书籍状态判断*/
{
printf("\nISBN number : %d book name: %s author: %s public : %s\nNow we have [ %d ] These Books,you can borrow it.\n\n",book[i].booknum,book[i].bookname,book[i].bookcreat,book[i].publi,book[i].turefalse);
fp2=fopen("record.txt","r"); /*打开借书操作数据库,调出借书的数据,查看此书被哪些人借阅*/
for(i=0;fread(&student[i],sizeof(struct student),1,fp2)!=0;i++)
{
if(strcmp(student[i].lendbook,bookname)==0)
{
printf("These books had been borrowed by this card number: %d \n",student[i].cardnum);
n++;
}
else continue;
}
fclose(fp2);
printf("\nThis book already be borrowed by [ %d ] peoples.\n",n);
getch();
}
else
{
printf("\nISBN number : %d book name: %s author: %s public : %s\nNow we have [ %d ] These Books,you can borrow it.\n\n",book[i].booknum,book[i].bookname,book[i].bookcreat,book[i].publi,book[i].turefalse);
printf("\nSORRY ! All of these books had been borrowed.\n");
getch();
}
}
else continue;
}
fclose(fp);
if(ture==0)
{
printf("\nWe don't have this book,please check it.\n");
getch();
}
{
clr();
printf("\nDo you want to input the book name once? (Y/N)\n");
scanf("%s",&choise);
if(choise=='y'||choise=='Y')
{
clr();
goto ind1;
}
else
{
clr();
return;
}
}
return;
}
bookaut() /*书籍查找,按作者*/
{
FILE *fp,*fp2; /*fp2为书籍被借情况的文件指针*/
char bookname[10];
char bookaut[10];
int ture,p,i,n=0;
char choise=0;
ind1:
n=0;
p=0; /*p为是否查到书籍的计数器*/
fp=fopen("book.txt","r");
printf("\ninput the book author:\n");
scanf("%s",bookaut);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(bookaut,book[i].bookcreat)==0)
{
clr();
p++;
*bookname=*book[i].bookname;
if(book[i].turefalse!=0) /*书籍状态判断*/
{
printf("\nISBN number : %d book name: %s author: %s public : %s\nNow we have [ %d ] These Books,you can borrow it.\n\n",book[i].booknum,book[i].bookname,book[i].bookcreat,book[i].publi,book[i].turefalse);
fp2=fopen("record.txt","r"); /*打开借书操作数据库,调出借书的数据,查看此书被哪些人借阅*/
for(i=0;fread(&student[i],sizeof(struct student),1,fp2)!=0;i++)
{
if(strcmp(student[i].lendbook,bookname)==0)
{
printf("These books had been borrowed by this card number: %d \n",student[i].cardnum); /*出于保护隐私,这里不把卡号对应到名字上去。对应的话,用for循环再调用一次card.txt,然后寻找对应即可。*/
n++;
}
else continue;
}
fclose(fp2);
printf("\nThis book already be borrowed by [ %d ] peoples.\n",n);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -