35657

Untitled

Jun 10th, 2023
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.19 KB | None | 0 0
  1.  
  2. #define _CRT_SECURE_NO_WARNINGS
  3.  
  4. #include <iostream>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. struct film {
  10.     char name[100];
  11.     char producer[100];
  12.     char genre[20];
  13.     int rating;
  14. };
  15.  
  16. class video_store {
  17.  
  18. public:
  19.  
  20.     video_store() : store(new film* [100]), films_number(0), max_films_number(100) {}
  21.  
  22.     video_store(int max_number) : store(new film* [max_number]), films_number(0), max_films_number(max_number) {}
  23.  
  24.     video_store(film arr[], int arr_size) : store(new film* [100]), films_number(0), max_films_number(100) {
  25.         for (int i = 0; i < arr_size; i++) {
  26.             AddMovie(arr[i].name, arr[i].producer, arr[i].genre, arr[i].rating);
  27.         }
  28.     }
  29.  
  30.     video_store(const video_store& other) : store(new film* [other.max_films_number]), films_number(0), max_films_number(other.max_films_number) {
  31.         for (int i = 0; i < other.films_number; i++) {
  32.             AddMovie(other.store[i]->name, other.store[i]->producer, other.store[i]->genre, other.store[i]->rating);
  33.         }
  34.     }
  35.  
  36.     video_store& operator=(const video_store& other) {
  37.         if (this != &other) {
  38.             for (int i = 0; i < films_number; i++) {
  39.                 delete store[i];
  40.             }
  41.             delete[] store;
  42.             store = new film * [other.max_films_number];
  43.             films_number = 0;
  44.             max_films_number = other.max_films_number;
  45.             for (int i = 0; i < other.films_number; i++) {
  46.                 AddMovie(other.store[i]->name, other.store[i]->producer, other.store[i]->genre, other.store[i]->rating);
  47.             }
  48.         }
  49.         return *this;
  50.     }
  51.  
  52.     video_store(video_store&& other) : store(other.store), films_number(other.films_number), max_films_number(other.max_films_number) {
  53.         other.store = nullptr;
  54.         other.films_number = 0;
  55.         other.max_films_number = 0;
  56.     }
  57.  
  58.     video_store& operator=(video_store&& other) {
  59.         if (this != &other) {
  60.             for (int i = 0; i < films_number; i++) {
  61.                 delete store[i];
  62.             }
  63.             delete[] store;
  64.             store = other.store;
  65.             films_number = other.films_number;
  66.             max_films_number = other.max_films_number;
  67.             other.store = nullptr;
  68.             other.films_number = 0;
  69.             other.max_films_number = 0;
  70.         }
  71.         return *this;
  72.     }
  73.  
  74.     void ShowAllMovies() {
  75.         if (films_number == 0) {
  76.             cout << "No movies" << endl;
  77.         }
  78.         for (int i = 0; i < films_number; i++) {
  79.             cout << store[i]->name << ", producer: " << store[i]->producer << ", genre: " << store[i]->genre << ", rating: " << store[i]->rating << endl;
  80.         }
  81.         cout << endl;
  82.     }
  83.  
  84.     void AddMovie(const char name[], const char producer[], const char genre[], const int rating) {
  85.         if (films_number == max_films_number) {
  86.             cout << "The movie cannot be added" << endl;
  87.             return;
  88.         }
  89.         store[films_number] = new film{};
  90.         strcpy(store[films_number]->name, name);
  91.         strcpy(store[films_number]->producer, producer);
  92.         strcpy(store[films_number]->genre, genre);
  93.         store[films_number]->rating = rating;
  94.         films_number++;
  95.         //films_number = films_number + 1;
  96.     }
  97.  
  98.     void DelMovie(const char name[]) {
  99.         for (int i = 0; i < films_number; i++) {
  100.             if (!strcmp(store[i]->name, name)) {
  101.                 delete store[i];
  102.                 for (int j = i; j < films_number - 1; j++) {
  103.                     store[j] = store[j + 1];
  104.                 }
  105.                 films_number--;
  106.             }
  107.         }
  108.     }
  109.  
  110.     void FindMovieName(const char name[]) {
  111.         for (int i = 0; i < films_number; i++) {
  112.             if (!strcmp(store[i]->name, name)) {
  113.                 cout << store[i]->name << ", producer: " << store[i]->producer << ", genre: " << store[i]->genre << ", rating: " << store[i]->rating << endl;
  114.             }
  115.         }
  116.         cout << endl;
  117.     }
  118.  
  119.     void FindMovieProducer(const char producer[]) {
  120.         for (int i = 0; i < films_number; i++) {
  121.             if (!strcmp(store[i]->producer, producer)) {
  122.                 cout << store[i]->name << ", producer: " << store[i]->producer << ", genre: " << store[i]->genre << ", rating: " << store[i]->rating << endl;
  123.             }
  124.         }
  125.         cout << endl;
  126.     }
  127.  
  128.     void FindMovieGenre(const char genre[]) {
  129.         for (int i = 0; i < films_number; i++) {
  130.             if (!strcmp(store[i]->genre, genre)) {
  131.                 cout << store[i]->name << ", producer: " << store[i]->producer << ", genre: " << store[i]->genre << ", rating: " << store[i]->rating << endl;
  132.             }
  133.         }
  134.         cout << endl;
  135.     }
  136.  
  137.     void FindMovieMostPopular(const char genre[]) {
  138.         int index = -1;
  139.         int rating = 0;
  140.         for (int i = 0; i < films_number; i++) {
  141.             if (!strcmp(store[i]->genre, genre)) {
  142.                 if (store[i]->rating > rating) {
  143.                     index = i;
  144.                     rating = store[i]->rating;
  145.                 }
  146.             }
  147.         }
  148.         if (index != -1) {
  149.             cout << store[index]->name << ", producer: " << store[index]->producer << ", genre: " << store[index]->genre << ", rating: " << store[index]->rating << endl;
  150.         }
  151.         cout << endl;
  152.     }
  153.  
  154.     ~video_store() {
  155.         for (int i = 0; i < films_number; i++) {
  156.             delete store[i];
  157.         }
  158.         delete[] store;
  159.     }
  160.  
  161.     friend bool operator==(const video_store& left, const video_store& right);
  162.  
  163. private:
  164.     film** store;
  165.     int films_number;
  166.     int max_films_number;
  167. };
  168.  
  169. bool operator==(const video_store& left, const video_store& right) {
  170.     if (left.films_number != right.films_number) {
  171.         return false;
  172.     }
  173.     for (int i = 0; i < left.films_number; i++) {
  174.         if (strcmp(left.store[i]->name, right.store[i]->name) || strcmp(left.store[i]->producer, right.store[i]->producer) || strcmp(left.store[i]->genre, right.store[i]->genre) || left.store[i]->rating != right.store[i]->rating) {
  175.             return false;
  176.         }
  177.     }
  178.     return true;
  179. }
  180.  
  181. bool operator!=(const video_store& left, const video_store& right) {
  182.     return !(left == right);
  183. }
  184.  
  185.  
  186. int main() { // для проверки функционала
  187.  
  188.     video_store my_store; // для проверки конструктора по умолчанию
  189.  
  190.     //video_store my_store(50); // для проверки конструктора для одного параметра
  191.  
  192.     //film some_films[]{ {"Pirates of the Caribbean", "Gore Verbinski", "fantasy", 5}, {"Harry Potter", "Chris Columbus", "fantasy", 4}, {"Diamond Hand", "Leonid Gaidai", "comedy", 5} }; // для проверки конструктора для двух параметров (закомментировать первые три добавления фильма)
  193.  
  194.     //video_store my_store(some_films, 3); // для проверки конструктора для двух параметров
  195.  
  196.     my_store.AddMovie("Pirates of the Caribbean", "Gore Verbinski", "fantasy", 5);
  197.     my_store.AddMovie("Harry Potter", "Chris Columbus", "fantasy", 4);
  198.     my_store.AddMovie("Diamond Hand", "Leonid Gaidai", "comedy", 5);
  199.     my_store.AddMovie("Ivan Vasilyevich is changing his profession", "Leonid Gaidai", "comedy", 4);
  200.     my_store.AddMovie("Sherlock Holmes", "Guy Ritchie", "detective", 5);
  201.     my_store.AddMovie("Lock, Stock and Two Smoking Barrels", "Guy Ritchie", "comedy", 5);
  202.  
  203.     my_store.ShowAllMovies();
  204.  
  205.     my_store.DelMovie("Pirates of the Caribbean");
  206.  
  207.     my_store.ShowAllMovies();
  208.  
  209.     my_store.AddMovie("Pirates of the Caribbean", "Gore Verbinski", "fantasy", 5);
  210.  
  211.     my_store.FindMovieName("Diamond Hand");
  212.  
  213.     my_store.FindMovieProducer("Guy Ritchie");
  214.  
  215.     my_store.FindMovieGenre("fantasy");
  216.  
  217.     my_store.FindMovieMostPopular("fantasy");
  218.  
  219.     video_store my_store2(200);
  220.    
  221.     my_store2 = move(my_store);
  222.  
  223.     cout << "my_store: " << endl;
  224.  
  225.     my_store.ShowAllMovies();
  226.  
  227.     cout << "my_store2: " << endl;
  228.  
  229.     my_store2.ShowAllMovies();
  230.  
  231.     cout << boolalpha;
  232.  
  233.     cout << (my_store == my_store2) << endl;
  234.  
  235.     cout << (my_store != my_store2) << endl;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment