Advertisement
gasaichan

pozhilaya_biblioteka

May 15th, 2019
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. // MP_OP_LAB5.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8. #include <algorithm>
  9.  
  10. using namespace std;
  11.  
  12. struct book {
  13.     string author_surname;
  14.     string author_name;
  15.     string book_name;
  16.     string publisher_name;
  17.     int publishment_year;
  18. };
  19.  
  20. void printMenu() {
  21.     cout << endl;
  22.     cout << "1. Добавить новую книгу" << endl;
  23.     cout << "2. Распечатать информацию о всех книгах" << endl;
  24.     cout << "3. Найти все книги заданного автора" << endl;
  25.     cout << "4. Найти всех авторов заданного издательства" << endl;
  26.     cout << "Ваш выбор: ";
  27. }
  28.  
  29. int handleInput() {
  30.     int choice;
  31.     while (true)
  32.     {
  33.         cin >> choice;
  34.         if (choice >= 1 && choice <= 4) { break; }
  35.         else { cout << endl << "Повторите ввод: "; continue; }
  36.     }
  37.     return choice;
  38. }
  39.  
  40. book * insertBook() {
  41.     book * newBook = new book();
  42.  
  43.     cout << "Введите фамилию автора: ";
  44.     cin >> newBook->author_surname;
  45.  
  46.     cout << "Введите имя автора: ";
  47.     cin >> newBook->author_name;
  48.  
  49.     cout << "Введите название книги: ";
  50.     cin >> newBook->book_name;
  51.  
  52.     cout << "Введите название издателя: ";
  53.     cin >> newBook->publisher_name;
  54.  
  55.     cout << "Введите год издания: ";
  56.     cin >> newBook->publishment_year;
  57.  
  58.     return newBook;
  59. }
  60.  
  61. int main()
  62. {
  63.     setlocale(LC_ALL, "Russian");
  64.  
  65.     vector<book *> books;
  66.     string author;
  67.     vector<book *> foundBooks;
  68.     vector<string> authors;
  69.     book * newBook = new book();
  70.  
  71.     while (true) {
  72.         printMenu();
  73.         int choice = handleInput();
  74.         switch (choice) {
  75.         case 1:
  76.             books.push_back(insertBook());
  77.             break;
  78.         case 2:
  79.             for (auto b : books) {
  80.                 std::cout << "Фамилия автора: " << b->author_surname
  81.                     << "; Имя автора: " << b->author_name
  82.                     << "; Название книги: " << b->book_name
  83.                     << "; Издатель: " << b->publisher_name
  84.                     << "; Год издания: " << b->publishment_year
  85.                     << endl;
  86.             }
  87.             break;
  88.         case 3:
  89.             cout << "Введите фамилию автора: ";
  90.             cin >> author;
  91.  
  92.             foundBooks.clear();
  93.             for (auto b : books) {
  94.                 if (b->author_surname == author) {
  95.                     foundBooks.push_back(b);
  96.                 }
  97.             }
  98.  
  99.             cout << "Книги данного автора: " << endl;
  100.             for (auto b : foundBooks) {
  101.                 std::cout << "Фамилия автора: " << b->author_surname
  102.                     << "; Имя автора: " << b->author_name
  103.                     << "; Название книги: " << b->book_name
  104.                     << "; Издатель: " << b->publisher_name
  105.                     << "; Год издания: " << b->publishment_year
  106.                     << endl;
  107.             }
  108.             break;
  109.         case 4:
  110.             string publisher;
  111.             cout << "Введите название издательства: ";
  112.             cin >> publisher;
  113.  
  114.             authors.clear();
  115.             for (auto b : books) {
  116.                 if (b->publisher_name == publisher) {
  117.                     authors.push_back(b->author_surname);
  118.                 }
  119.             }
  120.  
  121.             sort(authors.begin(), authors.end());
  122.  
  123.             cout << "Авторы заданного издательства: " << endl;
  124.             for (auto a : authors) {
  125.                 cout << a << endl;
  126.             }
  127.             break;
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement