35657

Untitled

Jun 29th, 2024
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | None | 0 0
  1.  
  2. #define _CRT_SECURE_NO_WARNINGS
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. struct contact {
  9.     char name[40];
  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() : contacts_number(0), max_contacts_number(4), store(new contact[4]) {}
  20.  
  21.     explicit phone_book(int max_number) : contacts_number(0), max_contacts_number(max_number), store(new contact[max_number]) {}
  22.  
  23.     phone_book(const phone_book& other) : contacts_number(0), max_contacts_number(other.max_contacts_number), store(new contact[other.max_contacts_number]) {
  24.         for (int i = 0; i < other.contacts_number; i++) {
  25.             add_contact(other.store[i].name, other.store[i].mobile_phone, other.store[i].home_phone, other.store[i].work_phone);
  26.         }
  27.     }
  28.  
  29.     phone_book& operator=(const phone_book& other) {
  30.         if (this != &other) {
  31.             delete[] store;
  32.             contacts_number = 0;
  33.             max_contacts_number = other.max_contacts_number;
  34.             store = new contact[max_contacts_number];
  35.             for (int i = 0; i < other.contacts_number; i++) {
  36.                 add_contact(other.store[i].name, other.store[i].mobile_phone, other.store[i].home_phone, other.store[i].work_phone);
  37.             }
  38.         }
  39.         return *this;
  40.     }
  41.  
  42.     phone_book(phone_book&& other) : contacts_number(other.contacts_number), max_contacts_number(other.max_contacts_number), store(other.store) {
  43.         other.store = nullptr;
  44.     }
  45.  
  46.     phone_book& operator=(phone_book&& other) {
  47.         if (this != &other) {
  48.             delete[] store;
  49.             contacts_number = other.contacts_number;
  50.             max_contacts_number = other.max_contacts_number;
  51.             store = other.store;
  52.             other.store = nullptr;
  53.         }
  54.         return *this;
  55.     }
  56.  
  57.     void add_contact(const char* name, const char* mobile_phone, const char* home_phone, const char* work_phone) {
  58.         if (contacts_number == max_contacts_number) {
  59.             contact* temp = new contact[max_contacts_number * 2];
  60.             for (int i = 0; i < contacts_number; i++) {
  61.                 temp[i] = store[i];
  62.             }
  63.             delete[] store;
  64.             store = temp;
  65.             max_contacts_number *= 2;
  66.         }
  67.         strcpy(store[contacts_number].name, name);
  68.         strcpy(store[contacts_number].mobile_phone, mobile_phone);
  69.         strcpy(store[contacts_number].home_phone, home_phone);
  70.         strcpy(store[contacts_number].work_phone, work_phone);
  71.         contacts_number++;
  72.     }
  73.  
  74.  
  75.     void del_contact(const char* name) {
  76.         for (int i = 0; i < contacts_number; i++) {
  77.             if (!strcmp(store[i].name, name)) {
  78.                 for (int j = i; j < contacts_number - 1; j++) {
  79.                     store[j] = store[j + 1];
  80.                 }
  81.                 contacts_number--;
  82.                 i--; // на случай, если подряд идут два одноименных контакта (иначе через второй перескакивает)
  83.             }
  84.         }
  85.     }
  86.  
  87.     void show_all_contacts() const {
  88.         for (int i = 0; i < contacts_number; i++) {
  89.             cout << store[i].name << ", мобильный телефон -  " << store[i].mobile_phone << ", домашний телефон - " << store[i].home_phone << ", рабочий телефон - " << store[i].work_phone << endl;
  90.         }
  91.         cout << endl;
  92.     }
  93.  
  94.     void find_contact_name(const char* name) const {
  95.         for (int i = 0; i < contacts_number; i++) {
  96.             if (!strcmp(store[i].name, name)) {
  97.                 cout << store[i].name << ", мобильный телефон -  " << store[i].mobile_phone << ", домашний телефон - " << store[i].home_phone << ", рабочий телефон - " << store[i].work_phone << endl;
  98.             }
  99.         }
  100.     }
  101.  
  102.     bool operator==(const phone_book& other) const {
  103.         if (this->contacts_number != other.contacts_number) {
  104.             return false;
  105.         }
  106.         for (int i = 0; i < contacts_number; i++) {
  107.             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)) {
  108.                 return false;
  109.             }
  110.         }
  111.         return true;
  112.     }
  113.  
  114.     bool operator!=(const phone_book& other) const {
  115.         return !(*this == other);
  116.     }
  117.  
  118.     ~phone_book() {
  119.         delete[] store;
  120.     }
  121.  
  122. private:
  123.     int contacts_number;
  124.     int max_contacts_number;
  125.     contact* store;
  126. };
  127.  
  128.  
  129. int main() {
  130.  
  131.     setlocale(LC_ALL, "ru");
  132.  
  133.     phone_book my_book;
  134.  
  135.     my_book.add_contact("Иван", "+79111111111", "111111", "121212");
  136.     my_book.add_contact("Оля", "+79111114444", "114444", "141414");
  137.     my_book.add_contact("Саша", "+79111115555", "115555", "151515");
  138.     my_book.add_contact("Женя", "+79111116666", "116666", "161616");
  139.  
  140.     my_book.show_all_contacts();
  141.  
  142.     my_book.del_contact("Женя");
  143.  
  144.     my_book.show_all_contacts();
  145.  
  146.     my_book.find_contact_name("Саша");
  147.  
  148.     cout << endl;
  149.  
  150.     const phone_book my_book2(my_book);
  151.  
  152.     my_book2.show_all_contacts();
  153.  
  154.     phone_book my_book3;
  155.  
  156.     my_book3 = my_book;
  157.  
  158.     my_book3.show_all_contacts();
  159.  
  160.     cout << (my_book == my_book2) << endl;
  161.  
  162.     cout << (my_book != my_book2) << endl;
  163.  
  164. }
  165.  
Advertisement
Add Comment
Please, Sign In to add comment