35657

Untitled

Oct 15th, 2023
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1.  
  2. // магазин (вариант на стеке)
  3.  
  4. #define _CRT_SECURE_NO_WARNINGS
  5.  
  6. #include <iostream>
  7. #include <string.h>
  8.  
  9. using namespace std;
  10.  
  11. struct film {
  12.     char name[100]{};
  13.     char producer[100]{};
  14.     char genre[20]{};
  15.     int rating = 0;
  16. };
  17.  
  18. struct video_store {
  19.     film store[100]{};
  20.     int films_number = 0;
  21. };
  22.  
  23. void ShowAllMovies(const video_store& some_store) {
  24.     cout << "The following films are available: " << endl;
  25.     if (some_store.films_number == 0) {
  26.         cout << "No movies";
  27.     }
  28.     for (int i = 0; i < some_store.films_number; ++i) {
  29.         cout << '\"' << some_store.store[i].name << "\", producer: " << some_store.store[i].producer << ", " << some_store.store[i].genre << ", rating " << some_store.store[i].rating << endl;
  30.     }
  31.     cout << endl;
  32. }
  33.  
  34. void AddMovie(video_store& some_store, const char name[], const char producer[], const char genre[], const int rating) { // не забывать ставить const, иначе не скомпилируется
  35.     if (some_store.films_number == 100) {
  36.         cout << "The movie cannot be added" << endl;
  37.         return;
  38.     }
  39.     strcpy(some_store.store[some_store.films_number].name, name);
  40.     strcpy(some_store.store[some_store.films_number].producer, producer);
  41.     strcpy(some_store.store[some_store.films_number].genre, genre);
  42.     some_store.store[some_store.films_number].rating = rating;
  43.     some_store.films_number++;
  44. }
  45.  
  46. void DelMovie(video_store& some_store, const char name[]) {
  47.     for (int i = 0; i < some_store.films_number; i++) {
  48.         if (!strcmp(some_store.store[i].name, name)) { // возвращает 0 если строки одинаковые, поэтому используем ! (чтобы возврат нуля означал "истину")
  49.             for (int j = i; j < some_store.films_number - 1; j++) {
  50.                 some_store.store[j] = some_store.store[j + 1];
  51.             }
  52.             some_store.store[some_store.films_number - 1] = {};
  53.             some_store.films_number--;
  54.         }
  55.     }
  56. }
  57.  
  58.  
  59. int main() {
  60.     video_store my_store;
  61.  
  62.     AddMovie(my_store, "Pirates of the Caribbean", "Gore Verbinski", "fantasy", 5);
  63.     AddMovie(my_store, "Harry Potter", "Chris Columbus", "fantasy", 4);
  64.     AddMovie(my_store, "Diamond Hand", "Leonid Gaidai", "comedy", 5);
  65.     AddMovie(my_store, "Ivan Vasilyevich is changing his profession", "Leonid Gaidai", "comedy", 4);
  66.     AddMovie(my_store, "Sherlock Holmes", "Guy Ritchie", "detective", 5);
  67.     AddMovie(my_store, "Lock, Stock and Two Smoking Barrels", "Guy Ritchie", "comedy", 5);
  68.  
  69.     ShowAllMovies(my_store);
  70.  
  71.     DelMovie(my_store, "Pirates of the Caribbean");
  72.  
  73.     ShowAllMovies(my_store);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment