35657

Untitled

Jun 2nd, 2023
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | None | 0 0
  1.  
  2. #define _CRT_SECURE_NO_WARNINGS
  3.  
  4. #include <iostream>
  5. #include <string.h>
  6.  
  7. using namespace std;
  8.  
  9. struct contact {
  10.     char name[100];
  11.     char mobile_phone[20];
  12.     char home_phone[20];
  13.     char work_phone[20];
  14. };
  15.  
  16. class phone_book {
  17.  
  18. public:
  19.  
  20.     phone_book() : store(new contact* [100]), contacts_number(0), max_contacts_number(100) {}
  21.  
  22.     phone_book(int max_number) : store(new contact* [max_number]), contacts_number(0), max_contacts_number(max_number) {}
  23.  
  24.     phone_book(contact arr[], int arr_size) : store(new contact* [100]), contacts_number(0), max_contacts_number(100) {
  25.         for (int i = 0; i < arr_size; i++) {
  26.             AddContact(arr[i].name, arr[i].mobile_phone, arr[i].home_phone, arr[i].work_phone);
  27.         }
  28.     }
  29.  
  30.     phone_book(const phone_book& other) : store(new contact* [other.max_contacts_number]), contacts_number(0), max_contacts_number(other.max_contacts_number) {
  31.         for (int i = 0; i < other.contacts_number; i++) {
  32.             AddContact(other.store[i]->name, other.store[i]->mobile_phone, other.store[i]->home_phone, other.store[i]->work_phone);
  33.         }
  34.     }
  35.  
  36.     void ShowAllContacts() {
  37.         cout << "The following contacts are available: " << endl;
  38.         if (contacts_number == 0) {
  39.             cout << "No contacts";
  40.         }
  41.         for (int i = 0; i < contacts_number; ++i) {
  42.             cout << '\"' << store[i]->name << "\", mobile_phone: " << store[i]->mobile_phone << ", home_phone: " << store[i]->home_phone << ", work_phone: " << store[i]->work_phone << endl;
  43.         }
  44.         cout << endl;
  45.     }
  46.  
  47.     void AddContact(const char name[], const char mobile_phone[], const char home_phone[], const char work_phone[]) { // не забывать ставить const, иначе не скомпилируется
  48.         if (contacts_number == max_contacts_number) {
  49.             cout << "The contact cannot be added" << endl;
  50.             return;
  51.         }
  52.         store[contacts_number] = new contact{};
  53.         strcpy(store[contacts_number]->name, name);
  54.         strcpy(store[contacts_number]->mobile_phone, mobile_phone);
  55.         strcpy(store[contacts_number]->home_phone, home_phone);
  56.         strcpy(store[contacts_number]->work_phone, work_phone);
  57.         contacts_number++;
  58.     }
  59.  
  60.     void DelContact(const char name[]) {
  61.         for (int i = 0; i < contacts_number; i++) {
  62.             if (!strcmp(store[i]->name, name)) { // возвращает 0 если строки одинаковые, поэтому используем ! (чтобы возврат нуля означал "истину")
  63.                 delete store[i];
  64.                 for (int j = i; j < contacts_number - 1; j++) {
  65.                     store[j] = store[j + 1];
  66.                 }
  67.                 store[contacts_number - 1] = nullptr;
  68.                 contacts_number--;
  69.             }
  70.         }
  71.     }
  72.  
  73.     void FindContactName(const char name[]) {
  74.         cout << "Found according to your request: " << endl;
  75.         for (int i = 0; i < contacts_number; i++) {
  76.             if (!strcmp(store[i]->name, name)) {
  77.                 cout << '\"' << store[i]->name << "\", mobile_phone: " << store[i]->mobile_phone << ", home_phone: " << store[i]->home_phone << ", work_phone: " << store[i]->work_phone << endl;
  78.             }
  79.         }
  80.         cout << endl;
  81.     }
  82.  
  83.     ~phone_book() {
  84.         for (int i = 0; i < contacts_number; i++) {
  85.             delete store[i];
  86.         }
  87.         delete[] store;
  88.     }
  89.  
  90. private:
  91.     contact** store;
  92.     int contacts_number;
  93.     int max_contacts_number;
  94.  
  95. };
  96.  
  97.  
  98. int main() { // для проверки функционала
  99.  
  100.     phone_book my_store; // для проверки конструктора по умолчанию
  101.  
  102.     //phone_book my_store(50); // для проверки конструктора для одного параметра
  103.  
  104.     //contact some_contacts[]{ {"Ivan", "+79111111111", "111111", "121212"}, {"Zhenya", "+79111111333", "111133", "121233"}}; // для проверки конструктора для двух параметров (закомментировать первые два добавления контакта)
  105.  
  106.     //phone_book my_store(some_contacts, 2); // для проверки конструктора для двух параметров
  107.  
  108.     my_store.AddContact("Ivan", "+79111111111", "111111", "121212");
  109.     my_store.AddContact("Zhenya", "+79111111333", "111133", "121233");
  110.     my_store.AddContact("Galya", "+79115511333", "166133", "127733");
  111.     my_store.AddContact("Olya", "+79115511333", "166133", "127733");
  112.  
  113.     my_store.ShowAllContacts();
  114.  
  115.     my_store.DelContact("Zhenya");
  116.  
  117.     my_store.ShowAllContacts();
  118.  
  119.     my_store.AddContact("Zhenya", "+79111111333", "111133", "121233");
  120.  
  121.     my_store.FindContactName("Galya");
  122.  
  123.     cout << "my_store: " << endl;
  124.  
  125.     my_store.ShowAllContacts();
  126.  
  127.     phone_book my_store2(my_store);
  128.  
  129.     cout << "my_store2: " << endl;
  130.  
  131.     my_store2.ShowAllContacts();
  132.  
  133.  
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment