📄 library.c
字号:
else /*还有此书的情况*/
fclose(fp); /*close book*/
fp=fopen("record.txt","a+");
student[n].cardnum=carnum;
fwrite(&student[n],sizeof(struct student),1,fp);
fclose(fp);
fp=fopen("book.txt","r"); /*打开书籍数据库*/
fp2=fopen("ExFile.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--; /*被借出一本,藏书数-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("\nBorrow seccessed!Press any key to continue.\n");
printf("\nDo you want to borrow another one? (Y/N)\n");
scanf("%s",&choise);
if(choise=='y'||choise=='Y')
{
clr();
goto ind1;
}
else
{
goto ind4;
}
}
else continue;
}
}
else continue;
}
{
printf("\nSORRY!The book that you want to borrow CANNOT BE FOUND.\nPlease check it,press any key to continue.\n"); /*对应上面找不到此图书时输出的语句*/
printf("\nDo you want to input the book name again? (Y/N)\n");
scanf("%s",&choise);
if(choise=='y'||choise=='Y')
{
fclose(fp);
clr();
goto ind1;
}
else
{
fclose(fp);
return;
}
}
}
}
printf("\nWRONG card number!Please check it!\n"); /*对应上面找不到对应卡号的情况*/
ind4:
fclose(fp);
printf("\nPress any Key to exit this mode.");
getch();
return;
}
showcard() /*显示所有的卡号资料*/
{
FILE *fp;
int i=0;
int n=0;
int p=0;
fp=fopen("card.txt","r");
for(i=0;fread(&card[i],sizeof(struct card),1,fp)!=0;i++)
{
if(i%5==4)
{
printf("CARD number : %d student name : %s class : %s\n",card[i].cardnum,card[i].studentname,card[i].studentclass);
n=n+1;
p++;
printf("\n\nPress Any Key to continue...\n");
getch();
clr();
}
else
{
printf("CARD number : %d student name : %s class : %s\n",card[i].cardnum,card[i].studentname,card[i].studentclass);
n=n+1;
p++;
}
}
fclose(fp);
printf("\nTotally has %d students.\n",n);
printf("The total number of students is : [ %d ]\n\n",p);
printf("Press any key to continue.\n");
printf("Press any Key to exit this mode.");
getch();
return;
}
cardcount() /*卡号对应资料显示,按卡号*/
{
FILE *fp,*fp2; /*fp为打开卡号数据库文件指针,fp2为查找对应借书资料的文件指针*/
int i,n,p; /*p为查找结果计数器,n为借书情况计数器*/
int cardnum;
char choise=0;
once:
n=0;
p=0;
printf("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) /*卡号存在*/
{
clr();
p++; /*查到了结果,计数器+1*/
printf("CARD number:%d Student Name :%s Class :%s\n\n",card[i].cardnum,card[i].studentname,card[i].studentclass);
fp2=fopen("record.txt","r"); /*打开借书操作数据库,调出借书的数据*/
for(i=0;fread(&student[i],sizeof(struct student),1,fp2)!=0;i++)
{
if(student[i].cardnum==cardnum)
{
printf("The book you borrowed: %s \n",student[i].lendbook);
n++; /*查到结果,计数器+1*/
}
else continue;
}
fclose(fp2);
printf("\nYou has borrowed %d books.\n",n);
getch();
}
else continue;
}
fclose(fp);
if(p==0) /*计数器为0,即没有找到书籍*/
{
printf("You have input a WRONG number,please CHECK it and input again.\n");
getch();
}
{
clr();
printf("\nDo you want to SEARCH once? (Y/N)\n");
scanf("%s",&choise);
if(choise=='y'||choise=='Y')
{
clr();
goto once;
}
else
{
return;
}
}
return;
}
cardname() /*卡号对应资料显示,按姓名*/
{
FILE *fp,*fp2; /*fp为打开卡号数据库文件指针,fp2为查找对应借书资料的文件指针*/
int i,n,p; /*p为查找结果计数器,n为借书情况计数器*/
char name[10];
int cardnum;
char choise=0;
once:
n=0;
p=0;
printf("Input your Name:\n");
scanf("%s",name);
fp=fopen("card.txt","r"); /*与数据库对照*/
for(i=0;fread(&card[i],sizeof(struct card),1,fp)!=0;i++)
{
if(strcmp(card[i].studentname,name)==0) /*姓名正确*/
{
p++;
clr();
cardnum=card[i].cardnum;
printf("CARD number:%d Student Name :%s Class :%s\n\n",card[i].cardnum,card[i].studentname,card[i].studentclass);
fp2=fopen("record.txt","r"); /*打开借书操作数据库,调出借书的数据*/
for(i=0;fread(&student[i],sizeof(struct student),1,fp2)!=0;i++)
{
if(student[i].cardnum==cardnum)
{
printf("The book you borrowed: %s \n",student[i].lendbook);
n++;
}
else continue;
}
fclose(fp2);
printf("\nYou has borrowed %d books.\n",n);
getch();
}
else continue;
}
fclose(fp);
if(p==0) /*未找到对应姓名*/
{
printf("You have input a WRONG name,please CHECK it and input again.\n");
getch();
}
{
clr();
printf("\nDo you want to SEARCH once? (Y/N)\n");
scanf("%s",&choise);
if(choise=='y'||choise=='Y')
{
clr();
goto once;
}
else
{
return;
}
}
return;
}
delcard() /*删除卡号*/
{
FILE *fp,*fp2;
int i;
int cardnum;
char choice;
fp=fopen("card.txt","r");
fp2=fopen("ExFile.txt","w");
printf("WARNING!\nThis operation will delete all the data of the card number you want to delete.\n"
"Are you sure want to do it?(Y/N)\n");
if(getchar()=='Y'||getchar()=='y') /*选择Y的话,执行操作*/
{
printf("Input the card number you want to delete.\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("Are you sure?(Y/N)\n");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
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");
getch();
return;
}
else /*对应第二次询问是否要删除*/
{
printf("Press any key to continue.\n");
getch();
return;
}
}
else /*对应第一次问是否要执行此操作*/
{
printf("Press any key to continue.\n");
getch();
return;
}
printf("Press any Key to exit this mode.");
getch();
return;
}
addcard() /*添加卡号操作*/
{
FILE *fp;
char choice='y';
int i=0;
jump:
clr();
fp=fopen("card.txt","a+");
while(choice=='y'||choice=='Y')
{
clr();
printf("Input your card number:\n");
scanf("%d",&card[i].cardnum);
printf("Input your name:\n");
scanf("%s",card[i].studentname);
printf("Input your class:\n");
scanf("%s",&card[i].studentclass);
fwrite(&card[i],sizeof(struct card),1,fp);
fclose(fp);
printf("Input seccessed!Press any key to continue.\n");
getch();
printf("Do you want to create another one?(Y/N)\n");
scanf("%s",&choice);
goto jump;
}
fclose(fp);
printf("Press any key to continue.\n");
printf("Press any Key to exit this mode.");
getch();
return;
}
changecard()
{
FILE *fp,*fp2;
char choice;
int i,cardnum;
fp=fopen("card.txt","r");
fp2=fopen("ExFile.txt","w"); /*写入到临时文件*/
printf("input the CAR NUMBER you want to change:\n");
scanf("%d",&cardnum);
for(i=0;fread(&card[i],sizeof(struct card),1,fp)!=0;i++)
{
clr();
if(cardnum==card[i].cardnum)
{
printf("\nCard number : %d student name : %s class : %s \n",card[i].cardnum,card[i].studentname,card[i].studentclass);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -