#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
typedef struct book{
char name[5];
char id[8]; //存八位编号
int loaned; //借出
int num; //总数
double price;
char publisher[5];
char author[5];
struct book*next;
}book;
typedef struct student{
char id[8];
char name[5];
int overdue; //逾期数
char borrow_book_name[5][8]; //借书数,最大为5,用编号存
int borrow_book_num;
struct student*next;
}student;
book* Initialisation_book(){
book *tail=NULL,*Head=NULL,*sd;
FILE*fp=NULL;
fp=fopen("library.dat","r");
if(fp==NULL){
printf("打开文件失败!");
exit(1);
}
while(!feof(fp)){
sd=(book*)malloc(sizeof(book));
sd->next=NULL;
fread(sd,sizeof(book),1,fp);
// printf("%s\n",sd->name);
if(Head){
tail->next=sd;
tail=tail->next;
}else{
Head=sd;
tail=sd;
}
}
tail=NULL;
return Head;
}
student*Initialisation_student(){
student *tail=NULL,*Head=NULL,*sd;
FILE*fp=NULL;
fp=fopen("student.dat","r");
if(fp==NULL){
printf("文件打开失败!");
exit(1);
}
while(!feof(fp)){
sd=(student*)malloc(sizeof(student));
sd->next=NULL;
fread(sd,sizeof(student),1,fp);
if(Head){
tail->next=sd;
tail=tail->next;
}else{
Head=sd;
tail=sd;
}
}
tail=NULL;
return Head;
}
/×udent.dat里面有
//"123456","asd",1,"格林",1
/Pbrary.dat里面有
//“asd”,“1001”,1,5,56,“yangguang”,“huangyi”
//Head是图书馆,borrow_book是要借的书的编号,Student是借书的同学的指针
void borrow(book*Head,char*borrow_book,student*Student){
book*pre=Head;
while(pre){
if(strcmp(pre->id,borrow_book)==0){ //if这一步没进去过,而且一直循环没出来
pre->loaned++;
strcpy(Student->borrow_book_name[Student->borrow_book_num],borrow_book);
break;
}
pre=pre->next;
}
printf("借书成功啦!");
}
int main(){
// student sd = {"123456","asd",1,"格林",1};
// FILE*fp=NULL;
// fp=fopen("student.dat","w");
// fwrite(&sd,sizeof(student),1,fp);
book *Head=Initialisation_book();
student*Student=Initialisation_student();
borrow(Head,"1001",Student);
return 0;
}