Advertisement
35657

Untitled

Jun 7th, 2023
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.85 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.     friend ostream& operator<<(ostream& output, const phone_book& book);
  52.  
  53.     bool operator==(const phone_book& other) {
  54.         if (this->contacts_number != other.contacts_number) {
  55.             return false;
  56.         }
  57.         for (int i = 0; i < contacts_number; i++) {
  58.             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)) {
  59.                 return false;
  60.             }
  61.         }
  62.         return true;
  63.     }
  64.  
  65.     bool operator!=(const phone_book& other) {
  66.         return !(*this == other);
  67.     }
  68.  
  69.     void AddContact(const char name[], const char mobile_phone[], const char home_phone[], const char work_phone[]) { // не забывать ставить const, иначе не скомпилируется
  70.         if (contacts_number == max_contacts_number) {
  71.             cout << "The contact cannot be added" << endl;
  72.             return;
  73.         }
  74.         store[contacts_number] = new contact{};
  75.         strcpy(store[contacts_number]->name, name);
  76.         strcpy(store[contacts_number]->mobile_phone, mobile_phone);
  77.         strcpy(store[contacts_number]->home_phone, home_phone);
  78.         strcpy(store[contacts_number]->work_phone, work_phone);
  79.         contacts_number++;
  80.     }
  81.  
  82.     void DelContact(const char name[]) {
  83.         for (int i = 0; i < contacts_number; i++) {
  84.             if (!strcmp(store[i]->name, name)) { // возвращает 0 если строки одинаковые, поэтому используем ! (чтобы возврат нуля означал "истину")
  85.                 delete store[i];
  86.                 for (int j = i; j < contacts_number - 1; j++) {
  87.                     store[j] = store[j + 1];
  88.                 }
  89.                 store[contacts_number - 1] = nullptr;
  90.                 contacts_number--;
  91.             }
  92.         }
  93.     }
  94.  
  95.     void FindContactName(const char name[]) {
  96.         cout << "Found according to your request: " << endl;
  97.         for (int i = 0; i < contacts_number; i++) {
  98.             if (!strcmp(store[i]->name, name)) {
  99.                 cout << '\"' << store[i]->name << "\", mobile_phone: " << store[i]->mobile_phone << ", home_phone: " << store[i]->home_phone << ", work_phone: " << store[i]->work_phone << endl;
  100.             }
  101.         }
  102.         cout << endl;
  103.     }
  104.  
  105.     ~phone_book() {
  106.         for (int i = 0; i < contacts_number; i++) {
  107.             delete store[i];
  108.         }
  109.         delete[] store;
  110.     }
  111.  
  112. private:
  113.     contact** store;
  114.     int contacts_number;
  115.     int max_contacts_number;
  116.  
  117. };
  118.  
  119. ostream& operator<<(ostream& output, const phone_book& book) {
  120.     output << "The following contacts are available: " << endl;
  121.     if (book.contacts_number == 0) {
  122.         output << "No contacts";
  123.     }
  124.     for (int i = 0; i < book.contacts_number; ++i) {
  125.         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;
  126.     }
  127.     output << endl;
  128.     return output;
  129. }
  130.  
  131.  
  132. int main() { // для проверки функционала
  133.  
  134.     phone_book my_store; // для проверки конструктора по умолчанию
  135.  
  136.     //phone_book my_store(50); // для проверки конструктора для одного параметра
  137.  
  138.     //contact some_contacts[]{ {"Ivan", "+79111111111", "111111", "121212"}, {"Zhenya", "+79111111333", "111133", "121233"}}; // для проверки конструктора для двух параметров (закомментировать первые два добавления контакта)
  139.  
  140.     //phone_book my_store(some_contacts, 2); // для проверки конструктора для двух параметров
  141.  
  142.     my_store.AddContact("Ivan", "+79111111111", "111111", "121212");
  143.     my_store.AddContact("Zhenya", "+79111111333", "111133", "121233");
  144.     my_store.AddContact("Galya", "+79115511333", "166133", "127733");
  145.     my_store.AddContact("Olya", "+79115511333", "166133", "127733");
  146.  
  147.     cout << my_store;
  148.  
  149.     my_store.DelContact("Zhenya");
  150.  
  151.     cout << my_store;
  152.  
  153.     my_store.AddContact("Zhenya", "+79111111333", "111133", "121233");
  154.  
  155.     my_store.FindContactName("Galya");
  156.  
  157.     cout << "my_store: " << endl;
  158.  
  159.     cout << my_store;
  160.  
  161.     phone_book my_store2(my_store);
  162.  
  163.     cout << "my_store2: " << endl;
  164.  
  165.     cout << my_store2;
  166.  
  167.     phone_book my_store3;
  168.  
  169.     my_store3 = my_store2;
  170.  
  171.     cout << "my_store3: " << endl;
  172.  
  173.     cout << my_store3;
  174.  
  175.     cout << boolalpha;
  176.  
  177.     cout << (my_store == my_store2) << endl;
  178.  
  179.     cout << (my_store != my_store2) << endl;
  180.  
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement