当前位置:   article > 正文

C++图书管理系统

C++图书管理系统

目录

实现功能                      

用户管理

图书管理

借阅与归还

未归还图书

部分效果图

结构体

Book 结构体

User 结构体

源代码

编译时在连接器命令行加入

完整代码 

    

实现功能                      

  1. 用户管理

    • 添加用户:输入用户ID、用户名和密码,将新用户添加到系统中。
    • 删除用户:根据用户ID删除指定用户。
    • 修改用户:根据用户ID修改用户的用户名和密码。
    • 显示所有用户:列出系统中的所有用户信息。
    • 查找用户:根据用户ID查找并显示用户信息。
  2. 图书管理

    • 添加图书:输入图书ID、书名、作者和出版社,将新图书添加到系统中。
    • 删除图书:根据图书ID删除指定图书。
    • 显示所有图书:列出系统中的所有图书信息。
    • 查找图书:根据图书ID查找并显示图书信息。
  3. 借阅与归还

    • 借阅图书:根据用户ID和图书ID记录借阅操作,并设置图书的到期日期。
    • 归还图书:根据用户ID和图书ID记录归还操作,更新图书的状态。
  4. 未归还图书

    • 列出未归还图书:显示所有未归还的图书信息,包括ID、书名、作者、出版社和到期日期。

部分效果图

结构体

Book 结构体

id: 图书ID

title: 书名

author: 作者

publisher: 出版社

isBorrowed: 是否被借阅

dueDate: 到期日期(tm 结构体)

User 结构体

id: 用户ID

username: 用户名

password: 密码

borrowedBooks: 借阅图书记录(书ID和归还时间的配对)

源代码

编译时在连接器命令行加入

-std=c++11

完整代码 

  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <ctime>
  7. #include <iomanip>
  8. using namespace std;
  9. // 图书信息
  10. struct Book {
  11. string id;
  12. string title;
  13. string author;
  14. string publisher;
  15. bool isBorrowed;
  16. tm dueDate;
  17. };
  18. // 用户信息
  19. struct User {
  20. string id;
  21. string username;
  22. string password;
  23. vector<pair<string, tm>> borrowedBooks; // 书ID和归还时间
  24. };
  25. // 全局变量
  26. vector<Book> books;
  27. vector<User> users;
  28. // 打印日期
  29. void printDate(const tm& date) {
  30. cout << (date.tm_year + 1900) << '-' << (date.tm_mon + 1) << '-' << date.tm_mday << ' '
  31. << date.tm_hour << ':' << date.tm_min << ':' << date.tm_sec;
  32. }
  33. // 加载用户数据
  34. void loadUsers() {
  35. ifstream infile("users.txt");
  36. if (!infile) return;
  37. User user;
  38. string line;
  39. while (getline(infile, line)) {
  40. istringstream ss(line);
  41. ss >> user.id >> user.username >> user.password;
  42. users.push_back(user);
  43. }
  44. }
  45. // 保存用户数据
  46. void saveUsers() {
  47. ofstream outfile("users.txt");
  48. for (const auto& user : users) {
  49. outfile << user.id << ' ' << user.username << ' ' << user.password << '\n';
  50. }
  51. }
  52. // 加载图书数据
  53. void loadBooks() {
  54. ifstream infile("books.txt");
  55. if (!infile) return;
  56. Book book;
  57. string line;
  58. while (getline(infile, line)) {
  59. istringstream ss(line);
  60. ss >> book.id >> book.title >> book.author >> book.publisher >> book.isBorrowed;
  61. ss >> book.dueDate.tm_year >> book.dueDate.tm_mon >> book.dueDate.tm_mday
  62. >> book.dueDate.tm_hour >> book.dueDate.tm_min >> book.dueDate.tm_sec;
  63. book.dueDate.tm_year -= 1900;
  64. book.dueDate.tm_mon -= 1;
  65. books.push_back(book);
  66. }
  67. }
  68. // 保存图书数据
  69. void saveBooks() {
  70. ofstream outfile("books.txt");
  71. for (const auto& book : books) {
  72. outfile << book.id << ' ' << book.title << ' ' << book.author << ' ' << book.publisher << ' '
  73. << book.isBorrowed << ' '
  74. << (book.dueDate.tm_year + 1900) << ' '
  75. << (book.dueDate.tm_mon + 1) << ' '
  76. << book.dueDate.tm_mday << ' '
  77. << book.dueDate.tm_hour << ' '
  78. << book.dueDate.tm_min << ' '
  79. << book.dueDate.tm_sec << '\n';
  80. }
  81. }
  82. // 验证管理员账号密码
  83. bool authenticate() {
  84. string username, password;
  85. cout << "输入用户名: ";
  86. cin >> username;
  87. cout << "输入密码: ";
  88. cin >> password;
  89. return username == "root" && password == "123";
  90. }
  91. // 修改管理员密码
  92. void changePassword() {
  93. string newPassword;
  94. cout << "输入新密码: ";
  95. cin >> newPassword;
  96. // 在实际应用中,需要保存新密码
  97. cout << "密码已更改! (Password changed!)\n";
  98. }
  99. // 添加图书
  100. void addBook() {
  101. Book book;
  102. cout << "输入图书ID: ";
  103. cin >> book.id;
  104. cout << "输入书名: ";
  105. cin.ignore(); // 清除输入缓冲区
  106. getline(cin, book.title);
  107. cout << "输入作者: ";
  108. getline(cin, book.author);
  109. cout << "输入出版社: ";
  110. getline(cin, book.publisher);
  111. book.isBorrowed = false;
  112. books.push_back(book);
  113. saveBooks();
  114. cout << "图书已添加! (Book added!)\n";
  115. }
  116. // 删除图书
  117. void deleteBook() {
  118. string id;
  119. cout << "输入要删除的图书ID: ";
  120. cin >> id;
  121. auto it = remove_if(books.begin(), books.end(), [&id](const Book& book) { return book.id == id; });
  122. if (it != books.end()) {
  123. books.erase(it, books.end());
  124. saveBooks();
  125. cout << "图书已删除! (Book deleted!)\n";
  126. } else {
  127. cout << "图书未找到! (Book not found!)\n";
  128. }
  129. }
  130. // 显示所有图书
  131. void displayBooks() {
  132. cout << "ID\t书名\t作者\t出版社\t是否被借阅\n";
  133. cout << "ID\tTitle\tAuthor\tPublisher\tIs Borrowed\n";
  134. for (const auto& book : books) {
  135. cout << book.id << '\t' << book.title << '\t' << book.author << '\t' << book.publisher << '\t'
  136. << (book.isBorrowed ? "Yes" : "No") << '\n';
  137. }
  138. }
  139. // 查找图书
  140. void searchBook() {
  141. string id;
  142. cout << "输入图书ID: ";
  143. cin >> id;
  144. auto it = find_if(books.begin(), books.end(), [&id](const Book& book) { return book.id == id; });
  145. if (it != books.end()) {
  146. const Book& book = *it;
  147. cout << "ID: " << book.id << '\n' << "书名: " << book.title << '\n' << "作者: " << book.author << '\n'
  148. << "出版社: " << book.publisher << '\n' << "是否被借阅: " << (book.isBorrowed ? "Yes" : "No") << '\n';
  149. cout << "到期日期: ";
  150. printDate(book.dueDate);
  151. cout << '\n';
  152. } else {
  153. cout << "图书未找到! (Book not found!)\n";
  154. }
  155. }
  156. // 添加用户
  157. void addUser() {
  158. User user;
  159. cout << "输入用户ID: ";
  160. cin >> user.id;
  161. cout << "输入用户名: ";
  162. cin >> user.username;
  163. cout << "输入密码: ";
  164. cin >> user.password;
  165. users.push_back(user);
  166. saveUsers();
  167. cout << "用户已添加! (User added!)\n";
  168. }
  169. // 删除用户
  170. void deleteUser() {
  171. string id;
  172. cout << "输入要删除的用户ID: ";
  173. cin >> id;
  174. auto it = remove_if(users.begin(), users.end(), [&id](const User& user) { return user.id == id; });
  175. if (it != users.end()) {
  176. users.erase(it, users.end());
  177. saveUsers();
  178. cout << "用户已删除! (User deleted!)\n";
  179. } else {
  180. cout << "用户未找到! (User not found!)\n";
  181. }
  182. }
  183. // 修改用户
  184. void modifyUser() {
  185. string id;
  186. cout << "输入要修改的用户ID: ";
  187. cin >> id;
  188. auto it = find_if(users.begin(), users.end(), [&id](const User& user) { return user.id == id; });
  189. if (it != users.end()) {
  190. User& user = *it;
  191. cout << "输入新的用户名: ";
  192. cin >> user.username;
  193. cout << "输入新的密码: ";
  194. cin >> user.password;
  195. saveUsers();
  196. cout << "用户已修改! (User modified!)\n";
  197. } else {
  198. cout << "用户未找到! (User not found!)\n";
  199. }
  200. }
  201. // 显示所有用户
  202. void displayUsers() {
  203. cout << "ID\t用户名\t密码\n";
  204. cout << "ID\tUsername\tPassword\n";
  205. for (const auto& user : users) {
  206. cout << user.id << '\t' << user.username << '\t' << user.password << '\n';
  207. }
  208. }
  209. // 查找用户
  210. void searchUser() {
  211. string id;
  212. cout << "输入用户ID: ";
  213. cin >> id;
  214. auto it = find_if(users.begin(), users.end(), [&id](const User& user) { return user.id == id; });
  215. if (it != users.end()) {
  216. const User& user = *it;
  217. cout << "ID: " << user.id << '\n' << "用户名: " << user.username << '\n' << "密码: " << user.password << '\n';
  218. } else {
  219. cout << "用户未找到! (User not found!)\n";
  220. }
  221. }
  222. // 借阅图书
  223. void borrowBook() {
  224. string userId, bookId;
  225. cout << "输入用户ID: ";
  226. cin >> userId;
  227. cout << "输入图书ID: ";
  228. cin >> bookId;
  229. auto userIt = find_if(users.begin(), users.end(), [&userId](const User& user) { return user.id == userId; });
  230. auto bookIt = find_if(books.begin(), books.end(), [&bookId](const Book& book) { return book.id == bookId; });
  231. if (userIt != users.end() && bookIt != books.end()) {
  232. User& user = *userIt;
  233. Book& book = *bookIt;
  234. if (!book.isBorrowed) {
  235. book.isBorrowed = true;
  236. time_t now = time(0);
  237. tm* now_tm = localtime(&now);
  238. book.dueDate = *now_tm;
  239. user.borrowedBooks.push_back(make_pair(bookId, book.dueDate));
  240. saveBooks();
  241. saveUsers();
  242. cout << "图书已借出! (Book borrowed!)\n";
  243. } else {
  244. cout << "图书已被借出! (Book is already borrowed!)\n";
  245. }
  246. } else {
  247. cout << "用户或图书未找到! (User or book not found!)\n";
  248. }
  249. }
  250. // 归还图书
  251. void returnBook() {
  252. string userId, bookId;
  253. cout << "输入用户ID: ";
  254. cin >> userId;
  255. cout << "输入图书ID: ";
  256. cin >> bookId;
  257. auto userIt = find_if(users.begin(), users.end(), [&userId](const User& user) { return user.id == userId; });
  258. auto bookIt = find_if(books.begin(), books.end(), [&bookId](const Book& book) { return book.id == bookId; });
  259. if (userIt != users.end() && bookIt != books.end()) {
  260. User& user = *userIt;
  261. Book& book = *bookIt;
  262. if (book.isBorrowed) {
  263. book.isBorrowed = false;
  264. auto borrowedBookIt = find_if(user.borrowedBooks.begin(), user.borrowedBooks.end(), [&bookId](const pair<string, tm>& borrowedBook) { return borrowedBook.first == bookId; });
  265. if (borrowedBookIt != user.borrowedBooks.end()) {
  266. user.borrowedBooks.erase(borrowedBookIt);
  267. }
  268. saveBooks();
  269. saveUsers();
  270. cout << "图书已归还! (Book returned!)\n";
  271. } else {
  272. cout << "图书没有被借出! (Book was not borrowed!)\n";
  273. }
  274. } else {
  275. cout << "用户或图书未找到! (User or book not found!)\n";
  276. }
  277. }
  278. // 列出逾期图书
  279. void listOverdueBooks() {
  280. time_t now = time(0);
  281. tm* now_tm = localtime(&now);
  282. cout << "ID\t书名\t作者\t出版社\t到期日期\n";
  283. cout << "ID\tTitle\tAuthor\tPublisher\tDue Date\n";
  284. for (const auto& book : books) {
  285. if (book.isBorrowed && difftime(mktime(now_tm), mktime(const_cast<tm*>(&book.dueDate))) > 0) {
  286. cout << book.id << '\t' << book.title << '\t' << book.author << '\t' << book.publisher << '\t';
  287. printDate(book.dueDate);
  288. cout << '\n';
  289. }
  290. }
  291. }
  292. // 主菜单
  293. int main() {
  294. check:
  295. if (!authenticate()) {
  296. cout << "认证失败! (Authentication failed!)\n";
  297. goto check;
  298. }
  299. loadUsers();
  300. loadBooks();
  301. int choice;
  302. do {
  303. cout << "1. 添加图书 (Add Book)\n2. 删除图书 (Delete Book)\n3. 显示所有图书 (Display Books)\n"
  304. << "4. 查找图书 (Search Book)\n5. 添加用户 (Add User)\n6. 删除用户 (Delete User)\n"
  305. << "7. 修改用户 (Modify User)\n8. 显示所有用户 (Display Users)\n9. 查找用户 (Search User)\n"
  306. << "10. 借阅图书 (Borrow Book)\n11. 归还图书 (Return Book)\n12. 列出未归还图书 (List Overdue Books)\n"
  307. << "13. 退出 (Exit)\n";
  308. cout << "Enter your choice: ";
  309. cin >> choice;
  310. switch (choice) {
  311. case 1: addBook(); break;
  312. case 2: deleteBook(); break;
  313. case 3: displayBooks(); break;
  314. case 4: searchBook(); break;
  315. case 5: addUser(); break;
  316. case 6: deleteUser(); break;
  317. case 7: modifyUser(); break;
  318. case 8: displayUsers(); break;
  319. case 9: searchUser(); break;
  320. case 10: borrowBook(); break;
  321. case 11: returnBook(); break;
  322. case 12: listOverdueBooks(); break;
  323. case 13: break;
  324. default: cout << "无效选择! (Invalid choice!)\n"; break;
  325. }
  326. } while (choice != 13);
  327. saveUsers();
  328. saveBooks();
  329. return 0;
  330. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/898675
推荐阅读
相关标签
  

闽ICP备14008679号