• C++ 实现图书馆资料管理系统


    1、问题描述 :

    图书馆中的资料很多,如果能分类对其资料流通进行管理,将会带来很多方 便,因此需要有一个媒体库管理系统。 图书馆共有三大类物品资料:图书、视频光盘、图画。 这三类物品共同具有的属性有:编号、标题、作者、评级(未评级,一般 成人,儿童) 等。其中图书类增加出版社、ISBN号、页数等信息;视频光盘类增加出品者 的名字、出品年份和视频时长等信息;图画类增加出品国籍、作品的长和宽 (以厘米计,整数)等信息。 读者:基本信息,可借阅本数 管理员:负责借阅

    2、功能要求 :

    (1)添加物品:主要完成图书馆三类物品信息的添加,要求编号唯一。当添 加了重复的编号时,则提示数据添加重复并取消添加:查询物品可按照三种 方式来查询物品,分别为: 按标题查询:输入标题,输出所查询的信息,若不存在该记录,则提示“该 标题+存在!”; 按编号查询:输入编号,输出所查询的信息,若不存在该记录,则提示“该 编号不存在!”; 按类别查询:输入类别,输出所查询的信息,若不存在记录,则提示“该类 别没有物品!”;

    (2)显示物品库:输出当前物品库中所有物品信息,每条记录占据一行。

    (3)编辑物品:可根据查询结果对相应的记录进行修改,修改时注意编号的 唯一性。

    (4)删除物品:主要完成图书馆物品信息的删除。如果当前物品库为空,则 提示“物品库为空!”,并返回操作:否则,输入要删除的编号,根据编号删 除该物品的记录,如果该编号不在物品库中,则提示“该编号不存在”。

    (5)借阅,管理员负责将物品借阅给读者,数据保存到文件中 。

    (6)统计信息 输出当前物品库中总物品数,以及按物品类别,统计出山前物品中各类别的 物品数并显。

    (7)物品存盘:将当前程序中的物品信息存入文件中。

    (8)读出物品 从文件中将物品信息读入程序。 开发完代码后需要进行测试,如果报错请修改,知道各项功能都可以正常运行。

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    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(pair.second)) ||
    104. (type == "Video" && dynamic_cast(pair.second)) ||
    105. (type == "Picture" && dynamic_cast(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(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(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(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(pair.second)) books++;
    241. else if (dynamic_cast(pair.second)) videos++;
    242. else if (dynamic_cast(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), 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), 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), 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), 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), 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), 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、运行效果:

    录入:

    统计:

    查询:

    需要协助课设或论文的加微信:LAF20151116

  • 相关阅读:
    数据结构与算法-二分查找
    阿里云服务器的介绍和使用
    【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
    含电热联合系统的微电网运行优化附Matlab代码
    【Unity3D】2D动画
    【Shiro】SpringBoot集成Shiro权限认证《上》
    你们天天说的应用性能监控: Sky Walking
    Workerman开启ssl方法如下
    Win11一键重装系统后如何使用自带的故障检测修复功能
    人工智能AI风口已开:如何赋予UI设计与视频剪辑新生命
  • 原文地址:https://blog.csdn.net/u010225915/article/details/140359023