35657

Untitled

Jun 2nd, 2023
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 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) {};
  23.  
  24.     phone_book(contact arr[], int arr_size) {};
  25.  
  26.     void ShowAllContacts() {
  27.         cout << "The following contacts are available: " << endl;
  28.         if (contacts_number == 0) {
  29.             cout << "No contacts";
  30.         }
  31.         for (int i = 0; i < contacts_number; ++i) {
  32.             cout << '\"' << store[i]->name << "\", mobile_phone: " << store[i]->mobile_phone << ", home_phone: " << store[i]->home_phone << ", work_phone: " << store[i]->work_phone << endl;
  33.         }
  34.         cout << endl;
  35.     }
  36.  
  37.     void AddContact(const char name[], const char mobile_phone[], const char home_phone[], const char work_phone[]) { // не забывать ставить const, иначе не скомпилируется
  38.         if (contacts_number == max_contacts_number) {
  39.             cout << "The contact cannot be added" << endl;
  40.             return;
  41.         }
  42.         store[contacts_number] = new contact{};
  43.         strcpy(store[contacts_number]->name, name);
  44.         strcpy(store[contacts_number]->mobile_phone, mobile_phone);
  45.         strcpy(store[contacts_number]->home_phone, home_phone);
  46.         strcpy(store[contacts_number]->work_phone, work_phone);
  47.         contacts_number++;
  48.     }
  49.  
  50.     void DelContact(const char name[]) {
  51.         for (int i = 0; i < contacts_number; i++) {
  52.             if (!strcmp(store[i]->name, name)) { // возвращает 0 если строки одинаковые, поэтому используем ! (чтобы возврат нуля означал "истину")
  53.                 delete store[i];
  54.                 for (int j = i; j < contacts_number - 1; j++) {
  55.                     store[j] = store[j + 1];
  56.                 }
  57.                 store[contacts_number - 1] = nullptr;
  58.                 contacts_number--;
  59.             }
  60.         }
  61.     }
  62.  
  63.     void FindContactName(const char name[]) {
  64.         cout << "Found according to your request: " << endl;
  65.         for (int i = 0; i < contacts_number; i++) {
  66.             if (!strcmp(store[i]->name, name)) {
  67.                 cout << '\"' << store[i]->name << "\", mobile_phone: " << store[i]->mobile_phone << ", home_phone: " << store[i]->home_phone << ", work_phone: " << store[i]->work_phone << endl;
  68.             }
  69.         }
  70.         cout << endl;
  71.     }
  72.  
  73.     ~phone_book() {}
  74.  
  75. private:
  76.     contact** store;
  77.     int contacts_number;
  78.     int max_contacts_number;
  79.  
  80. };
  81.  
  82.  
  83. int main() { // для проверки функционала
  84.  
  85.     phone_book my_store; // для проверки конструктора по умолчанию
  86.  
  87.     //phone_book my_store(50); // для проверки конструктора для одного параметра
  88.  
  89.     //contact some_contacts[]{ {"Ivan", "+79111111111", "111111", "121212"}, {"Zhenya", "+79111111333", "111133", "121233"}}; // для проверки конструктора для двух параметров (закомментировать первые два добавления контакта)
  90.  
  91.     //phone_book my_store(some_contacts, 2); // для проверки конструктора для двух параметров
  92.  
  93.     my_store.AddContact("Ivan", "+79111111111", "111111", "121212");
  94.     my_store.AddContact("Zhenya", "+79111111333", "111133", "121233");
  95.     my_store.AddContact("Galya", "+79115511333", "166133", "127733");
  96.     my_store.AddContact("Olya", "+79115511333", "166133", "127733");
  97.  
  98.     my_store.ShowAllContacts();
  99.  
  100.     my_store.DelContact("Zhenya");
  101.  
  102.     my_store.ShowAllContacts();
  103.  
  104.     my_store.AddContact("Zhenya", "+79111111333", "111133", "121233");
  105.  
  106.     my_store.FindContactName("Galya");
  107.  
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment