기술나눔

C 도서관정보관리시스템을 구현한다.

2024-07-12

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

1. 문제 설명:

도서관에는 자료가 많이 있는데, 자료의 순환을 분류하여 관리할 수 있다면 편의성이 높아질 것이다. 도서관에는 책, 비디오 CD, 사진의 세 가지 주요 자료 카테고리가 있습니다. 이 세 가지 항목 유형의 공통 속성은 번호, 제목, 작성자, 등급(등급 없음, 일반 성인, 어린이) 등입니다. 그 중 도서 카테고리에는 출판사, ISBN 번호, 페이지 번호 등의 정보가 추가되고, 비디오 CD 카테고리에는 제작자 이름, 제작 연도, 영상 길이 등의 정보가 추가되고, 사진 카테고리에는 제작 국가가 추가됩니다. 작품의 길이와 너비(센티미터, 정수) 및 기타 정보. 독자 : 기본정보, 대출가능 도서수 관리자 : 대출담당자

2. 기능적 요구사항:

(1) 항목 추가: 고유 번호가 필요한 라이브러리의 세 가지 유형의 항목에 대한 정보 추가를 주로 완료합니다. 중복된 번호가 추가되면 중복된 데이터를 추가하고 추가를 취소하라는 메시지가 표시됩니다. 항목을 쿼리하는 방법에는 세 가지가 있습니다. 제목으로 쿼리: 레코드가 존재하지 않는 경우 제목을 입력하고 쿼리된 정보를 출력합니다. 그러면 "이 제목 + 존재합니다!"라는 메시지가 표시됩니다. 번호를 입력하고 쿼리된 정보를 출력합니다. 레코드가 존재하지 않으면 "이 번호는 존재하지 않습니다."라는 메시지가 표시됩니다. 카테고리를 선택하고 조회된 정보를 출력합니다. 기록이 없으면 "이 카테고리에 항목이 없습니다!"라는 메시지가 표시됩니다.

(2) 아이템 라이브러리 표시: 현재 아이템 라이브러리의 모든 아이템 정보를 각 레코드가 한 줄씩 출력합니다.

(3) 편집 항목: 해당 레코드는 쿼리 결과에 따라 수정될 수 있습니다. 수정 시에는 번호의 고유성에 주의하세요.

(4) 항목 삭제: 주로 라이브러리 항목 정보 삭제를 완료합니다. 현재 항목 라이브러리가 비어 있으면 "항목 라이브러리가 비어 있습니다!"라는 메시지가 표시되고 작업으로 돌아갑니다. 그렇지 않으면 삭제할 번호를 입력하고 해당 번호를 기준으로 항목의 기록을 삭제합니다. 항목 라이브러리에 없으면 "이 번호는 존재하지 않습니다"라는 메시지가 표시됩니다.

(5) 대출, 관리자는 독자에게 항목을 대출하고 데이터를 파일로 저장하는 책임을 집니다.

(6) 통계정보는 현재 아이템 라이브러리에 있는 총 아이템 개수를 출력하며, 아이템 카테고리별로 산 앞의 아이템에 있는 카테고리별 아이템 개수를 세어 표시해 줍니다.

(7) 항목 저장 : 현재 프로그램의 항목 정보를 파일로 저장합니다.

(8) 항목을 읽고 파일의 항목 정보를 프로그램으로 읽어옵니다. 코드를 개발한 후 테스트해야 합니다. 오류가 보고되면 모든 기능이 정상적으로 실행될 수 있도록 수정하세요.

  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. 작동 효과:

기입:

통계:

문의:

코스 설계나 논문에 도움이 필요하시면 WeChat을 추가해주세요: LAF20151116