Advertisement
igoryanchik

1.1, 1.2

Sep 11th, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <vector>
  4. #include <string>
  5. #include <random>
  6. #include <ctime>
  7. using namespace std;
  8.  
  9. using _ptr = bool(*)(const char&);
  10.  
  11. class Book
  12. {
  13. private:
  14.     string first_name_a;
  15.     string second_name_a;
  16.     string titel;
  17.     string publisher;
  18.     int count; 
  19.     int year;
  20.     int paper;
  21.     int electronic;
  22.     int audio;
  23.  
  24.     string names [5] = {"Alice", "Bob", "Carla", "David", "Eva" };
  25.     string surnames[5] = { "Smith", "Johnson", "Brown", "Davis", "Wilson" };
  26.     string publishers[5] = { "Penguin Random House", "HarperCollins", "Simon & Schuster", "Macmillan Publishers", "Hachette Livre" };
  27.     string books[5] = { "To Kill a Mockingbird",    "1984", "The Great Gatsby", "Pride and Prejudice", "The Catcher in the Rye" };
  28.  
  29. public:
  30.  
  31.     Book(string const& f = " ", string const& s = " ", string const& t = " ",
  32.         string const& p = " ", int c = 0, int y = 0, int ta = 0, int  tp = 0, int te = 0)
  33.     {
  34.         first_name_a = f;
  35.         second_name_a = s;
  36.         titel = t;
  37.         publisher = p;
  38.         count = c;
  39.         year = y;
  40.         paper = ta;
  41.         electronic = tp;
  42.         audio = te;
  43.     }
  44.  
  45.     bool operator < (const Book & B) const
  46.     {
  47.         if (count != B.count)
  48.             return (count < B.count);
  49.         else if (year != B.year)
  50.             return (year < B.year);
  51.         else return (titel < B.titel);
  52.     }
  53.  
  54.     int count_() { return count;}
  55.     int year_() { return year; }
  56.     string titel_() { return titel; }
  57.  
  58.     void fill()
  59.     {
  60.         srand(time(NULL));
  61.  
  62.         int i1 = rand() % 5, i2 = rand() % 5, i3 = rand() % 5, i4 = rand() % 5,
  63.             i5 = rand() % 5000, i6 = rand() % 2023, i7 = rand() % 1000, i8 = rand() % 1000, i9 = rand() % 1000;
  64.  
  65.         first_name_a = names[i1];
  66.         second_name_a = surnames[i2];
  67.         titel = books[i3];
  68.         publisher = publishers[i4];
  69.         count = i5;
  70.         year = i6;
  71.         paper = i7;
  72.         electronic = i8;
  73.         audio = i9;
  74.  
  75.     }
  76.  
  77.     void print() const
  78.     {
  79.         cout << "\nName authot: " << first_name_a << "\nSurname author: " << second_name_a <<
  80.             "\nTitel of book: " << titel << "\nCount books: " << count << "\nPublisher: " << publisher <<
  81.             "\nYear of publication: " << year << "\nNumber of paper books: " << paper << "\nNumber of electronic books:" <<
  82.             electronic << "\nNumber of audio books:" << audio << '\n';
  83.     }
  84. };
  85.  
  86.  
  87. Book pop(list<Book>& l, list<Book>::iterator it)
  88. {
  89.     l.erase(it);
  90.     return l.back();
  91. }
  92.  
  93. bool  vowels(char const& c)
  94. {
  95.     return (c == 'A' || c == 'a' ||
  96.             c == 'E' || c == 'e' ||
  97.             c == 'I' || c == 'i' ||
  98.             c == 'O' || c == 'o' ||
  99.             c == 'U' || c == 'u' ||
  100.             c == 'Y' || c == 'y');
  101. }
  102.  
  103. template<class T>
  104. list<T> filter(list<T>& lst, _ptr comp)
  105. {
  106.     list<T> ans;
  107.  
  108.     for (auto it = lst.begin(); it != lst.end(); ++it)
  109.     {
  110.         if (comp(*it))
  111.             ans.push_back(*it);
  112.     }
  113.  
  114.     return ans;
  115. }
  116.  
  117. template<class T>
  118. void push(list<T>& lst, const T&  element)
  119. {
  120.     auto i = lst.begin();
  121.  
  122.     for (; i != lst.end(); ++i)
  123.         if (element < *i)
  124.             break;
  125.  
  126.     lst.insert(i, element);
  127. }
  128.  
  129. template<class T>
  130. void print(list<T>& const lst)
  131. {
  132.     for (auto i = lst.begin(); i != lst.end(); ++i)
  133.         cout << *i << " ";
  134.  
  135.     cout << '\n';
  136. }
  137.  
  138.  
  139. int main()
  140. {
  141.     /*list<char> lst;
  142.     _ptr ptr = &vowels;
  143.  
  144.     for (char i = 'A'; i < 'Z'; ++i)
  145.         lst.push_back(i);
  146.  
  147.     print(lst);
  148.     cout << '\n';
  149.  
  150.     list<char> lst2 = filter(lst, ptr);
  151.  
  152.     print(lst2); cout << '\n';
  153.  
  154.     insertF(lst2, 'A'); print(lst2);*/
  155.  
  156.     list<Book> lst;
  157.  
  158.     for (int i = 0; i < 5; ++i)
  159.     {
  160.         Book A;
  161.         A.fill();
  162.         push(lst, A);
  163.  
  164.     }
  165.  
  166.     auto iter = lst.begin();
  167.     advance(iter, 2);
  168.     Book B = pop(lst, iter);
  169.     B.print();
  170.  
  171.     return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement