avr39ripe

cppPhoneBookGittAndriyVersion

Jul 29th, 2021
664
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Встреча №2
  2. Тема: Конструкторы, деструкторы
  3. Задание.
  4. Создайте приложение "Телефонная книга". Необходимо
  5. хранить данные об абоненте (ФИО, домашний телефон,
  6. рабочий телефон, мобильный телефон, дополнительная
  7. информация о контакте) внутри соответствующего класса.
  8. Наполните класс переменными-членами, функциями-членами,
  9. конструкторами, inline-функциями-членами,
  10. используйте инициализаторы, реализуйте деструктор.
  11. Обязательно необходимо выделять динамически память
  12. под ФИО. Предоставьте пользователю возможность добавлять
  13. новых абонентов, удалять абонентов, искать абонентов по ФИО,
  14. показывать всех абонентов, сохранять
  15. информацию в файл и загружать из файла.
  16.  
  17. #include <iostream>
  18. #include <fstream>
  19.  
  20. class Subscriber
  21. {
  22.     char* surname;
  23.     char* name;
  24.     char* secondName;
  25. public:
  26.     Subscriber() : Subscriber{ "Surname","Name","Second name" } {}
  27.     Subscriber(const char* surnameP, const char* nameP, const char* secondNameP) :
  28.         surname{ initSurname(surnameP) }, name{ initName(nameP) }, secondName{ initSecondName(secondNameP) } {}
  29.  
  30.     Subscriber(const Subscriber& object) :
  31.         surname{ initSurname(object.surname) }, name{ initName(object.name) }, secondName{ initSecondName(object.secondName) } {}
  32.  
  33.     Subscriber& setSurname(const char* surnameP);
  34.     Subscriber& setName(const char* nameP);
  35.     Subscriber& setSecondName(const char* secondNameP);
  36.  
  37.     char* initSurname(const char* surnameP);
  38.     char* initName(const char* nameP);
  39.     char* initSecondName(const char* secondNameP);
  40.  
  41.     Subscriber& init(const char* surnameP, const char* nameP, const char* secondNameP);
  42.  
  43.     const char* getSurname()const { return surname; }
  44.     const char* getName()const { return name; }
  45.     const char* getSecondName()const { return secondName; }
  46.  
  47.     Subscriber& print();
  48.  
  49.     ~Subscriber() { delete[] surname; delete[] name; delete[] secondName; }
  50. };
  51.  
  52. Subscriber& Subscriber::setSurname(const char* surnameP)
  53. {
  54.     auto len{ strlen(surnameP) + 1 };
  55.     if (!surname){ surname = new char[len]; }
  56.     else if (surname and strlen(surname) + 1 < len)
  57.     {
  58.         delete[] surname;
  59.         surname = new char[len];
  60.     }
  61.     strcpy_s(surname, len, surnameP);
  62.     return *this;
  63. }
  64.  
  65. Subscriber& Subscriber::setName(const char* nameP)
  66. {
  67.     auto len{ strlen(nameP) + 1 };
  68.     if (!name) { name = new char[len]; }
  69.     else if (name and strlen(name) + 1 < len)
  70.     {
  71.         delete[] name;
  72.         name = new char[len];
  73.     }
  74.     strcpy_s(name, len, nameP);
  75.     return *this;
  76. }
  77.  
  78. Subscriber& Subscriber::setSecondName(const char* secondNameP)
  79. {
  80.     auto len{ strlen(secondNameP) + 1 };
  81.     if (!secondName) { secondName = new char[len]; }
  82.     else if (secondName and strlen(secondName) + 1 < len)
  83.     {
  84.         delete[] secondName;
  85.         secondName = new char[len];
  86.     }
  87.     strcpy_s(secondName, len, secondNameP);
  88.     return *this;
  89. }
  90.  
  91. char* Subscriber::initSurname(const char* surnameP)
  92. {
  93.     surname = { new char[strlen(surnameP) + 1] };
  94.     strcpy_s(surname, strlen(surnameP) + 1, surnameP);
  95.     return surname;
  96. }
  97. char* Subscriber::initName(const char* NameP)
  98. {
  99.     name = { new char[strlen(NameP) + 1] };
  100.     strcpy_s(name, strlen(NameP) + 1, NameP);
  101.     return name;
  102. }
  103. char* Subscriber::initSecondName(const char* secondNameP)
  104. {
  105.     secondName = { new char[strlen(secondNameP) + 1] };
  106.     strcpy_s(secondName, strlen(secondNameP) + 1, secondNameP);
  107.     return secondName;
  108. }
  109.  
  110. Subscriber& Subscriber::init(const char* surnameP, const char* nameP, const char* secondNameP)
  111. {
  112.     setSurname(surnameP);
  113.     setName(nameP);
  114.     setSecondName(secondNameP);
  115.     return *this;
  116. }
  117.  
  118. Subscriber& Subscriber::print()
  119. {
  120.     std::cout << "\033[1;32m" << "Surname: " << "\033[0m" << surname
  121.         << "\033[1;32m" << "\nName: " << "\033[0m" << name << "\nSecond name: " << secondName << '\n';
  122.     return *this;
  123. }
  124.  
  125. class PhoneBook
  126. {
  127.     Subscriber contact;
  128.     int homePhone;
  129.     int workPhone;
  130.     int mobilePhone;
  131.     char* addContactDetails;
  132.  
  133. public:
  134.     PhoneBook() : PhoneBook{ "test", "test","test", 0,0,0,"test" } {}
  135.     PhoneBook(const char* surnameP, const char* nameP, const char* secondNameP, int homePhoneP, int workPhoneP, int mobilePhoneP, const char* addContactDetailsP) :
  136.         contact{ surnameP,nameP,secondNameP }, homePhone{ homePhoneP }, workPhone{ workPhoneP }, mobilePhone{ mobilePhoneP }, addContactDetails{ initContactDetailsP(addContactDetailsP) } {}
  137.     PhoneBook(const PhoneBook& contacts) :
  138.         contact{ contacts.contact }, homePhone{ contacts.homePhone }, workPhone{ contacts.workPhone }, mobilePhone{ contacts.mobilePhone }, addContactDetails{ initContactDetailsP(contacts.addContactDetails) } {}
  139.  
  140.     PhoneBook& setContacts(const char* surnameP, const char* nameP, const char* secondNameP)
  141.     {
  142.         contact.setSurname(surnameP); contact.setName(nameP); contact.setSecondName(secondNameP); return *this;
  143.     }
  144.    
  145.     PhoneBook& setHomePhone(int homePhoneP) { homePhone = homePhoneP; return *this; }
  146.     PhoneBook& setWorkPhone(int workPhoneP) { workPhone = workPhoneP; return *this; }
  147.     PhoneBook& setMobilePhone(int mobilePhoneP) { mobilePhone = mobilePhoneP; return *this; }
  148.  
  149.     PhoneBook& setContactDetails(const char* addContactDetailsP);
  150.  
  151.     const char* getContactSurname()const { return contact.getSurname(); }
  152.     const char* getContactName()const { return contact.getName(); }
  153.     const char* getContactSecondName()const { return contact.getSecondName(); }
  154.  
  155.     int getHomePhone()const { return homePhone; }
  156.     int getWorkPhone()const { return workPhone; }
  157.     int getMobilePhone()const { return mobilePhone; }
  158.  
  159.     const char* getaddContactDetails()const { return addContactDetails; }
  160.  
  161.     char* initContactDetailsP(const char* addContactDetailsP);
  162.  
  163.     PhoneBook& init(const char* surnameP, const char* nameP, const char* secondNameP, int homePhoneP, int workPhoneP, int mobilePhoneP, const char* addContactDetailsP);
  164.  
  165.     PhoneBook& print();
  166.  
  167.     ~PhoneBook() { delete[] addContactDetails; }
  168. };
  169.  
  170. PhoneBook& PhoneBook::setContactDetails(const char* addContactDetailsP)
  171. {
  172.     auto len{ strlen(addContactDetailsP) + 1 };
  173.     if (!addContactDetails) { addContactDetails = new char[len]; }
  174.     else if (addContactDetails and strlen(addContactDetails) + 1 < len)
  175.     {
  176.         delete[] addContactDetails;
  177.         addContactDetails = new char[len];
  178.     }
  179.     strcpy_s(addContactDetails, len, addContactDetailsP);
  180.     return *this;
  181. }
  182.  
  183. char* PhoneBook::initContactDetailsP(const char* addContactDetailsP)
  184. {
  185.     addContactDetails = { new char[strlen(addContactDetailsP) + 1] };
  186.     strcpy_s(addContactDetails, strlen(addContactDetailsP) + 1, addContactDetailsP);
  187.     return addContactDetails;
  188. }
  189.  
  190. PhoneBook& PhoneBook::init(const char* surnameP, const char* nameP, const char* secondNameP, int homePhoneP, int workPhoneP, int mobilePhoneP, const char* addContactDetailsP)
  191. {
  192.     setContacts(surnameP, nameP, secondNameP);
  193.     homePhone = homePhoneP;
  194.     workPhone = workPhoneP;
  195.     mobilePhone = mobilePhoneP;
  196.     setContactDetails(addContactDetailsP);
  197.     return *this;
  198. }
  199.  
  200. PhoneBook& PhoneBook::print()
  201. {
  202.     contact.print();
  203.     std::cout << "Home phone: " << homePhone << "\nWork phone: " << workPhone << "\nMobile phone: " << mobilePhone
  204.         << "\nAdditional contact details: " << addContactDetails << '\n';
  205.     return *this;
  206. }
  207.  
  208. void listPhoneBook(PhoneBook* begin, PhoneBook* end)
  209. {
  210.     for (auto contacts{ begin }; contacts != end; ++contacts)
  211.     {
  212.         contacts->print(); std::cout << '\n';
  213.     }
  214. }
  215.  
  216. void contactSearch(PhoneBook* begin, PhoneBook* end, char* found, bool(*searchCriterion)(const PhoneBook& const contacts, char* found))
  217. {
  218.     for (auto contacts{ begin }; contacts != end; ++contacts)
  219.     {
  220.         if (searchCriterion(*contacts, found)) { contacts->print(); std::cout << '\n'; }
  221.     }
  222. }
  223.  
  224. bool searchBySurname(const PhoneBook& const contacts, char* found)
  225. {
  226.     return strstr(contacts.getContactSurname(), found);
  227. }
  228.  
  229. bool searchByName(const PhoneBook& const contacts, char* found)
  230. {
  231.     return strstr(contacts.getContactName(), found);
  232. }
  233.  
  234. void savePhoneBook(PhoneBook* begin, PhoneBook* end)
  235. {
  236.     std::ofstream outf;
  237.     outf.open("PhoneBook.txt");
  238.     for (auto contacts{ begin }; contacts != end; ++contacts)
  239.     {
  240.         outf << contacts->getContactSurname() << '\n';
  241.         outf << contacts->getContactName() << '\n';
  242.         outf << contacts->getContactSecondName() << '\n';
  243.         outf << contacts->getHomePhone() << '\n';
  244.         outf << contacts->getWorkPhone() << '\n';
  245.         outf << contacts->getMobilePhone() << '\n';
  246.         outf << contacts->getaddContactDetails() << '\n';
  247.     }
  248.     outf.close();
  249. }
  250.  
  251. void loadPhoneBook(PhoneBook* begin, PhoneBook* end)
  252. {
  253.     std::ifstream inf;
  254.     char surnameP[15];
  255.     char nameP[15];
  256.     char secondNameP[15];
  257.     int homePhoneP{ 0 };
  258.     int workPhoneP{ 0 };
  259.     int mobilePhoneP{ 0 };
  260.     char contactDetailsP[15];
  261.  
  262.     inf.open("PhoneBook.txt");
  263.     for (auto contacts{ begin }; contacts != end; ++contacts)
  264.     {
  265.         inf.getline(surnameP, 15);
  266.         inf.getline(nameP, 15);
  267.         inf.getline(secondNameP, 15);
  268.         inf >> homePhoneP;
  269.         inf >> workPhoneP;
  270.         inf >> mobilePhoneP;
  271.         inf.ignore(100, '\n');
  272.         inf.getline(contactDetailsP, 15);
  273.         contacts->init(surnameP, nameP, secondNameP, homePhoneP, workPhoneP, mobilePhoneP, contactDetailsP);
  274.     }
  275.     inf.close();
  276. }
  277.  
  278. void pastInArr(PhoneBook*& Arr, size_t& arrSize, size_t idxPastElem)
  279. {
  280.     PhoneBook* newArr{ new PhoneBook[arrSize + 1] };
  281.     for (size_t i{ 0 }; i < arrSize; ++i)
  282.     {
  283.         newArr[i + (i >= idxPastElem)].PhoneBook::PhoneBook(Arr[i]);
  284.     }
  285.     delete[] Arr;
  286.     Arr = newArr;
  287.     arrSize++;
  288. }
  289.  
  290. void cutFromArr(PhoneBook*& Arr, size_t& arrSize, size_t idxCutElem)
  291. {
  292.     PhoneBook* newArr{ new PhoneBook[arrSize--] };
  293.     for (size_t i{ 0 }; i < arrSize; ++i)
  294.     {
  295.         newArr[i].PhoneBook::PhoneBook(Arr[i + (i >= idxCutElem)]);
  296.     }
  297.     delete[]Arr;
  298.     Arr = newArr;
  299. }
  300.  
  301. void phoneBookSort(PhoneBook* contacts, size_t PhoneBookSize, bool(*criteria)(const PhoneBook& contactsA, const PhoneBook& contactsB))
  302. {
  303.     PhoneBook tmp;
  304.     for (size_t head{ 0 }; head < PhoneBookSize; ++head)
  305.     {
  306.         for (size_t tail{ PhoneBookSize - 1 }; tail > head; --tail)
  307.         {
  308.             if (criteria(contacts[tail], contacts[head]))
  309.             {
  310.                 tmp.PhoneBook::PhoneBook(contacts[tail]);
  311.                 contacts[tail].PhoneBook::PhoneBook(contacts[head]);
  312.                 contacts[head].PhoneBook::PhoneBook(tmp);
  313.             }
  314.         }
  315.     }
  316. }
  317.  
  318. bool sortBySurname(const PhoneBook& contactsA, const PhoneBook& contactsB)
  319. {
  320.     return strcmp(contactsA.getContactSurname(), contactsB.getContactSurname()) < 0;
  321. }
  322.  
  323. bool sortByName(const PhoneBook& contactsA, const PhoneBook& contactsB)
  324. {
  325.     return strcmp(contactsA.getContactName(), contactsB.getContactName()) < 0;
  326. }
  327.  
  328. int main()
  329. {
  330.     size_t PhoneBookSize{ 10 };
  331.     PhoneBook* contacts{ new PhoneBook[PhoneBookSize] };
  332.     auto contactsEnd{ contacts + PhoneBookSize };
  333.  
  334.     contacts[0].init("Kulyk", "Volodymir", "Oleksandrovich", 0, 0, 312231701, " ");
  335.     contacts[1].init("Stepanuk", "Oleh", "Vasylyovich", 0, 0, 672356575, " ");
  336.     contacts[2].init("Marchuk", "Ivan", "Stepanovich", 0, 0, 6756455, " ");
  337.     contacts[3].init("Kozluk", "Andriy", "Mykolayovich", 0, 0, 502456988, "Friend");
  338.     contacts[4].init("Philipchuk", "Yriy", "Leonidvich", 312221321, 312231705, 677777777, "Director");
  339.     contacts[5].init("Karasev", "Dmytro", "Stepanovich", 0, 312225565, 679638521, " ");
  340.     contacts[6].init("Shevchuk", "Oleksander", "Ivanovich", 0, 312225533, 670996523, " ");
  341.     contacts[7].init("Solovey", "Andriy", "Oleksandrovich", 312228511, 312231799, 507412525, "Mechanic");
  342.     contacts[8].init("Havryluk", "Valeriy", "Stepanovich", 312221300, 0, 637894532, " ");
  343.     contacts[9].init("Marchuk", "Olena", "Mykolaivna", 312221515, 312231755, 671030303, "Famaly doctor");
  344.  
  345.     listPhoneBook(contacts, contactsEnd);
  346.  
  347.     char foundS[]{ "Marchuk" };
  348.     std::cout << "\033[1;31m" << "\nSearch by Surname:\n" << "\033[0m";
  349.     contactSearch(contacts, contactsEnd, foundS, searchBySurname);
  350.  
  351.     char foundN[]{ "Andriy" };
  352.     std::cout << "\033[1;31m" << "\nSearch by name:\n" << "\033[0m";
  353.     contactSearch(contacts, contactsEnd, foundN, searchByName);
  354.  
  355.     savePhoneBook(contacts, contactsEnd);
  356.  
  357.     std::cout << "\033[1;31m" << "\nLoad contacts from file :\n" << "\033[0m";
  358.     PhoneBook* fileContacts{ new PhoneBook[PhoneBookSize] };
  359.     auto fileContactsEnd{ fileContacts + PhoneBookSize };
  360.     loadPhoneBook(fileContacts, fileContactsEnd);
  361.     listPhoneBook(fileContacts, fileContactsEnd);
  362.  
  363.     std::cout << "\033[1;31m" << "\nAdd contact in contacts :\n" << "\033[0m";
  364.     pastInArr(contacts, PhoneBookSize, 3);
  365.     contacts[3].init("Savchuk", "Oleh", "Olehovich", 0, 0, 502582525, "worker");
  366.     listPhoneBook(contacts, contacts + PhoneBookSize);
  367.  
  368.     std::cout << "\033[1;31m" << "\nDelete contact from contacts :\n" << "\033[0m";
  369.     cutFromArr(contacts, PhoneBookSize, 3);
  370.     listPhoneBook(contacts, contacts + PhoneBookSize);
  371.  
  372.     phoneBookSort(contacts, PhoneBookSize, sortBySurname);
  373.     std::cout << "\033[1;31m" << "\nSorted by Surname:\n" << "\033[0m";
  374.     listPhoneBook(contacts, contacts + PhoneBookSize);
  375.  
  376.     delete[] contacts;
  377.     return 0;
  378. }
  379.  
  380.  
RAW Paste Data