Advertisement
igoryanchik

2.1-2.2

Sep 14th, 2023 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <set>
  4. #include <string>
  5. #include <ctime>
  6. #include <random>
  7. using namespace std;
  8.  
  9. class Book
  10. {
  11. private:
  12.     string first_name_a;
  13.     string second_name_a;
  14.     string titel;
  15.     string publisher;
  16.     int count;
  17.     int year;
  18.     int paper;
  19.     int electronic;
  20.     int audio;
  21.  
  22. public:
  23.  
  24.     Book(string const f = " ", string const s = " ", string const t = " ",
  25.         string const p = " ", int c = 0, int y = 0, int ta = 0, int  tp = 0, int te = 0)
  26.     {
  27.         first_name_a = f;
  28.         second_name_a = s;
  29.         titel = t;
  30.         publisher = p;
  31.         count = c;
  32.         year = y;
  33.         paper = ta;
  34.         electronic = tp;
  35.         audio = te;
  36.     }
  37.  
  38.     bool operator < (const Book& B) const
  39.     {
  40.         return ((count < B.count) + ((count != B.count) * (year < B.year)) +
  41.             ((count != B.count) * (year != B.year) * (titel < B.titel)));
  42.     }
  43.  
  44.     bool operator ==(const Book& B) const
  45.     {
  46.         return !(*this < B) && !(B < *this);
  47.     }
  48.  
  49.     bool operator >(int P) const
  50.     {
  51.         return (count > P);
  52.     }
  53.  
  54.     int count_() { return count; }
  55.     int year_() { return year; }
  56.     string titel_() { return titel; }
  57.  
  58.     void print() const
  59.     {
  60.         cout << "\nName authot: " << first_name_a << "\nSurname author: " << second_name_a <<
  61.             "\nTitel of book: " << titel << "\nCount books: " << count << "\nPublisher: " << publisher <<
  62.             "\nYear of publication: " << year << "\nNumber of paper books: " << paper << "\nNumber of electronic books:" <<
  63.             electronic << "\nNumber of audio books:" << audio << '\n';
  64.     }
  65.  
  66.     ~Book() = default;
  67.  
  68.     friend void fill(Book& V);
  69. };
  70.  
  71. void fill(Book& V)
  72. {
  73.     srand(time(NULL));
  74.  
  75.     string names[5] = { "Alice", "Bob", "Carla", "David", "Eva" };
  76.     string surnames[5] = { "Smith", "Johnson", "Brown", "Davis", "Wilson" };
  77.     string publishers[5] = { "Penguin Random House", "HarperCollins", "Simon & Schuster", "Macmillan Publishers", "Hachette Livre" };
  78.     string books[5] = { "To Kill a Mockingbird", "1984", "The Great Gatsby", "Pride and Prejudice", "The Catcher in the Rye" };
  79.  
  80.     int i1 = rand() % 5, i2 = rand() % 5, i3 = rand() % 5, i4 = rand() % 5,
  81.         i5 = rand() % 5000, i6 = rand() % 2023, i7 = rand() % 1000, i8 = rand() % 1000, i9 = rand() % 1000;
  82.  
  83.     V.first_name_a = names[i1];
  84.     V.second_name_a = surnames[i2];
  85.     V.titel = books[i3];
  86.     V.publisher = publishers[i4];
  87.     V.count = i5;
  88.     V.year = i6;
  89.     V.paper = i7;
  90.     V.electronic = i8;
  91.     V.audio = i9;
  92. }
  93.  
  94.  
  95. bool book_count(Book B, int P)
  96. {
  97.     return (B > P);
  98. }
  99.  
  100. template<class K, class V>
  101. K find_key(const map<K, V>& m, V const& val)
  102. {
  103.     auto it = m.begin();
  104.     for (it; it != m.end(); ++it)
  105.         if (it->second == val)
  106.             break;
  107.     return it->first;
  108. }
  109.  
  110. template <class K, class V>
  111. V find_val(const map<K, V>& m, K const& key)
  112. {
  113.     auto it = m.begin();
  114.     for (it; it != m.end(); ++it)
  115.         if (it->first == key)
  116.             break;
  117.     return it->second;
  118. }
  119.  
  120. template<class K, class V, class T>
  121. map<K, V> filter(const map<K, V>& m, bool(*comp)(V,T), T P)
  122. {
  123.     if (comp == nullptr) cout << "nullptr";
  124.  
  125.     map<K, V> ans;
  126.     for (auto it = m.begin(); it != m.end(); ++it)
  127.         if (comp(it->second,P))
  128.             ans.emplace(*it);
  129.  
  130.     return ans;
  131. }
  132.  
  133. template<class K, class V>
  134. set<V> all_values(const map<K, V>& m)
  135. {
  136.     set<V> s;
  137.  
  138.     for (auto it = m.begin(); it != m.end(); ++it)
  139.         s.emplace(it->second);
  140.  
  141.     return s;
  142. }
  143.  
  144.  
  145. template<class K, class V>
  146. void map_print(const map<K,V>& m)
  147. {
  148.     for (auto it = m.begin(); it != m.end(); ++it)
  149.         cout << "Key: " << it->first << ", " << "Value: " << it->second << '\n';
  150. }
  151.  
  152.  
  153. ///////////////////////////////////////////////////////////////////////////
  154.  
  155. template<class K, class V>
  156. K find_key(const multimap<K, V>& m, V const& val)
  157. {
  158.     auto it = m.begin();
  159.     for (it; it != m.end(); ++it)
  160.         if (it->second == val)
  161.             break;
  162.     return it->first;
  163. }
  164.  
  165. template <class K, class V>
  166. V find_val(const multimap<K, V>& m, K const& key)
  167. {
  168.     auto it = m.begin();
  169.     for (it; it != m.end(); ++it)
  170.         if (it->first == key)
  171.             break;
  172.     return it->second;
  173. }
  174.  
  175. template<class K, class V, class T>
  176. multimap<K, V> filter(const multimap<K, V>& m, bool(*comp)(V, T), T P)
  177. {
  178.     if (comp == nullptr) cout << "nullptr";
  179.  
  180.     multimap<K, V> ans;
  181.     for (auto it = m.begin(); it != m.end(); ++it)
  182.         if (comp(it->second, P))
  183.             ans.emplace(*it);
  184.  
  185.     return ans;
  186. }
  187.  
  188. template<class K, class V>
  189. set<V> all_values(const multimap<K, V>& m)
  190. {
  191.     set<V> s;
  192.  
  193.     for (auto it = m.begin(); it != m.end(); ++it)
  194.         s.emplace(it->second);
  195.  
  196.     return s;
  197. }
  198.  
  199.  
  200. template<class K, class V>
  201. void multimap_print(const multimap<K, V>& m)
  202. {
  203.     for (auto it = m.begin(); it != m.end(); ++it)
  204.         cout << "Key: " << it->first << ", " << "Value: " << it->second << '\n';
  205. }
  206.  
  207. int main()
  208. {
  209.     map<string, int> marks;
  210.  
  211.     marks["Petrov"] = 5;
  212.     marks["Ivanov"] = 4;
  213.     marks["Sidorov"] = 5;
  214.     marks["Nikolaev"] = 3;
  215.     marks["Abramov"] = 4;
  216.  
  217.     map_print(marks);
  218.  
  219.     auto a = find_key(marks, 5);
  220.     cout << '\n' << a << '\n';
  221.  
  222.     set<int> s = all_values(marks);
  223.  
  224.     for (auto it = s.begin(); it != s.end(); ++it)
  225.         cout << *it << " ";
  226.     cout << '\n';
  227.  
  228.     map<int, Book> books;
  229.  
  230.     for (int i = 0; i < 5; ++i)
  231.     {
  232.         Book A;
  233.         fill(A);
  234.         books[i] = A;
  235.     }
  236.     bool(*ptr)(Book, int) = &book_count;
  237.     map<int, Book> ans = filter(books, ptr, 2000);
  238.  
  239.     cout << books.size() << " " << ans.size();
  240.  
  241.     return 0;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement