Advertisement
plarmi

workcpp_5_1

Jun 19th, 2023
786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. class Book {
  6. private:
  7.     std::string author;
  8.     std::string title;
  9.     std::string publisher;
  10.     int year;
  11.     int quantity;
  12.     int pages;
  13.  
  14. public:
  15.     explicit Book(const std::string& author, const std::string& title, const std::string& publisher,
  16.                   int year, int quantity, int pages)
  17.             : author(author), title(title), publisher(publisher), year(year), quantity(quantity), pages(pages) {}
  18.  
  19.     const std::string& getAuthor() const {
  20.         return author;
  21.     }
  22.  
  23.     const std::string& getTitle() const {
  24.         return title;
  25.     }
  26.  
  27.     const std::string& getPublisher() const {
  28.         return publisher;
  29.     }
  30.  
  31.     int getYear() const {
  32.         return year;
  33.     }
  34.  
  35.     int getQuantity() const {
  36.         return quantity;
  37.     }
  38.  
  39.     int getPages() const {
  40.         return pages;
  41.     }
  42.  
  43.     void display() const {
  44.         std::cout << "Author: " << author << std::endl;
  45.         std::cout << "Title: " << title << std::endl;
  46.         std::cout << "Publisher: " << publisher << std::endl;
  47.         std::cout << "Year: " << year << std::endl;
  48.         std::cout << "Quantity: " << quantity << std::endl;
  49.         std::cout << "Pages: " << pages << std::endl;
  50.         std::cout << std::endl;
  51.     }
  52. };
  53.  
  54. int main() {
  55.     std::vector<Book> books;
  56.  
  57.     // Добавляем книги в массив
  58.     books.emplace_back("Author 1", "Title 1", "Publisher 1", 2020, 5, 200);
  59.     books.emplace_back("Author 2", "Title 2", "Publisher 2", 2021, 3, 150);
  60.     books.emplace_back("Author 1", "Title 3", "Publisher 3", 2022, 2, 300);
  61.     books.emplace_back("Author 3", "Title 4", "Publisher 1", 2022, 4, 250);
  62.     books.emplace_back("Author 2", "Title 5", "Publisher 2", 2023, 1, 180);
  63.  
  64.     // Список книг заданного автора
  65.     std::cout << "Список книг заданного автора:" << std::endl;
  66.     std::string searchAuthor = "Author 1";
  67.     for (const auto& book : books) {
  68.         if (book.getAuthor() == searchAuthor) {
  69.             book.display();
  70.         }
  71.     }
  72.  
  73.     // Список книг, выпущенных заданным издательством
  74.     std::cout << "Список книг, выпущенных заданным издательством:" << std::endl;
  75.     std::string searchPublisher = "Publisher 2";
  76.     for (const auto& book : books) {
  77.         if (book.getPublisher() == searchPublisher) {
  78.             book.display();
  79.         }
  80.     }
  81.  
  82.     // Список книг, выпущенных после заданного года
  83.     std::cout << "Список книг, выпущенных после заданного года:" << std::endl;
  84.     int searchYear = 2021;
  85.     for (const auto& book : books) {
  86.         if (book.getYear() > searchYear) {
  87.             book.display();
  88.         }
  89.     }
  90.  
  91.     return 0;
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement