Advertisement
35657

Untitled

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