35657

Untitled

Jul 6th, 2024
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 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.     void add_contact(const char* name, const char* mobile_phone, const char* home_phone, const char* work_phone) {
  43.         if (contacts_number == max_contacts_number) {
  44.             contact* temp = new contact[max_contacts_number * 2];
  45.             for (int i = 0; i < contacts_number; i++) {
  46.                 temp[i] = store[i];
  47.             }
  48.             delete[] store;
  49.             store = temp;
  50.             max_contacts_number *= 2;
  51.         }
  52.         strcpy(store[contacts_number].name, name);
  53.         strcpy(store[contacts_number].mobile_phone, mobile_phone);
  54.         strcpy(store[contacts_number].home_phone, home_phone);
  55.         strcpy(store[contacts_number].work_phone, work_phone);
  56.         contacts_number++;
  57.     }
  58.  
  59.  
  60.     void del_contact(const char* name) {
  61.         for (int i = 0; i < contacts_number; i++) {
  62.             if (!strcmp(store[i].name, name)) {
  63.                 for (int j = i; j < contacts_number - 1; j++) {
  64.                     store[j] = store[j + 1];
  65.                 }
  66.                 contacts_number--;
  67.                 i--; // на случай, если подряд идут два одноименных контакта (иначе через второй перескакивает)
  68.             }
  69.         }
  70.     }
  71.  
  72.     void show_all_contacts() const {
  73.         for (int i = 0; i < contacts_number; i++) {
  74.             cout << store[i].name << ", мобильный телефон -  " << store[i].mobile_phone << ", домашний телефон - " << store[i].home_phone << ", рабочий телефон - " << store[i].work_phone << endl;
  75.         }
  76.         cout << endl;
  77.     }
  78.  
  79.     void find_contact_name(const char* name) const {
  80.         for (int i = 0; i < contacts_number; i++) {
  81.             if (!strcmp(store[i].name, name)) {
  82.                 cout << store[i].name << ", мобильный телефон -  " << store[i].mobile_phone << ", домашний телефон - " << store[i].home_phone << ", рабочий телефон - " << store[i].work_phone << endl;
  83.             }
  84.         }
  85.     }
  86.  
  87.     bool operator==(const phone_book& other) const {
  88.         if (this->contacts_number != other.contacts_number) {
  89.             return false;
  90.         }
  91.         for (int i = 0; i < contacts_number; i++) {
  92.             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)) {
  93.                 return false;
  94.             }
  95.         }
  96.         return true;
  97.     }
  98.  
  99.     bool operator!=(const phone_book& other) const {
  100.         return !(*this == other);
  101.     }
  102.  
  103.     ~phone_book() {
  104.         delete[] store;
  105.     }
  106.  
  107. private:
  108.     int contacts_number;
  109.     int max_contacts_number;
  110.     contact* store;
  111. };
  112.  
  113.  
  114. int main() {
  115.  
  116.     setlocale(LC_ALL, "ru");
  117.  
  118.     phone_book my_book;
  119.  
  120.     my_book.add_contact("Иван", "+79111111111", "111111", "121212");
  121.     my_book.add_contact("Оля", "+79111114444", "114444", "141414");
  122.     my_book.add_contact("Саша", "+79111115555", "115555", "151515");
  123.     my_book.add_contact("Женя", "+79111116666", "116666", "161616");
  124.  
  125.     my_book.show_all_contacts();
  126.  
  127.     my_book.del_contact("Женя");
  128.  
  129.     my_book.show_all_contacts();
  130.  
  131.     my_book.find_contact_name("Саша");
  132.  
  133.     cout << endl;
  134.  
  135.     const phone_book my_book2(my_book);
  136.  
  137.     my_book2.show_all_contacts();
  138.  
  139.     phone_book my_book3;
  140.  
  141.     my_book3 = my_book;
  142.  
  143.     my_book3.show_all_contacts();
  144.  
  145.     cout << (my_book == my_book2) << endl;
  146.  
  147.     cout << (my_book != my_book2) << endl;
  148.  
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment