📄 c.txt
字号:
#include "stdio.h"
#include "stdlib.h"
struct book {
int id;
char name[100];
char author[100];
int lent_student;
char time[100];
struct book *next;
};
struct student {
int id;
char name[100];
struct student *next;
};
struct book *book_head=NULL;
struct student *student_head=NULL;
int inLib=0;
void save()
{
FILE *fp;
struct book *p1;
struct student *p2;
fp=fopen("books.txt","wb");
p1=book_head->next;
while(p1!=NULL)
{
fwrite(p1,sizeof(struct book),1,fp);
p1=p1->next;
}
fclose(fp);
fp=fopen("students.txt","wb");
p2=student_head->next;
while(p2!=NULL)
{
fwrite(p2,sizeof(struct student),1,fp);
p2=p2->next;
}
fclose(fp);
}
void load()
{
FILE *fp;
struct book *p1,*p1_end;
struct student *p2,*p2_end;
fp=fopen("books.txt","rb");
if(fp!=NULL)
{
p1_end=book_head;
while(!feof(fp))
{
p1=(struct book *)malloc(sizeof(struct book));
fread(p1,sizeof(struct book),1,fp);
p1_end->next=p1;
p1->next=NULL;
if(p1->lent_student!=0) inLib++;
p1_end=p1;
}
fclose(fp);
}
fp=fopen("students.txt","rb");
if(fp!=NULL)
{
p2_end=student_head;
while(!feof(fp))
{
p2=(struct student *)malloc(sizeof(struct student));
fread(p2,sizeof(struct student),1,fp);
p2_end->next=p2;
p2->next=NULL;
p2_end=p2;
}
fclose(fp);
}
}
void main()
{
int s;
struct book *p1,*p3;
struct student *p2,*p4;
int id;
book_head=(struct book *)malloc(sizeof(struct book));
book_head->next=NULL;
student_head=(struct student *)malloc(sizeof(struct student));
student_head->next=NULL;
load();
do
{
printf("\n 图书馆管理 \n");
printf("1.添加书 2.删除书\n");
printf("3.添加学生 4.删除学生\n");
printf("5.借书 6.还书\n");
printf("0.退出\n");
printf("目前存书%d册\n",inLib);
printf("请做选择:");
scanf("%d",&s);
switch(s)
{
case 1:
p3=(struct book *)malloc(sizeof(struct book));
printf("请输入书的编号:");
scanf("%d",&p3->id);
printf("请输入书名:");
scanf("%s",p3->name);
printf("请输入作者:");
scanf("%s",p3->author);
p3->lent_student=0;
p3->next=NULL;
for(p1=book_head;p1->next!=NULL;p1=p1->next);
p1->next=p3;
printf("添加完成!\n");
inLib++;
break;
case 2:
printf("请输入要删除的书的编号:");
scanf("%d",&id);
for(p1=book_head;p1->next!=NULL;p1=p1->next)
{
if(p1->next->id==id)
{
p3=p1->next;
p1->next=p3->next;
free(p3);
break;
}
}
printf("删除成功\n");
break;
case 3:
p4=(struct student *)malloc(sizeof(struct student));
printf("请输入学号:");
scanf("%d",&p4->id);
printf("请输入姓名:");
scanf("%s",p4->name);
for(p2=student_head;p2->next!=NULL;p2=p2->next);
p2->next=p4;
p4->next=NULL;
printf("添加完成!\n");
inLib--;
break;
case 4:
printf("请输入要删除的学生学号:");
scanf("%d",&id);
for(p2=student_head;p2->next!=NULL;p2=p2->next)
{
if(p2->next->id==id)
{
p4=p2->next;
p2->next=p4->next;
free(p4);
break;
}
}
printf("删除成功\n");
break;
case 5:
printf("请输入要借的书的编号:");
scanf("%d",&id);
for(p1=book_head->next;p1!=NULL;p1=p1->next)
{
if(p1->id==id) break;
}
if(p1==NULL)
{
printf("找不到这本书\n");
break;
}
if(p1->lent_student!=0)
{
printf("这本书已经被借出了!\n");
break;
}
printf("请输入要借书的学生学号:");
scanf("%d",&id);
for(p2=student_head;p2!=NULL;p2=p2->next)
{
if(p2->id==id) break;
}
if(p2==NULL)
{
printf("找不到这名学生\n");
break;
}
printf("请输入借书时间:");
scanf("%s",p1->time);
p1->lent_student=id;
printf("借书成功!\n");
inLib--;
break;
case 6:
printf("请输入要还的书的编号:");
scanf("%d",&id);
for(p1=book_head->next;p1!=NULL;p1=p1->next)
{
if(p1->id==id) break;
}
if(p1==NULL)
{
printf("找不到这本书\n");
break;
}
if(p1->lent_student==0)
{
printf("这本书还没有被借出!\n");
break;
}
p1->lent_student=0;
printf("还书成功\n");
inLib++;
}
}
while(s!=0);
save();
while(book_head!=NULL)
{
p1=book_head->next;
free(book_head);
book_head=p1;
}
while(student_head!=NULL)
{
p2=student_head->next;
free(student_head);
student_head=p2;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -