使用Visual Studio 2022编译工具进行编写代码的。
项目源码直接奉上:
book1.h头文件:
#ifndef __BOOK1_H //预处理用于条件编译 避免头文件反复包含
#define __BOOK1_H#include<stdio.h>
#include <string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>//显示当前时间#define MAX_BOOK 1000
#define MAX_BOOK1 100 //每个编号图书数量的最大限制
#define MAX_STUBOOK 10 //读者借书的最大限制//图书信息
typedef struct book_str {unsigned int ID; //图书编号char name[MAX_BOOK1]; //图书名字char anthor[MAX_BOOK1];//作者unsigned int price; //单本书价格unsigned int num; //图书数量char location[MAX_BOOK1]; //图书位置
}BOOK;typedef struct book_str1 {struct book_str book;struct book_str1* next;
}BookList;//登录信息
typedef struct stu {char name[MAX_BOOK1]; //用户名char password[20]; //密码 unsigned long int phone;//电话
}USER;typedef struct stu1 {USER st;struct stu1* next;
}UserLog;//图书借阅
typedef struct book_borr
{char stuname[MAX_BOOK1]; //学生姓名unsigned int ID; //图书IDchar name[MAX_BOOK1]; //图书名字char state[MAX_BOOK1];//借书的状态char time[MAX_BOOK1]; //time 保存20个字节就可以了int num;//允许借书的最大值}Borrow;typedef struct book_borr1
{struct book_borr data_book;struct book_borr1* next;
}BorrowList;void fun();
void fun1();
void fun2();
//创建图书借阅的链表
BorrowList* init_borrow();
//尾插法--插入原始数据
bool tail_borrow(BorrowList* head, char stuname[MAX_BOOK1], unsigned int ID, char name[MAX_BOOK1], char state[MAX_BOOK1], char time[MAX_BOOK1]);
//用户记录--借阅信息
//尾插法
bool tail_borrow1(BorrowList* head, BookList* head1, char stuname[MAX_BOOK1],unsigned int ID, char name[MAX_BOOK1]);
//找到借阅的人
bool find_borrow(BorrowList* head, char stuname[MAX_BOOK1]);
//用户归还图书
bool del_borrow1(BorrowList* head, BookList* head1, char stuname[MAX_BOOK1], unsigned int ID, char name[MAX_BOOK1]);
//遍历链表
void print_borrow(BorrowList* head);
//回收链表
void free_borrow(BorrowList* head);//创建图书链表
BookList* init_book();
//尾插法
bool tail_book(BookList* head);
//按编号进行排序
void sort_book(BookList* head);
//删除图书
bool del_book(BookList* head, unsigned int ID);
//删除一本图书
bool del_book1(BookList* head, unsigned int ID);
//查询图书---按编号查找
bool find_book(BookList* head, unsigned int ID);
//查询图书---按书名查找
bool find_bookname(BookList* head, char name[MAX_BOOK1]);
//查询图书---按作者查找
bool find_anthorname(BookList* head, char anthor[MAX_BOOK1]);
//修改数据
bool rework_book(BookList* head, unsigned int ID, unsigned int sum);
//向链表中插入原始数据
bool tail_book1(BookList* head, unsigned int ID, char name[MAX_BOOK1], char anthor[MAX_BOOK1], unsigned int price, unsigned int num);
//链表长度
int length_book(BookList* head);
//遍历
void print_book(BookList* head);
//回收链表
void free_book(BookList* head);
//int count;
//写文件--将文件中的信息写入到链表中 图书信息
void file_readbook(BookList* head);
//写文件--将链表中的信息写到文件中 图书信息
void file_savebook(BookList* head);
//学生借阅信息表--从链表写入到文件
void file_savestubook(BorrowList* head);
//学生借阅信息表--从文件写入到链表
void file_readstuook(BorrowList* head);
//写文件--将文件中的信息写入到链表中 图书信息
void file_readbook1(BookList* head);
//写文件--将链表中的信息写到文件中 图书信息
void file_savebook1(BookList* head);
//用户登录信息表--从链表写入到文件
void file_saveuser(UserLog* head);
//用户登录信息表--从文件写入到链表
void file_readuser(UserLog* head);//图书信息查找---按编号,书名,作者
bool book_find(BookList* head);//创建用户的链表
UserLog* init_user();
//插入用户账号信息
bool tail_user(UserLog* head, char user[MAX_BOOK1], char password[20], unsigned long int phone);
//注册用户
bool register_user(UserLog* head);
//找到某个用户信息
bool find_user(UserLog* head, char name[MAX_BOOK1]);
//修改用户个人信息
bool rework_user(UserLog* head, char name[MAX_BOOK1]);
//遍历用户链表
void print_user(UserLog* head);
//回收链表
void free_user(UserLog* head);//管理员登录
void admin_log();
//登录操作
bool user_log(UserLog* head);
//用户登录
void student_log(char stuanme[MAX_BOOK1]);#endif
#pragma once
main.c 文件:
#include "book1.h"//提示页面
void fun()
{printf("----------------------------------------------------------------------------------\n");printf("= 欢迎使用图书管理软件 =\n");printf("----------------------------------------------------------------------------------\n");printf("= 请选择功能 =\n");printf("= 0.退出系统 =\n");printf("= 1.用户登录 =\n");printf("= 2.用户注册 =\n");printf("----------------------------------------------------------------------------------\n");}void fun1()
{printf("----------------------------------------------------------------------------------\n");printf("= 管理员登录 =\n");printf("----------------------------------------------------------------------------------\n");printf("= 0.退出系统 =\n");printf("= 1.图书汇总表 2.添加书籍 =\n");printf("= 3.删除书籍 4.查找书籍 =\n");printf("= 5.修改图书 6.图书借阅 =\n");printf("= 7.查看用户信息 8.图书库存 =\n");printf("----------------------------------------------------------------------------------\n");
}void fun2()
{printf("----------------------------------------------------------------------------------\n");printf("= 用户登录 =\n");printf("----------------------------------------------------------------------------------\n");printf("= 0.退出系统 =\n");printf("= 1.图书借阅 2.图书归还 =\n");printf("= 3.图书查阅 4.信息修改 =\n");printf("----------------------------------------------------------------------------------\n");
}//创建用户的链表
UserLog* init_user()
{UserLog* head = (UserLog*)malloc(sizeof(UserLog));if (head == NULL){printf("当前没有用户,请先创建\n");return NULL;}head->next = NULL;return head;
}
//插入用户账号信息
bool tail_user(UserLog* head, char user[MAX_BOOK1], char password[20], unsigned long int phone)
{bool find_flag = false;UserLog* new_node = (UserLog*)malloc(sizeof(UserLog));if (new_node == NULL) return false;strcpy_s(new_node->st.name,MAX_BOOK1, user);strcpy_s(new_node->st.password, 20,password);new_node->st.phone = phone;new_node->next = NULL;UserLog* p = head;while (p != NULL){if (p->next == NULL){find_flag = true;break;}p = p->next;}if (find_flag == false){printf("插入失败,该用户已存在,请登录!\n");}p->next = new_node;return true;
}//注册用户
bool register_user(UserLog* head)
{bool find_flag = false;UserLog* new_node = (UserLog*)malloc(sizeof(UserLog));if (new_node == NULL)return false;printf("请依次输入账号, 密码, 电话:");scanf_s("%s", new_node->st.name,MAX_BOOK1);scanf_s("%s", new_node->st.password, 20);scanf_s("%ld", &new_node->st.phone);new_node->next = NULL;UserLog* p = head;while (p != NULL){if (strcmp(p->st.name,new_node->st.name) == 0){find_flag = false;break;}if (p->next == NULL){find_flag = true;break;}p = p->next;}if (find_flag == false){printf("库中已有该账号,添加失败!!!\n");return false;}else{printf("已成功添加!!!\n");p->next = new_node;}return true;
}
//找到某个用户信息
bool find_user(UserLog* head, char name[MAX_BOOK1])
{bool find_flag = false;UserLog* p = head->next;if (p == NULL) return false;printf("\n-----------------------------------------------------\n");printf("\n=====================用户信息表======================\n");printf("%-15s %-15s %-15s\n", "账号", "密码", "电话");while (p != NULL){if (strcmp(p->st.name, name) == 0){find_flag = true;printf("%-15s %-15s %-15ld\n", p->st.name, p->st.password, p->st.phone);}p = p->next;}if (find_flag == false){printf("没找到该用户\n");}return true;
}
//修改用户个人信息
bool rework_user(UserLog* head,char name[MAX_BOOK1])
{UserLog* p = head->next;if (p == NULL) return false;printf("\n-----------------------------------------------------\n");printf("\n=====================用户信息表======================\n");printf("%-15s %-15s %-15s\n", "账号", "密码", "电话");while (p != NULL){if (strcmp(p->st.name, name) == 0){printf("%-15s %-15s %-15ld\n", p->st.name, p->st.password, p->st.phone);printf("----------------------------------------------------------------------------------\n");printf("= 请选择要修改的信息 =\n");printf("----------------------------------------------------------------------------------\n");printf("= 0.退出修改 =\n");printf("= 1.修改账号名称 2.修改密码 =\n");printf("= 3.修改手机号 =\n");printf("----------------------------------------------------------------------------------\n");while (1){int n = 0;printf("select>");scanf_s("%d", &n);switch (n){case 0:fun2();return false;case 1:{char stuname[MAX_BOOK1];printf("请输入要修改的名称:");scanf_s("%s", stuname, MAX_BOOK1);strcpy(p->st.name, stuname);printf("\n-----------------------------------------------------\n");printf("\n=====================用户信息表======================\n");printf("%-15s %-15s %-15ld\n", p->st.name, p->st.password, p->st.phone);}break;case 2:{char password[20];printf("请输入要修改的密码:");scanf_s("%s", password, 20);strcpy(p->st.password,password);printf("\n-----------------------------------------------------\n");printf("\n=====================用户信息表======================\n");printf("%-15s %-15s %-15ld\n", p->st.name, p->st.password, p->st.phone);break;}case 3:{unsigned int phone;printf("请输入要修改的电话:");scanf_s("%d", &phone);p->st.phone = phone;printf("\n-----------------------------------------------------\n");printf("\n=====================用户信息表======================\n");printf("%-15s %-15s %-15ld\n", p->st.name, p->st.password, p->st.phone);break;}default:printf("您输入的数据无效,请重新输入!!!\n");break;}}}p = p->next;}return true;
}
//遍历用户链表
void print_user(UserLog* head)
{UserLog* p = head->next;if (p == NULL)return;printf("\n-----------------------------------------------------\n");printf("\n=====================用户信息表======================\n");printf("%-15s %-15s %-15s\n", "账号", "密码","电话");while (p != NULL){printf("%-15s %-15s %-15ld\n", p->st.name,p->st.password,p->st.phone);p = p->next;}
}
//回收链表
void free_user(UserLog* head)
{UserLog* p = head->next, * q = p;if (p == NULL)return;while (p != NULL){q = p->next;free(p);p = q;}
}//创建图书借阅的链表
BorrowList* init_borrow()
{BorrowList* head = (BorrowList*)malloc(sizeof(BorrowList));if (head == NULL){printf("当前没有书籍,请先创建\n");return NULL;}head->next = NULL;return head;
}
//尾插法--插入原始数据
bool tail_borrow(BorrowList* head, char stuname[MAX_BOOK1],unsigned int ID, char name[MAX_BOOK1], char state[MAX_BOOK1], char time[MAX_BOOK1])
{bool find_flag = false;BorrowList* new_node = (BorrowList*)malloc(sizeof(BorrowList));if (new_node == NULL) return false;strcpy_s(new_node->data_book.stuname, MAX_BOOK1, stuname);new_node->data_book.ID = ID;strcpy_s(new_node->data_book.name, MAX_BOOK1, name);strcpy_s(new_node->data_book.state, MAX_BOOK1, state);strcpy_s(new_node->data_book.time,MAX_BOOK1,time);new_node->next = NULL;BorrowList* p = head;while (p != NULL){if (p->next == NULL){find_flag = true;break;}p = p->next;}if (find_flag == false){printf("插入失败!\n");}p->next = new_node;return true;
}//用户记录--借阅信息
//尾插法
bool tail_borrow1( BorrowList* head, BookList* head1,char stuname[MAX_BOOK1], unsigned int ID, char name[MAX_BOOK1])
{// 获取当前时间的秒数time_t currentTime = time(NULL);// 将秒数转换为本地时间的结构体struct tm* localTime = localtime(¤tTime);// 格式化本地时间为字符串char timeString[MAX_BOOK1];strftime(timeString, sizeof(timeString), "%Y%m%d %H:%M:%S", localTime);// 输出当前时间printf("\n当前时间:%s\n", timeString);bool find_flag = false;BorrowList* new_node = (BorrowList*)malloc(sizeof(BorrowList));if (new_node == NULL)return false;//printf("请依次输入借阅人,图书ID,图书名称:");//scanf_s("%s", &new_node->data_book.stuname, MAX_BOOK1);//scanf_s("%d", &new_node->data_book.ID);//scanf_s("%s", new_node->data_book.name, MAX_BOOK1);strcpy_s(new_node->data_book.stuname, MAX_BOOK1, stuname); //学生名字new_node->data_book.ID = ID;//图书编号strcpy_s(new_node->data_book.name, MAX_BOOK1, name); //书名strcpy_s(new_node->data_book.time, MAX_BOOK1, timeString); //当前时间strcpy_s(new_node->data_book.state, MAX_BOOK1, "已借阅");new_node->next = NULL;BookList* q = head1;while (q != NULL){if ((q->book.ID == new_node->data_book.ID) && (q->book.num > 0)) //用来判断用户所借书的ID与现有图书的ID是否相等以及该编号的图书是否还有库存{q->book.num--; //现有图书数量就会减少一本find_flag = true;break;}q = q->next;}if (find_flag == false){printf("登记失败,没有该编号的图书!!!\n");return false;}BorrowList* p = head;p->data_book.num = 0;while (p != NULL){if (strcmp(p->data_book.stuname, new_node->data_book.stuname) == 0 && strcmp(p->data_book.state,"已借阅")){printf("借书数量:%d\n", p->data_book.num);if (p->data_book.num > MAX_STUBOOK) //判断读者借的图书是否超过限制{find_flag = false;printf("借书超过限制!!!\n");break;}p->data_book.num++;}//否则就将用户的借阅信息利用尾插法记入else if (p->next == NULL){//num++;find_flag = true;p->next = new_node;break;}p = p->next;}return true;
}//找到借阅的人
bool find_borrow(BorrowList* head, char stuname[MAX_BOOK1])
{bool find_flag = false;BorrowList* p = head->next;if (p == NULL) return false;printf("\n---------------------------------------------------------------------------\n");printf("\n============================图书借阅表=====================================\n");printf("%-10s %-15s %-15s %-15s %-15s\n", "借阅人", "图书编号", "借阅状态", "图书名字", "借阅日期");while (p != NULL){if ((strcmp(p->data_book.stuname, stuname) == 0)){find_flag = true;printf("%-10s %-15d %-15s %-15s %s\n", p->data_book.stuname, p->data_book.ID, p->data_book.name, p->data_book.state, p->data_book.time);//break;}p = p->next;}return true;
}//查找已借阅人的信息
bool find_borrow1(BorrowList* head)
{bool find_flag = false;BorrowList* p = head->next;if (p == NULL) return false;printf("\n---------------------------------------------------------------------------\n");printf("\n============================图书借阅表=====================================\n");printf("%-10s %-15s %-15s %-15s %-15s\n", "借阅人", "图书编号", "借阅状态", "图书名字", "借阅日期");while (p != NULL){if ((strcmp(p->data_book.state, "已借阅") == 0)){find_flag = true;printf("%-10s %-15d %-15s %-15s %s\n", p->data_book.stuname, p->data_book.ID, p->data_book.name, p->data_book.state, p->data_book.time);//break;}p = p->next;}return true;
}
//查找已归还人的信息
bool find_borrow2(BorrowList* head)
{bool find_flag = false;BorrowList* p = head->next;if (p == NULL) return false;printf("\n---------------------------------------------------------------------------\n");printf("\n============================图书借阅表=====================================\n");printf("%-10s %-15s %-15s %-15s %-15s\n", "借阅人", "图书编号", "借阅状态", "图书名字", "借阅日期");while (p != NULL){if ((strcmp(p->data_book.state, "已归还") == 0)){find_flag = true;printf("%-10s %-15d %-15s %-15s %s\n", p->data_book.stuname, p->data_book.ID, p->data_book.name, p->data_book.state, p->data_book.time);//break;}p = p->next;}return true;
}//管理员查找借阅的信息
void find_borrowfun(BorrowList *head)
{printf("----------------------------------------------------------------------------------\n");printf("= 请选择要查阅的信息 =\n");printf("----------------------------------------------------------------------------------\n");printf("= 0.退出查阅 =\n");printf("= 1.查找借阅人 2.查找已借阅的人 =\n");printf("= 3.查找已归还的人 =\n");printf("----------------------------------------------------------------------------------\n");while (1){int n = 0;printf("select>");scanf_s("%d", &n);switch (n){case 0:fun1();return;case 1:{char stuname[MAX_BOOK1];printf("请输入要查找的借阅人:");scanf_s("%s", stuname, MAX_BOOK1);find_borrow(head, stuname);}break;case 2:{find_borrow1(head);break;}case 3:{find_borrow2(head);break;}default:printf("您输入的数据无效,请重新输入!!!\n");break;}}
}//用户归还图书
bool del_borrow1(BorrowList* head, BookList* head1, char stuname[MAX_BOOK1], unsigned int ID, char name[MAX_BOOK1])
{bool find_flag = false;// 获取当前时间的秒数time_t currentTime = time(NULL);// 将秒数转换为本地时间的结构体struct tm* localTime = localtime(¤tTime);// 格式化本地时间为字符串char timeString[MAX_BOOK1];strftime(timeString, sizeof(timeString), "%Y%m%d %H:%M:%S", localTime);BookList* q = head1;while (q != NULL){if ((q->book.ID == ID)){q->book.num++; //还书库存加1find_flag = true;break;}q = q->next;}if (find_flag == false){printf("登记失败,没有该编号的图书!!!\n");return false;}BorrowList* p = head->next;if (p == NULL) return false;while (p != NULL){if ((strcmp(p->data_book.stuname, stuname) == 0) && (p->data_book.ID == ID) && (strcmp(p->data_book.name, name) == 0)){find_flag = true;strcpy_s(p->data_book.time, MAX_BOOK1, timeString); //当前时间printf("\n----------------------------------------------------------------------------\n");printf("\n============================图书借阅表=====================================\n");printf("%-10s %-15s %-15s %-15s %-15s\n", "借阅人", "图书编号", "图书名字", "借阅状态", "借阅日期");printf("%-10s %-15d %-15s %-15s %s\n", p->data_book.stuname, p->data_book.ID, p->data_book.name, p->data_book.state, p->data_book.time);break;}p = p->next;}if (find_flag == false){printf("未找到该编号的图书\n");return false;}else{printf("已找到!!!\n");strcpy_s(p->data_book.state, MAX_BOOK1, "已归还");}return true;}//遍历链表
void print_borrow(BorrowList* head)
{BorrowList* p = head->next;if (p == NULL) return;printf("\n----------------------------------------------------------------------------\n");printf("\n============================图书借阅表=====================================\n");printf("%-10s %-15s %-15s %-15s %-15s\n", "借阅人", "图书编号", "图书名字", "借阅状态", "借阅日期");while (p != NULL){printf("%-10s %-15d %-15s %-15s %s\n", p->data_book.stuname, p->data_book.ID, p->data_book.name, p->data_book.state, p->data_book.time);p = p->next;}
}//回收链表
void free_borrow(BorrowList* head)
{BorrowList* p = head->next, * q = head;if (p == NULL) return;while (p != NULL){q = p->next;free(p);p = q;}
}//创建图书链表
BookList* init_book()
{BookList* head = (BookList*)malloc(sizeof(BookList));if (head == NULL)return NULL;head->next = NULL;return head;}//尾插法--添加书籍
bool tail_book(BookList* head)
{bool find_flag = false;BookList* new_node = (BookList*)malloc(sizeof(BookList));if (new_node == NULL)return false;printf("请依次输入图书编号,图书名称,图书作者,图书价格/单本元,图书数量,图书位置:");scanf_s("%d", &new_node->book.ID);scanf_s("%s", new_node->book.name, MAX_BOOK1);scanf_s("%s", new_node->book.anthor, MAX_BOOK1);scanf_s("%d", &new_node->book.price);scanf_s("%d", &new_node->book.num);scanf_s("%s", new_node->book.location, MAX_BOOK1);new_node->next = NULL;BookList* p = head;while (p != NULL){if ((p->book.ID == new_node->book.ID)){find_flag = false;break;}if (p->next == NULL){find_flag = true;break;}p = p->next;}if (find_flag == false){printf("库中已有该编号的图书,添加失败!!!\n");find_book(head, new_node->book.ID);//fun1();return false;}else{printf("已成功添加!!!\n");p->next = new_node;sort_book(head);print_book(head);}//p->book.num++;return true;
}//按编号进行排序
void sort_book(BookList* head)
{int size = length_book(head);BookList* p = head->next;for (int i = 0; i < size - 1; i++){p = head->next;for (int j = 0; j < size - i - 1; j++, p = p->next){if (p->book.ID > p->next->book.ID){struct book_str temp = p->book;p->book = p->next->book;p->next->book = temp;}}}
}//删除图书
bool del_book(BookList* head, unsigned int ID)
{bool find_flag = false;BookList* p = head->next, * q = head;if (p == NULL || head == NULL)return false;while (p != NULL){if (p->book.ID == ID){find_flag = true;break;}p = p->next;q = q->next;}if (find_flag == false){printf("未找到该编号的图书\n");fun1();return false;}else{printf("已找到该图书\n");find_book(head, ID);printf("----------------------------------------------------------------------------------\n");printf("= 请选择要删除图书的信息 =\n");printf("----------------------------------------------------------------------------------\n");printf("= 0.无需删除 =\n");printf("= 1.删除该书所有 2.仅删除一本 =\n");printf("----------------------------------------------------------------------------------\n");while (1){int n = 0;printf("select>");scanf_s("%d", &n);switch (n){case 0:printf("退出删除操作\n");fun1();return false;case 1:{q->next = p->next;free(p);printf("已删除!!!\n");printf("删除后:\n");print_book(head);break;}case 2:{p->book.num--;//q->next = p->next;//free(p);printf("已删除!!!\n");printf("删除后:\n");print_book(head);break;}default:printf("您输入的数据无效,请重新输入!!!\n");break;}}}return true;
}//删除一本图书
bool del_book1(BookList* head, unsigned int ID)
{bool find_flag = false;BookList* p = head->next, * q = head;if (p == NULL || head == NULL)return false;//int num = length_book(head);while (p != NULL){if (p->book.ID == ID){find_flag = true;break;}p = p->next;q = q->next;}if (find_flag == false){printf("未找到该编号的图书\n");return false;}p->book.num--;//q->next = p->next;//free(p);printf("已删除!!!");return true;
}//查询图书---按编号查找
bool find_book(BookList* head, unsigned int ID)
{bool find_flag = false;BookList* p = head->next;if (p == NULL) return false;while (p != NULL){if (p->book.ID == ID){find_flag = true;//printf("%d", p->book.ID);printf("\n---------------------------------------------------------------------------------------------\n");printf("\n=======================================图书信息表============================================\n");printf("%-10s %-15s %-15s %-15s %-15s %-15s\n", "图书编号", "图书名字", "图书作者", "图书价格/单本元", "图书数量","图书位置");printf("%-10d %-15s %-15s %-15d %-15d %-15s\n", p->book.ID, p->book.name, p->book.anthor, p->book.price, p->book.num,p->book.location);break;}p = p->next;}if (find_flag == false){printf("未找到该编号的图书\n");return false;}else{printf("已找到!!!\n");}return true;
}//查询图书---按书名查找
bool find_bookname(BookList* head, char name[MAX_BOOK1])
{bool find_flag = false;BookList* p = head->next;if (p == NULL) return false;while (p != NULL){if (strcmp(p->book.name, name) == 0){find_flag = true;//printf("%d", p->book.ID);printf("\n---------------------------------------------------------------------------------------------\n");printf("\n=======================================图书信息表============================================\n");printf("%-10s %-15s %-15s %-15s %-15s %-15s\n", "图书编号", "图书名字", "图书作者", "图书价格/单本元", "图书数量", "图书位置");printf("%-10d %-15s %-15s %-15d %-15d %-15s\n", p->book.ID, p->book.name, p->book.anthor, p->book.price, p->book.num, p->book.location);break;}p = p->next;}if (find_flag == false){printf("未找到该编号的图书\n");return false;}else{printf("已找到!!!\n");}return true;
}//查询图书---按作者查
bool find_anthorname(BookList* head, char anthor[MAX_BOOK1])
{bool find_flag = false;BookList* p = head->next;if (p == NULL) return false;while (p != NULL){if (strcmp(p->book.anthor, anthor) == 0){find_flag = true;//printf("%d", p->book.ID);printf("\n---------------------------------------------------------------------------------------------\n");printf("\n=======================================图书信息表============================================\n");printf("%-10s %-15s %-15s %-15s %-15s %-15s\n", "图书编号", "图书名字", "图书作者", "图书价格/单本元", "图书数量", "图书位置");printf("%-10d %-15s %-15s %-15d %-15d %-15s\n", p->book.ID, p->book.name, p->book.anthor, p->book.price, p->book.num, p->book.location);break;}p = p->next;}if (find_flag == false){printf("未找到该编号的图书\n");return false;}else{printf("已找到!!!\n");}return true;
}//修改数据
bool rework_book(BookList* head)
{bool find_flag = false;BookList* p = head->next;if (p == NULL) return false;unsigned int sum = 0;if (sum > MAX_BOOK1) //每个编号图书的数量不能大于MAX_BOOK1(100){printf("修改的值超出限制!!!\n");return false;}unsigned int ID = 0;printf("请输入要修改的图书ID:");scanf_s("%d", &ID);while (p != NULL){if (p->book.ID == ID){printf("已找到:\n");printf("\n---------------------------------------------------------------------------------------------\n");printf("\n=======================================图书信息表============================================\n");printf("%-10s %-15s %-15s %-15s %-15s %-15s\n", "图书编号", "图书名字", "图书作者", "图书价格/单本元", "图书数量", "图书位置");printf("%-10d %-15s %-15s %-15d %-15d %-15s\n", p->book.ID, p->book.name, p->book.anthor, p->book.price, p->book.num, p->book.location);printf("----------------------------------------------------------------------------------\n");printf("= 请选择要修改的信息 =\n");printf("----------------------------------------------------------------------------------\n");printf("= 0.退出修改 =\n");printf("= 1.修改图书名称 2.修改作者 =\n");printf("= 3.修改图书数量 4.修改图书价格 =\n");printf("= 5.修改图书位置 =\n");printf("----------------------------------------------------------------------------------\n");find_flag = true;//printf("%d", p->book.ID);while (1){int n = 0;printf("select>");scanf_s("%d", &n);switch (n){case 0:fun1();return false;case 1:{char name[MAX_BOOK1];printf("请输入要修改的图书名称:");scanf_s("%s", name, MAX_BOOK1);strcpy(p->book.name, name);printf("\n---------------------------------------------------------------------------------------------\n");printf("\n=======================================图书信息表============================================\n");printf("%-10s %-15s %-15s %-15s %-15s %-15s\n", "图书编号", "图书名字", "图书作者", "图书价格/单本元", "图书数量", "图书位置");printf("%-10d %-15s %-15s %-15d %-15d %-15s\n", p->book.ID, p->book.name, p->book.anthor, p->book.price, p->book.num, p->book.location);}break;case 2:{char anthor[MAX_BOOK1];printf("请输入要修改的作者:");scanf_s("%s",anthor, MAX_BOOK1);strcpy(p->book.anthor,anthor);printf("\n---------------------------------------------------------------------------------------------\n");printf("\n=======================================图书信息表============================================\n");printf("%-10s %-15s %-15s %-15s %-15s %-15s\n", "图书编号", "图书名字", "图书作者", "图书价格/单本元", "图书数量", "图书位置");printf("%-10d %-15s %-15s %-15d %-15d %-15s\n", p->book.ID, p->book.name, p->book.anthor, p->book.price, p->book.num, p->book.location);break;}case 3:{unsigned int num;printf("请输入要修改的图书数量:");scanf_s("%d", &num);p->book.num = num;printf("\n---------------------------------------------------------------------------------------------\n");printf("\n=======================================图书信息表============================================\n");printf("%-10s %-15s %-15s %-15s %-15s %-15s\n", "图书编号", "图书名字", "图书作者", "图书价格/单本元", "图书数量", "图书位置");printf("%-10d %-15s %-15s %-15d %-15d %-15s\n", p->book.ID, p->book.name, p->book.anthor, p->book.price, p->book.num, p->book.location);break;}case 4:{unsigned int price;printf("请输入要修改的图书价格:");scanf_s("%d", &price);p->book.price = price;printf("\n---------------------------------------------------------------------------------------------\n");printf("\n=======================================图书信息表============================================\n");printf("%-10s %-15s %-15s %-15s %-15s %-15s\n", "图书编号", "图书名字", "图书作者", "图书价格/单本元", "图书数量", "图书位置");printf("%-10d %-15s %-15s %-15d %-15d %-15s\n", p->book.ID, p->book.name, p->book.anthor, p->book.price, p->book.num, p->book.location);break;}case 5:{char location[MAX_BOOK1];printf("请输入要修改的图书位置:");scanf_s("%s",location,MAX_BOOK1);strcpy(p->book.location, location);printf("\n---------------------------------------------------------------------------------------------\n");printf("\n=======================================图书信息表============================================\n");printf("%-10s %-15s %-15s %-15s %-15s %-15s\n", "图书编号", "图书名字", "图书作者", "图书价格/单本元", "图书数量", "图书位置");printf("%-10d %-15s %-15s %-15d %-15d %-15s\n", p->book.ID, p->book.name, p->book.anthor, p->book.price, p->book.num, p->book.location);}default:printf("您输入的数据无效,请重新输入!!!\n");break;}}break;}p = p->next;}if (find_flag == false){printf("未找到该编号的图书\n");return false;}else{p->book.num = sum;printf("修改后:\n");printf("\n---------------------------------------------------------------------------------------------\n");printf("\n=======================================图书信息表============================================\n");printf("%-10s %-15s %-15s %-15s %-15s %-15s\n", "图书编号", "图书名字", "图书作者", "图书价格/单本元", "图书数量", "图书位置");printf("%-10d %-15s %-15s %-15d %-15d %-15s\n", p->book.ID, p->book.name, p->book.anthor, p->book.price, p->book.num, p->book.location);}return true;}//向链表中插入原始数据
bool tail_book1(BookList* head, unsigned int ID, char name[MAX_BOOK1],char anthor[MAX_BOOK1],unsigned int price, unsigned int num,char location[MAX_BOOK1])
{BookList* new_node = (BookList*)malloc(sizeof(BookList));if (new_node == NULL)return false;//printf("请依次输入图书编号,图书名称, 图书作者, 图书价格/单本, 图书数量:");new_node->book.ID = ID;strcpy_s(new_node->book.name, MAX_BOOK1, name);strcpy_s(new_node->book.anthor, MAX_BOOK1, anthor);new_node->book.price = price;new_node->book.num = num;strcpy_s(new_node->book.location, MAX_BOOK1, location);new_node->next = NULL;BookList* p = head;if (p == NULL)return false;while (p != NULL){if (p->next == NULL)break;p = p->next;}p->next = new_node;return true;
}//链表长度
int length_book(BookList* head)
{int count = 0;BookList* p = head->next;if (p == NULL) return false;while (p != NULL){count++;p = p->next;}return count;
}//遍历
void print_book(BookList* head)
{BookList* p = head->next;if (p == NULL)return;printf("\n---------------------------------------------------------------------------------------------\n");printf("\n=======================================图书信息表============================================\n");printf("%-10s %-15s %-15s %-15s %-15s %-15s\n", "图书编号", "图书名字", "图书作者", "图书价格/单本元", "图书数量", "图书位置");while (p != NULL){printf("%-10d %-15s %-15s %-15d %-15d %-15s\n", p->book.ID, p->book.name, p->book.anthor, p->book.price, p->book.num, p->book.location);p = p->next;}
}//回收链表
void free_book(BookList* head)
{BookList* p = head->next, * q = p;if (p == NULL)return;while (p != NULL){q = p->next;free(p);p = q;}}//int count;
//写文件--将文件中的信息写入到链表中 图书信息
void file_readbook(BookList* head)
{//打开节点FILE* fd = NULL;fd = fopen("./book1.txt", "r+");if (!fd){printf("文件打开失败\n");return;}BookList* p = head;//if (p != NULL) return;while (1){BookList* new_node = (BookList*)malloc(sizeof(BookList));//if (new_node == NULL) return;fscanf_s(fd, "%d %s %s %d %d %s", &new_node->book.ID, new_node->book.name, MAX_BOOK1,new_node->book.anthor,MAX_BOOK1,&new_node->book.price, &new_node->book.num,new_node->book.location,MAX_BOOK1);new_node->next = NULL;if (feof(fd)){break;}else if (p->next == NULL){p->next = new_node;}p = p->next;}fclose(fd);}//写文件--将链表中的信息写到文件中 图书信息
void file_savebook(BookList* head)
{FILE* fp;fp = fopen("./book1.txt", "w+");if (fp == NULL){printf("文件打开失败\n");return;}//遍历链表BookList* p = head->next;while (p != NULL){fprintf(fp, "%d\t%s\t%s\t%d\t%d\t%s\n", p->book.ID, p->book.name,p->book.anthor,p->book.price, p->book.num,p->book.location);p = p->next;}fclose(fp);}//写文件--将文件中的信息写入到链表中 图书信息
void file_readbook1(BookList* head)
{//打开节点FILE* fd = NULL;fd = fopen("./图书库存.txt", "r+");if (!fd){printf("文件打开失败\n");return;}BookList* p = head;//if (p != NULL) return;while (1){BookList* new_node = (BookList*)malloc(sizeof(BookList));//if (new_node == NULL) return;fscanf_s(fd, "%d %s %s %d %d %s", &new_node->book.ID, new_node->book.name, MAX_BOOK1, new_node->book.anthor, MAX_BOOK1, &new_node->book.price, &new_node->book.num, new_node->book.location, MAX_BOOK1);new_node->next = NULL;if (feof(fd)){break;}else if (p->next == NULL){p->next = new_node;}p = p->next;}fclose(fd);
}//写文件--将链表中的信息写到文件中 图书信息
void file_savebook1(BookList* head)
{FILE* fp;fp = fopen("./图书库存.txt", "w+");if (fp == NULL){printf("文件打开失败\n");return;}//遍历链表BookList* p = head->next;while (p != NULL){fprintf(fp, "%d\t%s\t%s\t%d\t%d\t%s\n", p->book.ID, p->book.name, p->book.anthor, p->book.price, p->book.num, p->book.location);p = p->next;}fclose(fp);}//学生借阅信息表--从链表写入到文件
void file_savestubook(BorrowList* head)
{FILE* fp;fp = fopen("./stubook1.txt", "w+");if (fp == NULL){printf("文件打开失败\n");return;}//遍历链表BorrowList* p = head->next;//遍历链表while (p != NULL){fwrite(&(p->data_book), sizeof(BorrowList), 1, fp);p = p->next;}fclose(fp);}//学生借阅信息表--从文件写入到链表
void file_readstuook(BorrowList* head)
{//打开节点FILE* fd = NULL;fd = fopen("./stubook1.txt", "r+");if (!fd){printf("文件打开失败\n");return;}BorrowList temp;while (fread(&(temp.data_book), sizeof(BorrowList), 1, fd) == 1) //feof函数feof()在到达给出的文件流的文件尾时返回一个非零值. {tail_borrow(head, temp.data_book.stuname, temp.data_book.ID, temp.data_book.name, temp.data_book.state, temp.data_book.time);}fclose(fd);
}//用户登录信息表--从链表写入到文件
void file_saveuser(UserLog* head)
{FILE* fp;fp = fopen("./用户信息.txt", "w+");if (fp == NULL){printf("文件打开失败\n");return;}//遍历链表UserLog* p = head->next;//遍历链表while (p != NULL){fwrite(&(p->st), sizeof(UserLog), 1, fp);p = p->next;}fclose(fp);}//用户登录信息表--从文件写入到链表
void file_readuser(UserLog* head)
{//打开节点FILE* fd = NULL;fd = fopen("./用户信息.txt", "r+");if (!fd){printf("文件打开失败\n");return;}UserLog temp;while (fread(&(temp.st), sizeof(UserLog), 1, fd) == 1) //feof函数feof()在到达给出的文件流的文件尾时返回一个非零值. {tail_user(head, temp.st.name, temp.st.password, temp.st.phone);}fclose(fd);
}//图书查找操作
bool book_find(BookList* head)
{//查找操作printf("----------------------------------------------------------------------------------\n");printf("= 请选择要查找图书的信息 =\n");printf("----------------------------------------------------------------------------------\n");printf("= 0.返回操作 =\n");printf("= 1.按图书编号查找 2.按图书名称查找 =\n");printf("= 3.按图书作者查找 =\n");printf("----------------------------------------------------------------------------------\n");{while (1){int n1 = 0;printf("select>");scanf_s("%d", &n1);switch (n1){case 0:printf("退出查找操作\n");fun1();return false;case 1:{int ID1 = 0;printf("请输入要查询图书的编号:");scanf_s("%d", &ID1);find_book(head, ID1);}break;case 2:{char name[MAX_BOOK1] = { 0 };printf("请输入要查询图书的名称:");scanf_s("%s", name, MAX_BOOK1);find_bookname(head, name);}break;case 3:{char anthor[MAX_BOOK1] = { 0 };printf("请输入要查询作者的名称:");scanf_s("%s", anthor, MAX_BOOK1);find_anthorname(head, anthor);}break;default:printf("您输入的数据无效,请重新输入!!!\n");break;}}}return true;
}//管理员登录
void admin_log()
{BorrowList* head1 = init_borrow(); //图书借阅链表/*tail_borrow(head1, "张三", 1001, "红楼梦", "已借阅", "20240721 12:30:05");tail_borrow(head1, "李四", 1002, "三国演义", "已借阅", "20240721 12:30:05");file_savestubook(head1);*/file_readstuook(head1);//链表中图书借阅原始数据BookList* head = init_book(); //图书链表/*tail_book1(head,1001,"红楼梦","曹雪芹",20,6,"1号架A面");tail_book1(head, 1002, "三国演义", "罗贯中", 22, 4,"1号架B面");tail_book1(head, 1003, "西游记", "吴承恩", 55, 9,"1号架C面");tail_book1(head, 1004, "水浒传", "施耐庵", 34, 2,"1号架D面");file_savebook(head);*/file_readbook(head); //原始数据链表//print_book(head);BookList* head3 = init_book(); //图书库存链表file_readbook1(head3);//登录信息表UserLog* head2 = init_user();file_readuser(head2);while (1){int n = 0;printf("select>");scanf_s("%d", &n);switch (n){case 0:printf("退出操作\n");fun();return;case 1://file_read(head);print_book(head);break;case 2:{//增加操作tail_book(head);file_savebook(head);file_savebook1(head);break;}case 3:{//删除操作int ID = 0;printf("请输入要删除图书的编号:");scanf_s("%d", &ID);del_book(head, ID);file_savebook(head);file_savebook1(head);break;}case 4:{//查找操作book_find(head);break;}case 5:{//修改操作rework_book(head);file_savebook(head);file_savebook1(head);break;}case 6:{//查看借阅信息//char stuname[MAX_BOOK1];print_borrow(head1);/*printf("请输入要查询的借阅人信息:");scanf_s("%s", stuname,MAX_BOOK1);find_borrow(head1, stuname);*/find_borrowfun(head1);break;}case 7:{//打印用户信息print_user(head2);char name[MAX_BOOK1];printf("请输入要查找的用户名称:");scanf_s("%s", name, MAX_BOOK1);find_user(head2, name);break;}case 8:{//打印图书库存print_book(head3);break;}default:printf("您输入的数据无效,请重新输入!!!\n");break;}}free_borrow(head1);free_book(head);
}//登录操作
bool user_log(UserLog *head)
{bool find_flag = false;//printf("%s %ld\n", name, password);char str1[] = { "admin" };char name[MAX_BOOK1] = { 0 };char password[20] = {0};printf("请输入用户名:");scanf_s("%s", name, MAX_BOOK1);printf("请输入密码:");scanf_s(" %s", password,20);if (strcmp(password, "888888") == 0 && strcmp(name, str1) == 0){find_flag = true;fun1();admin_log();}else{UserLog* p = head->next;while (p != NULL){if (strcmp(p->st.name, name) == 0 && strcmp(p->st.password, password) == 0){find_flag = true;fun2();student_log(p->st.name);}p = p->next;}}if(find_flag == false){printf("用户名及密码错误!!!请重新输入\n");}return true;}//用户登录
void student_log(char stuname[MAX_BOOK1])
{//图书借阅表BorrowList* head1 = init_borrow();file_readstuook(head1);//图书信息表BookList* head = init_book();file_readbook(head); //原始数据链表//fun2();//登录信息表UserLog* head2 = init_user();file_readuser(head2);BookList* head3 = init_book(); //图书库存链表file_readbook1(head3);while (1){int n = 0;printf("select>");scanf_s("%d", &n);switch (n){case 0:printf("退出用户登录\n");fun();return;case 1:{find_borrow(head1,stuname);//借阅图书unsigned int ID = 0;char name[MAX_BOOK1];//print_borrow(head1);printf("请依次输入图书ID,图书名称:");scanf_s("%d", &ID);scanf_s("%s", name, MAX_BOOK1);tail_borrow1(head1, head3,stuname,ID,name); //插入到个人显示find_borrow(head1, stuname);file_savestubook(head1); //借阅信息file_savebook1(head3); //图书库存信息printf("图书库存:\n");print_book(head);break;}case 2:{//归还图书//该用户的借阅信息find_borrow(head1, stuname);unsigned int ID = 0;char stuname[MAX_BOOK1];char name[MAX_BOOK1];printf("请依次输入借阅人,图书ID,图书名称:");scanf_s("%s", stuname, MAX_BOOK1);scanf_s("%d", &ID);scanf_s("%s", name, MAX_BOOK1);del_borrow1(head1, head3, stuname, ID, name);find_borrow(head1, stuname);file_savestubook(head1); //写入文件file_savebook1(head3); //图书库存信息printf("图书库存:\n");print_book(head3); //打印图书库存 还一本书就加1break;}case 3:{//读者查看图书库存print_book(head);break;}case 4:{//修改个人信息rework_user(head2, stuname);file_saveuser(head2);break;}default:printf("您输入的数据无效,请重新输入!!!\n");break;}}free_borrow(head1);free_book(head);free_user(head2);}int main()
{//--------------图书管理系统----------------fun();UserLog* head = init_user();/*tail_user(head, "张三", "1", 123);tail_user(head, "郭胜雨", "11", 111);tail_user(head, "李四", "12", 222);tail_user(head, "王五", "13", 333);file_saveuser(head);*/file_readuser(head);//print_user(head);if (head == NULL) return 0;while (1){int n = 0;printf("select>");scanf_s("%d", &n);switch (n){case 0:printf("退出程序\n");return 0;case 1:{user_log(head); //登录账号break;}case 2:{ register_user(head); //注册用户file_saveuser(head);break;}default:printf("您输入的数据无效,请重新输入!!!\n");break;}}free_user(head);
}