Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstring>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. typedef struct {
  9.     char name[80 + 1];
  10.     char phone[12 + 1];
  11.     int group;
  12. } Contact;
  13.  
  14. typedef struct {
  15.     Contact* data = new Contact;
  16.     size_t size = 0;
  17.     size_t capacity = 0;
  18. } Vector;
  19.  
  20. Vector* CreateList();
  21. void menu(Vector* list);
  22. void NameCheckup(Vector* list, Contact* c);
  23. int StrToEnum(char group[20]);
  24. void CreateContact(Vector* list);
  25. void DeleteContact(Vector* list);
  26. void EditName(Vector* list, int index);
  27. void EditPhoneNumber(Vector* list, int index);
  28. void EditGroup(Vector* list, int index);
  29. int FindByName(Vector* list, char* name);
  30. void EditContact(Vector* list);
  31. void erase(Vector* v, size_t index);
  32. void reserve(Vector* v, size_t newCap);
  33. void PrintByName(Vector* list);
  34. void PrintByNumber(Vector* list);
  35. void PrintByGroup(Vector* list);
  36. void ShowContacts(Vector* list);
  37. void PrintContact(Vector* list, int index);
  38. void PrintAllContacts(Vector* list);
  39.  
  40. enum Groups {
  41.     NO_GROUP,
  42.     FAMILY,
  43.     FRIENDS,
  44.     COLLEAGUES
  45. };
  46.  
  47. Vector* CreateList() {
  48.     Vector* list = new Vector;
  49.     list->data = 0;
  50.     list->size = 0;
  51.     list->capacity = 0;
  52.     return list;
  53. }
  54. void menu(Vector* list){
  55.     cout << "List of available commands :" << endl
  56.         << "1. Create contact" << endl
  57.         << "2. Delete contact" << endl
  58.         << "3. Edit contact" << endl
  59.         << "4. Show contact(s)" << endl
  60.         << "5. Exit"<< endl
  61.         << "Command :" << endl;
  62.     int command;
  63.     cin >> command;
  64.     switch (command) {
  65.         case 1: {
  66.             CreateContact(list);
  67.             break;
  68.         }
  69.         case 2: {
  70.             DeleteContact(list);
  71.             break;
  72.         }
  73.         case 3: {
  74.             EditContact(list);
  75.             break;
  76.         }
  77.         case 4: {
  78.             ShowContacts(list);
  79.             break;
  80.         }
  81.         case 5: {
  82.             return;
  83.         }
  84.         default: {
  85.             cout << " This command does not exist.";
  86.             menu(list);
  87.         }
  88.  
  89.     }
  90. }
  91. void NameCheckup(Vector* list, Contact* c) {
  92.     bool flag = false;
  93.     while (true) {
  94.         cout << "Enter name: ";
  95.         char* buf = new char[81];
  96.         cin.getline(buf, 81);
  97.         for (int i = 0; i < list->size; ++i) {
  98.             if (strcmp(buf, list->data[i].name) == 0)
  99.             {
  100.                 cout << "Error! This name cannot be used. Choose another name." << endl;
  101.                 flag = true;
  102.                 break;
  103.             }
  104.         }
  105.         if (flag == false) {
  106.             strcpy(c->name, buf);
  107.             return;
  108.         }
  109.         else {
  110.             flag = false;
  111.             continue;
  112.         }
  113.     }
  114.  
  115. }
  116. int StrToEnum(char group[20]) {
  117.  
  118.     if (strcmp(group, "FRIENDS") == 0) {
  119.         return FRIENDS;
  120.     }
  121.     else if (strcmp(group, "FAMILY") == 0) {
  122.         return FAMILY;
  123.     }
  124.     else if (strcmp(group, "COLLEAGUES") == 0) {
  125.         return COLLEAGUES;
  126.     }
  127.     else {
  128.         return NO_GROUP;
  129.     }
  130. }
  131. void CreateContact(Vector* list) {
  132.     reserve(list, list->capacity + 1);
  133.  
  134.     cout << "Contact creation:" << endl;
  135.  
  136.     Contact* c = new Contact;
  137.     NameCheckup(list, c);
  138.  
  139.     cout<< "Enter phone number: ";
  140.     cin.getline(c->phone, 12);
  141.  
  142.     cout << "Enter group: ";
  143.     char group[20];
  144.     cin >> group;
  145.     c->group = StrToEnum(group);
  146.  
  147.     list->data[list->size + 1] = *c;
  148. }
  149.  
  150. void DeleteContact(Vector* list) {
  151.  
  152.     cout << "Enter name: ";
  153.     char* del = new char[80];
  154.     cin.getline(del, 80);
  155.     for (int i = 0; i < list->size; i++) {
  156.         if (strcmp(del, list->data[i].name) == 0) {
  157.             erase(list, i);
  158.  
  159.             return;
  160.         }
  161.     }
  162.     cout << "This contact does not exist" << endl;
  163. }
  164. void EditName(Vector* list, int index) {
  165.     char* newName = new char[81];
  166.     cin.getline(newName, 81);
  167.     strcpy(list->data[index].name, newName);
  168. }
  169. void EditPhoneNumber(Vector* list, int index) {
  170.     char* newNumber = new char[13];
  171.     cin.getline(newNumber, 13);
  172.     strcpy(list->data[index].phone, newNumber);
  173. }
  174. void EditGroup(Vector* list, int index) {
  175.     char newGroup[20];
  176.     cin.getline(newGroup, 20);
  177.     list->data[index].group = StrToEnum(newGroup);
  178. }
  179. int FindByName(Vector* list, char* name) {
  180.     for (int i = 0; i < list->size; i++) {
  181.         if (strcmp(name, list->data[i].name) == 0) {
  182.             return i;
  183.         }
  184.     }
  185.     cout << "That contact does not exist" << endl;
  186.     return -1;
  187. }
  188. void EditContact(Vector* list) {
  189.     int index;
  190.     while (true) {
  191.         cout << "Enter contact name: ";
  192.         char* buf = new char[80];
  193.         cin.getline(buf, 80);
  194.  
  195.         int ind = FindByName(list, buf);
  196.             if (ind == -1) {
  197.                 cout << "That contact does not exist. Try again:" << endl;
  198.                 continue;
  199.             }
  200.             else {
  201.                 index = ind;
  202.                 break;
  203.             }
  204.     }
  205.     cout << "1. Edit name " << endl
  206.          << "2. Edit phone number" << endl
  207.          << "3. Edit group" << endl
  208.          << "4. Back " << endl;
  209.  
  210.     int editby;
  211.     cin >> editby;
  212.  
  213.     switch (editby) {
  214.     case 1:
  215.         EditName(list, index);
  216.         break;
  217.  
  218.     case 2:
  219.         EditPhoneNumber(list, index);
  220.         break;
  221.  
  222.     case 3:
  223.         EditGroup(list, index);
  224.         break;
  225.  
  226.     case 4:
  227.         //возврат к меню
  228.         break;
  229.  
  230.     default:
  231.         cout << "Error";
  232.         break;
  233.  
  234.     }
  235. }
  236. void erase(Vector* v, size_t index) {
  237.  
  238.     for (size_t i = index; i < v->size; i++) {
  239.         v->data[i] = v->data[i + 1];
  240.     }
  241.     v->size--;
  242.     reserve(v, v->size);
  243. }
  244. void reserve(Vector* v, size_t newCap) {
  245.  
  246.     if (newCap > v->capacity) {
  247.         Contact* new_data = new Contact [newCap];
  248.         memcpy(new_data, v->data, v->size * sizeof(int));
  249.         delete[] v->data;
  250.         v->data = new_data;
  251.     }
  252. }
  253. void PrintByName(Vector* list) {
  254.     cout << "Enter name: ";
  255.     char* name = new char[81];
  256.     cin.getline(name, 81);
  257.  
  258.     for (int i = 0; i < list->size; ++i) {
  259.         if (strcmp(name, list->data[i].name) == 0) {
  260.             PrintContact(list, i);
  261.         }
  262.     }
  263. }
  264. void PrintByNumber(Vector* list) {
  265.     cout << "Enter phone number: ";
  266.     char* number = new char[13];
  267.     cin.getline(number, 13);
  268.  
  269.     for (int i = 0; i < list->size; ++i) {
  270.         if (strcmp(number, list->data[i].phone) == 0) {
  271.             PrintContact(list, i);
  272.         }
  273.     }
  274. }
  275. void PrintByGroup(Vector* list) {
  276.     cout << "Enter group: ";
  277.     char* group = new char[20];
  278.     cin.getline(group, 20);
  279.     int gr = StrToEnum(group);
  280.     for (int i = 0; i < list->size; ++i) {
  281.         if (gr == list->data[i].group) {
  282.             PrintContact(list, i);
  283.         }
  284.     }
  285. }
  286. void ShowContacts(Vector* list) {
  287.     cout << "Print contact(s) :" << endl
  288.         << "1. By name" << endl
  289.         << "2. By phone number" << endl
  290.         << "3. By group" << endl
  291.         << "4. All contacts" << endl
  292.         << "5. Back" << endl;
  293.     int comand;
  294.     cin >> comand;
  295.     switch(comand) {
  296.         case 1:
  297.             PrintByName(list);
  298.             break;
  299.         case 2:
  300.             PrintByNumber(list);
  301.             break;
  302.         case 3:
  303.             PrintByGroup(list);
  304.             break;
  305.         case 4:
  306.             PrintAllContacts(list);
  307.             break;
  308.         case 5:
  309.             //возврат к менюшке;
  310.         default:
  311.             cout << "Error!";
  312.             break;
  313.     }
  314. }
  315. void PrintContact(Vector* list, int index) {
  316.     cout << "Name: " << list->data[index].name << endl;
  317.     cout << "Number: " << list->data[index].phone << endl;
  318.     cout << "Group: ";
  319.     switch (list->data[index].group) {
  320.  
  321.     case NO_GROUP: {
  322.         cout << "No group";
  323.         break;
  324.     }
  325.  
  326.     case FAMILY: {
  327.         cout << "Family";
  328.         break;
  329.     }
  330.  
  331.     case FRIENDS: {
  332.         cout << "Friends";
  333.         break;
  334.     }
  335.  
  336.     case COLLEAGUES: {
  337.         cout << "Colleagues";
  338.         break;
  339.     }
  340.     }
  341. }
  342. void PrintAllContacts(Vector* list) {
  343.     for (int i = 0; i < list->size; ++i) {
  344.         PrintContact(list, i);
  345.     }
  346. }
  347. int main(){
  348.     Vector* PhoneBook = CreateList();
  349.     while (true) {
  350.         menu(PhoneBook);
  351.     }
  352.     return 0;
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement