fork download
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <limits>
  5. using namespace std;
  6.  
  7. struct Book {
  8. int id;
  9. string title;
  10. string author;
  11. bool borrowed;
  12. };
  13.  
  14. // Ham nhap so nguyen
  15. bool readInt(const string& prompt, int& x) {
  16. cout << prompt;
  17.  
  18. if (cin >> x)
  19. return true;
  20.  
  21. if (cin.eof())
  22. return false;
  23.  
  24. cin.clear();
  25. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  26.  
  27. cout << "Vui long nhap so nguyen!\n";
  28. return false;
  29. }
  30.  
  31. // Tim sach theo ID
  32. Book* findBook(Book* ds, int n, int id) {
  33. for (int i = 0; i < n; i++) {
  34. if (ds[i].id == id) {
  35. return &ds[i];
  36. }
  37. }
  38.  
  39. return nullptr;
  40. }
  41.  
  42. // Nhap danh sach
  43. void inputBooks(Book* ds, int n) {
  44. for (int i = 0; i < n; i++) {
  45. cout << "\nSach thu " << i + 1 << ":\n";
  46.  
  47. while (true) {
  48. int id;
  49.  
  50. if (!readInt("ID: ", id))
  51. continue;
  52.  
  53. if (findBook(ds, i, id) != nullptr) {
  54. cout << "ID da ton tai!\n";
  55. continue;
  56. }
  57.  
  58. ds[i].id = id;
  59. break;
  60. }
  61.  
  62. cout << "Tieu de: ";
  63. getline(cin >> ws, ds[i].title);
  64.  
  65. cout << "Tac gia: ";
  66. getline(cin >> ws, ds[i].author);
  67.  
  68. ds[i].borrowed = false;
  69. }
  70. }
  71.  
  72. // Hien thi mot cuon sach
  73. void printBook(const Book& b) {
  74. cout << "ID: " << b.id
  75. << " | Tieu de: " << b.title
  76. << " | Tac gia: " << b.author
  77. << " | Trang thai: "
  78. << (b.borrowed ? "Da muon" : "Chua muon")
  79. << '\n';
  80. }
  81.  
  82. // Hien thi danh sach
  83. void showBooks(Book* ds, int n) {
  84. if (n == 0) {
  85. cout << "Danh sach trong!\n";
  86. return;
  87. }
  88.  
  89. for (int i = 0; i < n; i++) {
  90. printBook(ds[i]);
  91. }
  92. }
  93.  
  94. // Tim va hien thi sach
  95. void searchBook(Book* ds, int n) {
  96. int id;
  97.  
  98. if (!readInt("Nhap ID can tim: ", id))
  99. return;
  100.  
  101. Book* p = findBook(ds, n, id);
  102.  
  103. if (p == nullptr) {
  104. cout << "Khong tim thay sach!\n";
  105. return;
  106. }
  107.  
  108. printBook(*p);
  109. }
  110.  
  111. // Muon sach
  112. void borrowBook(Book* ds, int n) {
  113. int id;
  114.  
  115. if (!readInt("Nhap ID muon muon: ", id))
  116. return;
  117.  
  118. Book* p = findBook(ds, n, id);
  119.  
  120. if (p == nullptr) {
  121. cout << "Khong tim thay sach!\n";
  122. return;
  123. }
  124.  
  125. if (p->borrowed) {
  126. cout << "Sach da duoc muon!\n";
  127. return;
  128. }
  129.  
  130. p->borrowed = true;
  131. cout << "Muon sach thanh cong!\n";
  132. }
  133.  
  134. // Tra sach
  135. void returnBook(Book* ds, int n) {
  136. int id;
  137.  
  138. if (!readInt("Nhap ID muon tra: ", id))
  139. return;
  140.  
  141. Book* p = findBook(ds, n, id);
  142.  
  143. if (p == nullptr) {
  144. cout << "Khong tim thay sach!\n";
  145. return;
  146. }
  147.  
  148. if (!p->borrowed) {
  149. cout << "Sach chua duoc muon!\n";
  150. return;
  151. }
  152.  
  153. p->borrowed = false;
  154. cout << "Tra sach thanh cong!\n";
  155. }
  156.  
  157. // Xoa sach - chi Librarian
  158. void deleteBook(Book*& ds, int& n, bool isLibrarian) {
  159. if (!isLibrarian) {
  160. cout << "Chi librarian duoc xoa sach!\n";
  161. return;
  162. }
  163.  
  164. int id;
  165.  
  166. if (!readInt("Nhap ID sach can xoa: ", id))
  167. return;
  168.  
  169. int index = -1;
  170.  
  171. for (int i = 0; i < n; i++) {
  172. if (ds[i].id == id) {
  173. index = i;
  174. break;
  175. }
  176. }
  177.  
  178. if (index == -1) {
  179. cout << "Khong tim thay sach!\n";
  180. return;
  181. }
  182.  
  183. Book* temp = nullptr;
  184.  
  185. if (n > 1) {
  186. temp = new Book[n - 1];
  187. int j = 0;
  188.  
  189. for (int i = 0; i < n; i++) {
  190. if (i == index)
  191. continue;
  192.  
  193. temp[j++] = ds[i];
  194. }
  195. }
  196.  
  197. delete[] ds;
  198. ds = temp;
  199. n--;
  200.  
  201. cout << "Xoa sach thanh cong!\n";
  202. }
  203.  
  204. // Dang nhap mo phong
  205. bool login(bool& isLibrarian) {
  206. int role;
  207.  
  208. if (!readInt("1. Librarian 2. User\nChon: ", role))
  209. return false;
  210.  
  211. if (role == 2) {
  212. isLibrarian = false;
  213. return true;
  214. }
  215.  
  216. if (role == 1) {
  217. string password;
  218.  
  219. cout << "Nhap mat khau librarian: ";
  220. cin >> password;
  221.  
  222. if (password == "Lib123") {
  223. isLibrarian = true;
  224. return true;
  225. }
  226. }
  227.  
  228. cout << "Dang nhap that bai!\n";
  229. return false;
  230. }
  231.  
  232. int main() {
  233. int n;
  234.  
  235. if (!readInt("Nhap so sach (0..1000): ", n)
  236. || n < 0 || n > 1000) {
  237. cout << "So sach khong hop le!\n";
  238. return 0;
  239. }
  240.  
  241. Book* ds = (n > 0) ? new Book[n] : nullptr;
  242. inputBooks(ds, n);
  243.  
  244. int mainChoice = -1;
  245.  
  246. do {
  247. if (!readInt(
  248. "\n=== MENU CHINH ===\n"
  249. "1. Dang nhap\n"
  250. "0. Thoat\n"
  251. "Chon: ", mainChoice)) {
  252. mainChoice = -1;
  253. if (cin.eof()) break;
  254. continue;
  255. }
  256.  
  257. if (mainChoice == 1) {
  258. bool isLibrarian = false;
  259.  
  260. if (!login(isLibrarian)) {
  261. if (cin.eof()) break;
  262. continue;
  263. }
  264.  
  265. int choice = -1;
  266.  
  267. do {
  268. if (!readInt(
  269. "\n=== MENU THU VIEN ===\n"
  270. "1. Hien thi\n"
  271. "2. Tim sach\n"
  272. "3. Muon sach\n"
  273. "4. Tra sach\n"
  274. "5. Xoa sach\n"
  275. "0. Dang xuat\n"
  276. "Chon: ", choice)) {
  277. choice = -1;
  278. if (cin.eof()) break;
  279. continue;
  280. }
  281.  
  282. switch (choice) {
  283. case 1:
  284. showBooks(ds, n);
  285. break;
  286.  
  287. case 2:
  288. searchBook(ds, n);
  289. break;
  290.  
  291. case 3:
  292. borrowBook(ds, n);
  293. break;
  294.  
  295. case 4:
  296. returnBook(ds, n);
  297. break;
  298.  
  299. case 5:
  300. deleteBook(ds, n, isLibrarian);
  301. break;
  302.  
  303. case 0:
  304. cout << "Da dang xuat!\n";
  305. break;
  306.  
  307. default:
  308. cout << "Lua chon khong hop le!\n";
  309. }
  310.  
  311. if (cin.eof()) break;
  312.  
  313. } while (choice != 0);
  314. }
  315. else if (mainChoice != 0) {
  316. cout << "Lua chon khong hop le!\n";
  317. }
  318.  
  319. if (cin.eof()) break;
  320.  
  321. } while (mainChoice != 0);
  322.  
  323. delete[] ds;
  324. ds = nullptr;
  325.  
  326. cout << "Da giai phong bo nho!\n";
  327. return 0;
  328. }
  329.  
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
Nhap so sach (0..1000): So sach khong hop le!