思路
- 加载文件至链表
- 选择功能
2.1 图书录入:使用头插法链表保存
2.2 图书列表显示:循环输出链表内容
2.3 借书:输入书籍ID,学生学号。判断该书籍是否可借,(是)记录数据,该书籍库存减一;(否)重新选择功能
2.4 还书:输入书籍ID,学生学号。判断该学生是否借过此书并未归还,(是)更改归还状态,书籍库存数加一;(否)重新选择功能
2.5 借书列表显示:循环输出链表内容
2.6 书籍删除:输入书籍ID。判断该书籍是否还有借出未归还的,(是)禁止删除;(否)找到该书籍,释放当前节点
思维导图
代码
文件加载
学生模块
void load_studentfile(char *filename,struct student *st){
FILE *fp = fopen(filename,"rb+");
struct student *new_st;
new_st = (struct student*)malloc(sizeof(struct student));
if(fp == NULL) {
printf("加载文件失败\n");
return;
}
while (fread(new_st, sizeof(struct student),1,fp) != 0){
new_st->next = st->next;
st->next = new_st;
new_st = (struct student*)malloc(sizeof(struct student));
}
fclose(fp);
return;
}
书籍模块
void load_bookfile(char *filename,struct book *head){
FILE *fp = fopen(filename,"rb+");
struct book *bk;
bk = (struct book*)malloc(sizeof(struct book));
if(fp == NULL) {
printf("加载文件失败\n");
return;
}
while (fread(bk, sizeof(struct book),1,fp) != 0){
bk->next = head->next;
head->next = bk;
bk = (struct book*)malloc(sizeof(struct book));
}
fclose(fp);
return;
}
主函数
int main(int argc,char* argv[])
{
struct book head;
struct student st;
int ncmd;
char ch;
/*初始化头结点*/
head.next = NULL;
st.next = NULL;
load_studentfile("student.bin",&st);
load_bookfile("book.bin",&head);
printf("----------------------------\n");
printf("1. 录入图书\n");
printf("2. 图书列表显示\n");
printf("3. 借书\n"