Berbagi teknologi

C Menerapkan sistem manajemen informasi perpustakaan

2024-07-12

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

1. Deskripsi masalah:

Bahan-bahan yang ada di perpustakaan sangatlah banyak, jika peredaran bahan-bahan tersebut dapat diklasifikasi dan dikelola maka akan memberikan banyak kemudahan, oleh karena itu diperlukan suatu sistem pengelolaan media perpustakaan. Ada tiga kategori utama bahan di perpustakaan: buku, CD video, dan gambar. Atribut umum dari ketiga jenis item ini adalah: nomor, judul, penulis, rating (tanpa rating, dewasa umum, anak-anak), dll. Diantaranya, kategori buku menambah informasi seperti penerbit, nomor ISBN, dan nomor halaman; kategori video CD menambahkan informasi seperti nama produser, tahun produksi, dan durasi video; panjang dan lebar pekerjaan (dalam sentimeter, bilangan bulat) dan keterangan lainnya. Pembaca: Informasi dasar, jumlah buku yang tersedia untuk dipinjam Administrator: Bertanggung jawab untuk meminjam

2. Persyaratan fungsional:

(1) Tambahkan item: Terutama melengkapi penambahan informasi pada tiga jenis item di perpustakaan, memerlukan nomor unik. Ketika nomor duplikat ditambahkan, ia meminta untuk menambahkan data duplikat dan membatalkan penambahan: Item kueri Ada tiga cara untuk mengkueri item, yaitu: Kueri berdasarkan judul: Masukkan judul dan keluarkan informasi kueri. Jika rekaman tidak ada, Kemudian akan muncul pesan "Judul ini + ada!"; kategori dan menampilkan informasi yang ditanyakan. Informasi, jika tidak ada catatan, maka akan muncul pesan "Tidak ada item dalam kategori ini!";

(2) Tampilkan perpustakaan item: Menampilkan semua informasi item di perpustakaan item saat ini, dengan setiap catatan menempati satu baris.

(3) Edit item: Catatan terkait dapat diubah sesuai dengan hasil kueri.

(4) Hapus item: terutama menyelesaikan penghapusan informasi item perpustakaan. Jika perpustakaan item saat ini kosong, ia akan menanyakan "Perpustakaan item kosong!" dan kembali ke operasi: jika tidak, masukkan nomor yang akan dihapus, dan hapus catatan item berdasarkan nomor tersebut tidak ada di perpustakaan item, ia akan menampilkan "Nomor ini tidak ada".

(5) Peminjaman, pengelola bertanggung jawab meminjamkan barang kepada pembaca dan menyimpan datanya ke dalam file.

(6) Informasi statistik menampilkan jumlah total item di perpustakaan item saat ini, dan berdasarkan kategori item, menghitung jumlah item dari setiap kategori di item sebelum gunung dan menampilkannya.

(7) Penyimpanan item: Menyimpan informasi item dalam program saat ini ke dalam file.

(8) Membaca item dan membaca informasi item dari file ke dalam program. Setelah mengembangkan kode, Anda perlu mengujinya. Jika ada kesalahan yang dilaporkan, harap modifikasi untuk memastikan semua fungsi dapat berjalan normal.

  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. Efek operasi:

Pintu masuk:

statistik:

Menanyakan:

Jika Anda memerlukan bantuan untuk desain mata kuliah atau skripsi, silakan tambahkan WeChat: LAF20151116