#include#include#include#include#define MAX_BOOKS 10000#define MAX_USERS 10000using namespace std;struct Book {int book_id;char title[100];char author[100];int available;int total;};struct bb {int bookid; //所借阅书籍的编号 bb* next; // 指向下一本借阅的书的指针};struct User {int userid;char name[15];long long phone;int bbn; // 借阅书数bb* booklist;// 借阅的书:指向链表头结点的指针User* next;};User* userslist = NULL;int sum = 0;struct Book books[MAX_BOOKS];int num_books = 0;int num_users = 0;int choose();int mainMenu();int bookMenu();void addbook();void borrowbook();void returnbook();void searchbook();void deletebook();int book();void readBook();void writeBook();int user();void writeUser();void readUser();int main() {while (1) {int choice = mainMenu();switch (choice) {case 1:book();break;case 2:user();break;case 3:exit(0);break;default:printf("无效数字!\n");//system("pause");break;}}}int choose(){int choice = -1;printf("输入选择:");scanf("%d", &choice);return choice;}//主系统 int mainMenu(){printf("\n****************************\n");printf("*图书馆系统*\n");printf("*1.图书管理系统*\n");printf("*2.会员管理系统*\n");printf("*3. 退出系统 *\n");printf("****************************\n");return choose();}int bookMenu(){printf("\n**********************************\n");printf("* 图书管理系统 *\n");printf("*1. 添加图书 *\n");printf("*2. 借阅图书 *\n");printf("*3. 归还图书 *\n");printf("*4. 查询图书 *\n");printf("*5. 删除图书 *\n");printf("*6. 保存修改 *\n");printf("*7. 返回主系统 *\n");printf("*8. 切换会员管理系统 *\n");printf("*9. 退出系统 *\n");printf("**********************************\n");return choose();}//添加图书void addBook() {// 检查是否已存在该书int book_id, count;char title[100];char author[100];printf("图书id:");scanf("%d", &book_id);printf("书名:");scanf("%s", title);printf("作者名:");scanf("%s", author);printf("添加数量:");scanf("%d", &count);if (count + sum > MAX_BOOKS){printf("图书馆存储空间已满,无法添加!");return;}for (int i = 0; i < num_books; i++) {if (books[i].book_id == book_id) {books[i].total += count;books[i].available += count;sum = sum + count;printf("图书添加成功!");return;}}// 不存在则添加新书books[num_books].book_id = book_id;strcpy(books[num_books].title, title);strcpy(books[num_books].author, author);books[num_books].total = count;books[num_books].available = count;sum = sum + count;num_books++;printf("图书添加成功!");return;}void borrowBook(){long long phone;int bookid;cout <> phone;User* c = userslist;while (c != NULL && c->phone != phone){c = c->next; // 寻找要借书的会员}if (c == NULL){cout << "未找到该手机号对应的会员信息!" <bbn >= 3) {cout << "借阅书籍已达上限,无法再借阅更多书籍。" << endl;return;}cout <> bookid;for (int i = 0; i 0){books[i].available--;printf("借阅成功\n");sum--;// 创建新的书籍节点bb* newbook = new bb();//newBooknewbook->bookid = bookid;newbook->next = NULL;if (c->bbn == 0){c->booklist = newbook; // 如果之前未借过书,直接把新书籍节点设为书单的头}else{bb* cb = c->booklist;//currentBookwhile (cb->next != NULL){cb = cb->next; // 找到书单的末尾节点}cb->next = newbook; // 将新书籍节点插入到末尾节点之后cb = cb->next;cb->next = NULL;}c->bbn++;}else{printf("该书已全部借出\n");}return;}}printf("未找到该书\n");return;}//归还图书 void returnBook(){long long phone;int book_id;char title[100];char author[100];printf("请输入归还者的手机号:");scanf("%lld", &phone);User* c = userslist;while (c != NULL && c->phone != phone){c = c->next; // 寻找要借书的会员}if (c == NULL){cout << "未找到该手机号对应的会员信息!" < MAX_BOOKS){printf("图书馆存储空间已满,无法添加!");return;}for (int i = 0; i < num_books; i++){if (books[i].book_id == book_id){if (books[i].available bookid = book_id;newbook->next = NULL;if (c->bbn == 0){c->booklist = newbook; // 如果之前未借过书,直接把新书籍节点设为书单的头}else{bb* cb = c->booklist;//currentBookwhile (cb->next != NULL){cb = cb->next; // 找到书单的末尾节点}cb->next = newbook; // 将新书籍节点插入到末尾节点之后cb = cb->next;cb->next = NULL;}c->bbn++;return;}else{printf("此书非待归还状态!\n");return;}}}printf("未找到该书!");}//查找图书 void searchBook() {char keyword[100];int found = 0;printf("输入查找信息(书名/作者): ");scanf("%s", &keyword);printf("查找结果:\n");for (int i = 0; i < num_books; i++) {if (strcmp(books[i].title, keyword) == 0 || strcmp(books[i].author, keyword) == 0){printf("书名: %s, 作者: %s,可借阅数目:%d,图书总数:%d", books[i].title, books[i].author, books[i].available, books[i].total);found = 1;}}if (!found) {printf("无符合要求图书!\n");}}//删除图书 void deleteBook() {char title[100];char author[100];int found = 0;printf("输入要删除的书名: ");scanf("%s", title);printf("输入要删除图书的作者名: ");scanf("%s", author);for (int i = 0; i < num_books; i++) {if (strcmp(books[i].title, title) == 0 && strcmp(books[i].author, author) == 0) {for (int j = i; j < num_books - 1; j++) {books[j] = books[j + 1];}found = 1;break;}}if (found) {num_books--;printf("图书删除成功!\n");}else {printf("未找到要删除的图书!\n");}}void writeBook(){FILE* fp = NULL;fp = fopen("Book.txt", "w+");//覆盖写入文件 int i;for (i = 0; i next = NULL;printf("请输入要添加的会员姓名:");scanf("%s", name);printf("请输入要添加会员的手机号:");cin >> phone;newUser->phone = phone;strcpy(newUser->name, name);newUser->bbn = 0;newUser->booklist = NULL;newUser->userid = num_users;if (userslist == NULL){userslist = newUser; // 如果会员链表为空,直接将新会员节点设为头节点}else{User* current = userslist;while (current->next != NULL){current = current->next; // 找到会员链表的末尾节点}current->next = newUser; // 将新会员节点插入到末尾节点之后}printf("会员添加成功!\n");num_users++;}// 查询会员编号、姓名、手机号、已借阅书籍量、具体借阅书籍 void searchUser() {long long phone;cout <> phone;User* current = userslist;while (current != NULL){if (current->phone == phone){cout << "会员编号:" <userid << endl;cout << "会员姓名:" <name << endl;cout << "会员手机号:" <phone << endl;cout << "已借阅书籍数量:" <bbn <bbn == 0){cout << "无借阅记录!" <booklist;cout << "借阅的书籍:";while (bookPtr != NULL){cout <bookid <next;}cout <next;}cout << "未找到该手机号对应的会员信息!\n" <> phone;User* current = userslist;User* p = NULL;// 寻找要删除的会员节点,并记录其前一个节点while (current != NULL && current->phone != phone){p = current;current = current->next;}// 如果未找到要删除的会员if (current == NULL){cout << "未找到要删除的会员!" <next;}else{p->next = current->next;}// 释放要删除的节点的内存delete current;cout << "会员删除成功!" <userid, member->name, member->phone, member->bbn);bb* p = new bb;p = member->booklist;while (p != NULL){fprintf(fp, "%s ", p->bookid);p = p->next;}fprintf(fp, "\n");member = member->next;}fclose(fp);printf("保存信息成功!\n");return;}void readUser() {FILE* fp = fopen("User.txt", "r");User* member = NULL;User* p = NULL;bb* book = NULL;char line[1000];while (fgets(line, sizeof(line), fp) != NULL){member = new User();int bytesRead = sscanf(line, "%d %s %lld %d", &member->userid, member->name, &member->phone, &member->bbn);//if (bytesRead != 4) {//cout << "文件格式错误" << endl;//delete member;//continue;//}for (int i = 0; i bbn; ++i){int bookid;book = new bb();book->bookid = NULL;book->next = NULL;if (member->booklist == NULL){member->booklist = book;}else{bb* temp = member->booklist;while (temp->next != NULL){temp = temp->next;}temp->next = book;}}if (p != NULL){p->next = member;num_users++;}else{userslist = member;}p = member;}fclose(fp);cout << "读取信息成功!" << endl;num_users++;}int userMenu(){printf("\n**********************************\n");printf("* 会员管理系统 *\n");printf("*1. 增加会员 *\n");printf("*2. 查询会员 *\n");printf("*3. 删除会员 *\n");printf("*4. 保存修改 *\n");printf("*5. 返回主系统 *\n");printf("*6. 切换图书管理系统 *\n");printf("*7. 退出系统 *\n");printf("**********************************\n");return choose();}int user(){readUser();while (1) {int choice = userMenu();switch (choice) {case 1:addUser();break;case 2:searchUser();break;case 3:deleteUser();break;case 4:readBook();writeUser();writeBook();break;case 5:mainMenu();break;case 6:book();break;case 7:printf("\n\n感谢您的使用!\n\n");exit(0);break;default:printf("无效数字!\n");//system("pause");break;}}return choose();}