35657

Untitled

Jun 21st, 2023
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.63 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <string.h>
  5.  
  6. using namespace std;
  7.  
  8. struct contact {
  9.     char name[100];
  10.     char mobile_phone[20];
  11.     char home_phone[20];
  12.     char work_phone[20];
  13. };
  14.  
  15. class phone_book {
  16.  
  17. public:
  18.  
  19.     phone_book() : store(new contact* [100]), contacts_number(0), max_contacts_number(100) {}
  20.  
  21.     phone_book(int max_number) : store(new contact* [max_number]), contacts_number(0), max_contacts_number(max_number) {}
  22.  
  23.     phone_book(contact arr[], int arr_size) : store(new contact* [100]), contacts_number(0), max_contacts_number(100) {
  24.         for (int i = 0; i < arr_size; i++) {
  25.             AddContact(arr[i].name, arr[i].mobile_phone, arr[i].home_phone, arr[i].work_phone);
  26.         }
  27.     }
  28.  
  29.     phone_book(const phone_book& other) : store(new contact* [other.max_contacts_number]), contacts_number(0), max_contacts_number(other.max_contacts_number) {
  30.         for (int i = 0; i < other.contacts_number; i++) {
  31.             AddContact(other.store[i]->name, other.store[i]->mobile_phone, other.store[i]->home_phone, other.store[i]->work_phone);
  32.         }
  33.     }
  34.  
  35.     phone_book& operator=(const phone_book& other) {
  36.         if (this != &other) {
  37.             for (int i = 0; i < contacts_number; i++) {
  38.                 delete store[i];
  39.             }
  40.             delete[] store;
  41.             contacts_number = 0;
  42.             max_contacts_number = other.max_contacts_number;
  43.             store = new contact * [max_contacts_number];
  44.             for (int i = 0; i < other.contacts_number; i++) {
  45.                 AddContact(other.store[i]->name, other.store[i]->mobile_phone, other.store[i]->home_phone, other.store[i]->work_phone);
  46.             }
  47.         }
  48.         return *this;
  49.     }
  50.  
  51.     phone_book(phone_book&& other) : store(other.store), contacts_number(other.contacts_number), max_contacts_number(other.max_contacts_number) {
  52.         other.store = nullptr;
  53.         other.contacts_number = 0;
  54.         other.max_contacts_number = 0;
  55.     }
  56.  
  57.     phone_book& operator=(phone_book&& other) {
  58.         if (this != &other) {
  59.             for (int i = 0; i < contacts_number; i++) {
  60.                 delete store[i];
  61.             }
  62.             delete[] store;
  63.             store = other.store;
  64.             contacts_number = other.contacts_number;
  65.             max_contacts_number = other.max_contacts_number;
  66.             other.store = nullptr;
  67.             other.contacts_number = 0;
  68.             other.max_contacts_number = 0;
  69.         }
  70.         return *this;
  71.     }
  72.  
  73.     friend ostream& operator<<(ostream& output, const phone_book& book);
  74.  
  75.     bool operator==(const phone_book& other) {
  76.         if (this->contacts_number != other.contacts_number) {
  77.             return false;
  78.         }
  79.         for (int i = 0; i < contacts_number; i++) {
  80.             if (strcmp(other.store[i]->name, store[i]->name) || strcmp(other.store[i]->mobile_phone, store[i]->mobile_phone) || strcmp(other.store[i]->home_phone, store[i]->home_phone) || strcmp(other.store[i]->work_phone, store[i]->work_phone)) {
  81.                 return false;
  82.             }
  83.         }
  84.         return true;
  85.     }
  86.  
  87.     bool operator!=(const phone_book& other) {
  88.         return !(*this == other);
  89.     }
  90.  
  91.     void AddContact(const char name[], const char mobile_phone[], const char home_phone[], const char work_phone[]) { // не забывать ставить const, иначе не скомпилируется
  92.         if (contacts_number == max_contacts_number) {
  93.             cout << "The contact cannot be added" << endl;
  94.             return;
  95.         }
  96.         store[contacts_number] = new contact{};
  97.         strcpy(store[contacts_number]->name, name);
  98.         strcpy(store[contacts_number]->mobile_phone, mobile_phone);
  99.         strcpy(store[contacts_number]->home_phone, home_phone);
  100.         strcpy(store[contacts_number]->work_phone, work_phone);
  101.         contacts_number++;
  102.     }
  103.  
  104.     void DelContact(const char name[]) {
  105.         for (int i = 0; i < contacts_number; i++) {
  106.             if (!strcmp(store[i]->name, name)) { // возвращает 0 если строки одинаковые, поэтому используем ! (чтобы возврат нуля означал "истину")
  107.                 delete store[i];
  108.                 for (int j = i; j < contacts_number - 1; j++) {
  109.                     store[j] = store[j + 1];
  110.                 }
  111.                 store[contacts_number - 1] = nullptr;
  112.                 contacts_number--;
  113.             }
  114.         }
  115.     }
  116.  
  117.     void FindContactName(const char name[]) {
  118.         cout << "Found according to your request: " << endl;
  119.         for (int i = 0; i < contacts_number; i++) {
  120.             if (!strcmp(store[i]->name, name)) {
  121.                 cout << '\"' << store[i]->name << "\", mobile_phone: " << store[i]->mobile_phone << ", home_phone: " << store[i]->home_phone << ", work_phone: " << store[i]->work_phone << endl;
  122.             }
  123.         }
  124.         cout << endl;
  125.     }
  126.  
  127.     ~phone_book() {
  128.         for (int i = 0; i < contacts_number; i++) {
  129.             delete store[i];
  130.         }
  131.         delete[] store;
  132.     }
  133.  
  134. private:
  135.     contact** store;
  136.     int contacts_number;
  137.     int max_contacts_number;
  138.  
  139. };
  140.  
  141. ostream& operator<<(ostream& output, const phone_book& book) {
  142.     output << "The following contacts are available: " << endl;
  143.     if (book.contacts_number == 0) {
  144.         output << "No contacts";
  145.     }
  146.     for (int i = 0; i < book.contacts_number; ++i) {
  147.         output << '\"' << book.store[i]->name << "\", mobile_phone: " << book.store[i]->mobile_phone << ", home_phone: " << book.store[i]->home_phone << ", work_phone: " << book.store[i]->work_phone << endl;
  148.     }
  149.     output << endl;
  150.     return output;
  151. }
  152.  
  153.  
  154. int main() { // для проверки функционала
  155.  
  156.     phone_book my_store; // для проверки конструктора по умолчанию
  157.  
  158.     //phone_book my_store(50); // для проверки конструктора для одного параметра
  159.  
  160.     //contact some_contacts[]{ {"Ivan", "+79111111111", "111111", "121212"}, {"Zhenya", "+79111111333", "111133", "121233"}}; // для проверки конструктора для двух параметров (закомментировать первые два добавления контакта)
  161.  
  162.     //phone_book my_store(some_contacts, 2); // для проверки конструктора для двух параметров
  163.  
  164.     my_store.AddContact("Ivan", "+79111111111", "111111", "121212");
  165.     my_store.AddContact("Zhenya", "+79111111333", "111133", "121233");
  166.     my_store.AddContact("Galya", "+79115511333", "166133", "127733");
  167.     my_store.AddContact("Olya", "+79115511333", "166133", "127733");
  168.  
  169.     cout << my_store;
  170.  
  171.     my_store.DelContact("Zhenya");
  172.  
  173.     cout << my_store;
  174.  
  175.     my_store.AddContact("Zhenya", "+79111111333", "111133", "121233");
  176.  
  177.     my_store.FindContactName("Galya");
  178.  
  179.     cout << "my_store: " << endl;
  180.  
  181.     cout << my_store;
  182.  
  183.     phone_book my_store2(my_store);
  184.  
  185.     cout << "my_store2: " << endl;
  186.  
  187.     cout << my_store2;
  188.  
  189.     phone_book my_store3;
  190.  
  191.     my_store3 = my_store2;
  192.  
  193.     cout << "my_store3: " << endl;
  194.  
  195.     cout << my_store3;
  196.  
  197.     cout << boolalpha;
  198.  
  199.     cout << (my_store == my_store2) << endl;
  200.  
  201.     cout << (my_store != my_store2) << endl;
  202.  
  203.     my_store3 = move(my_store2);
  204.  
  205.     phone_book my_store4(move(my_store3));
  206.  
  207.     cout << my_store2;
  208.  
  209.     cout << my_store3;
  210.  
  211.     cout << my_store4;
  212.  
  213. }
  214.  
Advertisement
Add Comment
Please, Sign In to add comment