Technology Sharing

C Implementing library information management system

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Problem description:

There are a lot of materials in the library. If the circulation of materials can be managed by classification, it will bring a lot of convenience. Therefore, a media library management system is needed. There are three major categories of materials in the library: books, video discs, and pictures. The common attributes of these three categories of items are: number, title, author, rating (unrated, generally adults, children), etc. Among them, books add publishers, ISBN numbers, number of pages and other information; video discs add producer names, production years and video duration and other information; pictures add producer nationality, length and width of the work (in centimeters, integers) and other information. Readers: basic information, number of books that can be borrowed Administrators: responsible for borrowing

2. Functional requirements:

(1) Adding items: mainly completing the addition of three types of information on items in the library, requiring unique numbers. When a duplicate number is added, it will prompt that the data has been added repeatedly and cancel the addition: There are three ways to search for items, namely: Search by title: enter the title and output the searched information. If the record does not exist, it will prompt "The title + exists!"; Search by number: enter the number and output the searched information. If the record does not exist, it will prompt "The number does not exist!"; Search by category: enter the category and output the searched information. If the record does not exist, it will prompt "There is no item in this category!";

(2) Display item library: Output all item information in the current item library, with each record occupying one line.

(3) Edit items: You can modify the corresponding records based on the query results. When modifying, pay attention to the uniqueness of the number.

(4) Delete items: mainly delete the information of library items. If the current item library is empty, it will prompt "The item library is empty!" and return to the operation: otherwise, enter the number to be deleted, and delete the record of the item according to the number. If the number is not in the item library, it will prompt "The number does not exist".

(5) Borrowing: The administrator is responsible for lending items to readers and saving the data in files.

(6) Statistical information outputs the total number of items in the current item library, and the number of items in each category in the front mountain items is counted and displayed by item category.

(7) Save item: Save the item information in the current program into a file.

(8) Read out items and read item information from the file into the program. After developing the code, you need to test it. If there is an error, please modify it until all functions can run normally.

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include <unordered_map>
  6. #include <windows.h>
  7. using namespace std;
  8. enum Rating { UNRATED, ADULT, CHILD };
  9. string ratingToString(Rating rating) {
  10. switch (rating) {
  11. case UNRATED: return "未评级";
  12. case ADULT: return "成人";
  13. case CHILD: return "儿童";
  14. default: return "";
  15. }
  16. }
  17. class Media {
  18. public:
  19. int id;
  20. string title;
  21. string author;
  22. Rating rating;
  23. Media(int id, string title, string author, Rating rating)
  24. : id(id), title(title), author(author), rating(rating) {}
  25. virtual void display() const {
  26. cout << "ID: " << id << ", 标题: " << title << ", 作者: " << author << ", 评级: " << ratingToString(rating) << endl;
  27. }
  28. virtual ~Media() {}
  29. };
  30. class Book : public Media {
  31. public:
  32. string publisher;
  33. string isbn;
  34. int pages;
  35. Book(int id, string title, string author, Rating rating, string publisher, string isbn, int pages)
  36. : Media(id, title, author, rating), publisher(publisher), isbn(isbn), pages(pages) {}
  37. void display() const override {
  38. Media::display();
  39. cout << "出版社: " << publisher << ", ISBN: " << isbn << ", 页数: " << pages << endl;
  40. }
  41. };
  42. class Video : public Media {
  43. public:
  44. string producer;
  45. int year;
  46. int duration;
  47. Video(int id, string title, string author, Rating rating, string producer, int year, int duration)
  48. : Media(id, title, author, rating), producer(producer), year(year), duration(duration) {}
  49. void display() const override {
  50. Media::display();
  51. cout << "出品者: " << producer << ", 年份: " << year << ", 时长: " << duration << " mins" << endl;
  52. }
  53. };
  54. class Picture : public Media {
  55. public:
  56. string nationality;
  57. int length;
  58. int width;
  59. Picture(int id, string title, string author, Rating rating, string nationality, int length, int width)
  60. : Media(id, title, author, rating), nationality(nationality), length(length), width(width) {}
  61. void display() const override {
  62. Media::display();
  63. cout << "国籍: " << nationality << ", 尺寸: " << length << "x" << width << " cm" << endl;
  64. }
  65. };
  66. class Library {
  67. unordered_map<int, Media*> mediaCollection;
  68. public:
  69. ~Library() {
  70. for (auto& pair : mediaCollection) {
  71. delete pair.second;
  72. }
  73. }
  74. void addMedia(Media* media) {
  75. if (mediaCollection.find(media->id) != mediaCollection.end()) {
  76. cout << "检测到重复的 ID, 未添加资料." << endl;
  77. delete media;
  78. return;
  79. }
  80. mediaCollection[media->id] = media;
  81. cout << "资料添加成功." << endl;
  82. }
  83. void queryByTitle(const string& title) {
  84. bool found = false;
  85. for (const auto& pair : mediaCollection) {
  86. if (pair.second->title == title) {
  87. pair.second->display();
  88. found = true;
  89. }
  90. }
  91. if (!found) cout << " 标题 "" << title << "" 不存在!" << endl;
  92. }
  93. void queryById(int id) {
  94. if (mediaCollection.find(id) != mediaCollection.end()) {
  95. mediaCollection[id]->display();
  96. } else {
  97. cout << " ID "" << id << "" 不存在!" << endl;
  98. }
  99. }
  100. void queryByType(const string& type) {
  101. bool found = false;
  102. for (const auto& pair : mediaCollection) {
  103. if ((type == "Book" && dynamic_cast<Book*>(pair.second)) ||
  104. (type == "Video" && dynamic_cast<Video*>(pair.second)) ||
  105. (type == "Picture" && dynamic_cast<Picture*>(pair.second))) {
  106. pair.second->display();
  107. found = true;
  108. }
  109. }
  110. if (!found) cout << "分类没有 "" << type << "" 找到!" << endl;
  111. }
  112. void displayAll() {
  113. if (mediaCollection.empty()) {
  114. cout << "数据库中没有相关资料." << endl;
  115. return;
  116. }
  117. for (const auto& pair : mediaCollection) {
  118. pair.second->display();
  119. }
  120. }
  121. void editMedia(int id, Media* newMedia) {
  122. if (mediaCollection.find(id) == mediaCollection.end()) {
  123. cout << " ID "" << id << "" 不存在!" << endl;
  124. delete newMedia;
  125. return;
  126. }
  127. delete mediaCollection[id];
  128. mediaCollection[id] = newMedia;
  129. cout << "资料编辑成功." << endl;
  130. }
  131. void deleteMedia(int id) {
  132. if (mediaCollection.find(id) == mediaCollection.end()) {
  133. cout << " ID "" << id << "" 不存在!" << endl;
  134. return;
  135. }
  136. delete mediaCollection[id];
  137. mediaCollection.erase(id);
  138. cout << "资料删除成功." << endl;
  139. }
  140. void saveToFile(const string& filename) {
  141. ofstream file(filename, ios::binary);
  142. if (!file) {
  143. cout << "无法打开文件进行写入." << endl;
  144. return;
  145. }
  146. for (const auto& pair : mediaCollection) {
  147. Media* media = pair.second;
  148. file.write((char*)&media->id, sizeof(media->id));
  149. size_t length = media->title.size();
  150. file.write((char*)&length, sizeof(length));
  151. file.write(media->title.c_str(), length);
  152. length = media->author.size();
  153. file.write((char*)&length, sizeof(length));
  154. file.write(media->author.c_str(), length);
  155. file.write((char*)&media->rating, sizeof(media->rating));
  156. if (Book* book = dynamic_cast<Book*>(media)) {
  157. char type = 'B';
  158. file.write(&type, sizeof(type));
  159. length = book->publisher.size();
  160. file.write((char*)&length, sizeof(length));
  161. file.write(book->publisher.c_str(), length);
  162. length = book->isbn.size();
  163. file.write((char*)&length, sizeof(length));
  164. file.write(book->isbn.c_str(), length);
  165. file.write((char*)&book->pages, sizeof(book->pages));
  166. } else if (Video* video = dynamic_cast<Video*>(media)) {
  167. char type = 'V';
  168. file.write(&type, sizeof(type));
  169. length = video->producer.size();
  170. file.write((char*)&length, sizeof(length));
  171. file.write(video->producer.c_str(), length);
  172. file.write((char*)&video->year, sizeof(video->year));
  173. file.write((char*)&video->duration, sizeof(video->duration));
  174. } else if (Picture* picture = dynamic_cast<Picture*>(media)) {
  175. char type = 'P';
  176. file.write(&type, sizeof(type));
  177. length = picture->nationality.size();
  178. file.write((char*)&length, sizeof(length));
  179. file.write(picture->nationality.c_str(), length);
  180. file.write((char*)&picture->length, sizeof(picture->length));
  181. file.write((char*)&picture->width, sizeof(picture->width));
  182. }
  183. }
  184. cout << "数据保存到文件." << endl;
  185. }
  186. void loadFromFile(const string& filename) {
  187. ifstream file(filename, ios::binary);
  188. if (!file) {
  189. cout << "无法打开文件进行读取." << endl;
  190. return;
  191. }
  192. while (file.peek() != EOF) {
  193. int id;
  194. file.read((char*)&id, sizeof(id));
  195. size_t length;
  196. file.read((char*)&length, sizeof(length));
  197. string title(length, ' ');
  198. file.read(&title[0], length);
  199. file.read((char*)&length, sizeof(length));
  200. string author(length, ' ');
  201. file.read(&author[0], length);
  202. Rating rating;
  203. file.read((char*)&rating, sizeof(rating));
  204. char type;
  205. file.read(&type, sizeof(type));
  206. if (type == 'B') {
  207. file.read((char*)&length, sizeof(length));
  208. string publisher(length, ' ');
  209. file.read(&publisher[0], length);
  210. file.read((char*)&length, sizeof(length));
  211. string isbn(length, ' ');
  212. file.read(&isbn[0], length);
  213. int pages;
  214. file.read((char*)&pages, sizeof(pages));
  215. mediaCollection[id] = new Book(id, title, author, rating, publisher, isbn, pages);
  216. } else if (type == 'V') {
  217. file.read((char*)&length, sizeof(length));
  218. string producer(length, ' ');
  219. file.read(&producer[0], length);
  220. int year, duration;
  221. file.read((char*)&year, sizeof(year));
  222. file.read((char*)&duration, sizeof(duration));
  223. mediaCollection[id] = new Video(id, title, author, rating, producer, year, duration);
  224. } else if (type == 'P') {
  225. file.read((char*)&length, sizeof(length));
  226. string nationality(length, ' ');
  227. file.read(&nationality[0], length);
  228. int length, width;
  229. file.read((char*)&length, sizeof(length));
  230. file.read((char*)&width, sizeof(width));
  231. mediaCollection[id] = new Picture(id, title, author, rating, nationality, length, width);
  232. }
  233. }
  234. cout << "从文件加载的数据." << endl;
  235. }
  236. void countItems() {
  237. cout << "Total items: " << mediaCollection.size() << endl;
  238. int books = 0, videos = 0, pictures = 0;
  239. for (const auto& pair : mediaCollection) {
  240. if (dynamic_cast<Book*>(pair.second)) books++;
  241. else if (dynamic_cast<Video*>(pair.second)) videos++;
  242. else if (dynamic_cast<Picture*>(pair.second)) pictures++;
  243. }
  244. cout << "Books: " << books << ", Videos: " << videos << ", Pictures: " << pictures << endl;
  245. }
  246. };
  247. int main() {
  248. SetConsoleOutputCP(65001);
  249. Library library;
  250. while (true) {
  251. cout << "欢迎使用图书馆资料管理系统: n1. 添加资料n2. 按标题查询资料n3. 按ID查询资料n4. 按类别查询资料n5. 显示所有资料n6. 编辑资料n7. 删除资料n8. 资料存盘n9. 读取资料n10. 统计资料n11. 退出n";
  252. int choice;
  253. cin >> choice;
  254. if (choice == 1) {
  255. cout << "选择类别: 1. 图书 2. 视频 3. 图画n";
  256. int type;
  257. cin >> type;
  258. int id;
  259. string title, author;
  260. int rating;
  261. cout << "输入ID: ";
  262. cin >> id;
  263. cout << "输入标题: ";
  264. cin.ignore();
  265. getline(cin, title);
  266. cout << "输入作者: ";
  267. getline(cin, author);
  268. cout << "输入评级 (0 未评级, 1 成人, 2 儿童): ";
  269. cin >> rating;
  270. if (type == 1) {
  271. string publisher, isbn;
  272. int pages;
  273. cout << "输入出版社: ";
  274. cin.ignore();
  275. getline(cin, publisher);
  276. cout << "输入ISBN: ";
  277. getline(cin, isbn);
  278. cout << "输入页数: ";
  279. cin >> pages;
  280. library.addMedia(new Book(id, title, author, static_cast<Rating>(rating), publisher, isbn, pages));
  281. } else if (type == 2) {
  282. string producer;
  283. int year, duration;
  284. cout << "输入出品者: ";
  285. cin.ignore();
  286. getline(cin, producer);
  287. cout << "输入出品年份: ";
  288. cin >> year;
  289. cout << "输入时长(分钟): ";
  290. cin >> duration;
  291. library.addMedia(new Video(id, title, author, static_cast<Rating>(rating), producer, year, duration));
  292. } else if (type == 3) {
  293. string nationality;
  294. int length, width;
  295. cout << "输入出品国籍: ";
  296. cin.ignore();
  297. getline(cin, nationality);
  298. cout << "输入长度(厘米): ";
  299. cin >> length;
  300. cout << "输入宽度(厘米): ";
  301. cin >> width;
  302. library.addMedia(new Picture(id, title, author, static_cast<Rating>(rating), nationality, length, width));
  303. }
  304. } else if (choice == 2) {
  305. string title;
  306. cout << "输入标题: ";
  307. cin.ignore();
  308. getline(cin, title);
  309. library.queryByTitle(title);
  310. } else if (choice == 3) {
  311. int id;
  312. cout << "输入ID: ";
  313. cin >> id;
  314. library.queryById(id);
  315. } else if (choice == 4) {
  316. string type;
  317. cout << "输入类别 (图书, 视频, 图画): ";
  318. cin.ignore();
  319. getline(cin, type);
  320. library.queryByType(type);
  321. } else if (choice == 5) {
  322. library.displayAll();
  323. } else if (choice == 6) {
  324. int id;
  325. cout << "输入要编辑资料的ID: ";
  326. cin >> id;
  327. cout << "选择新分类: 1. 图书 2. 视频 3. 图画n";
  328. int type;
  329. cin >> type;
  330. string title, author;
  331. int rating;
  332. cout << "输入新标题: ";
  333. cin.ignore();
  334. getline(cin, title);
  335. cout << "输入新作者: ";
  336. getline(cin, author);
  337. cout << "输入新评级 (0 未评级, 1 成人, 2 儿童): ";
  338. cin >> rating;
  339. if (type == 1) {
  340. string publisher, isbn;
  341. int pages;
  342. cout << "输入新的出版社: ";
  343. cin.ignore();
  344. getline(cin, publisher);
  345. cout << "输入新的ISBN: ";
  346. getline(cin, isbn);
  347. cout << "输入新的页数: ";
  348. cin >> pages;
  349. library.editMedia(id, new Book(id, title, author, static_cast<Rating>(rating), publisher, isbn, pages));
  350. } else if (type == 2) {
  351. string producer;
  352. int year, duration;
  353. cout << "输入新的作者: ";
  354. cin.ignore();
  355. getline(cin, producer);
  356. cout << "输入新的年份: ";
  357. cin >> year;
  358. cout << "输入新的持续时间(分钟): ";
  359. cin >> duration;
  360. library.editMedia(id, new Video(id, title, author, static_cast<Rating>(rating), producer, year, duration));
  361. } else if (type == 3) {
  362. string nationality;
  363. int length, width;
  364. cout << "输入新国籍: ";
  365. cin.ignore();
  366. getline(cin, nationality);
  367. cout << "输入新长度(厘米): ";
  368. cin >> length;
  369. cout << "输入新宽度(厘米): ";
  370. cin >> width;
  371. library.editMedia(id, new Picture(id, title, author, static_cast<Rating>(rating), nationality, length, width));
  372. }
  373. } else if (choice == 7) {
  374. int id;
  375. cout << "输入要删除的资料ID: ";
  376. cin >> id;
  377. library.deleteMedia(id);
  378. } else if (choice == 8) {
  379. string filename;
  380. cout << "输入文件名: ";
  381. cin.ignore();
  382. getline(cin, filename);
  383. library.saveToFile(filename);
  384. } else if (choice == 9) {
  385. string filename;
  386. cout << "输入文件名: ";
  387. cin.ignore();
  388. getline(cin, filename);
  389. library.loadFromFile(filename);
  390. } else if (choice == 10) {
  391. library.countItems();
  392. } else if (choice == 11) {
  393. break;
  394. } else {
  395. cout << "无效的选择,请再试一次." << endl;
  396. }
  397. }
  398. return 0;
  399. }

3. Operation effect:

Entry:

statistics:

Inquire:

If you need help with course design or thesis, please add WeChat: LAF20151116