Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.47 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <windows.h>
  5. #include "fstream"
  6. #include "string"
  7. using namespace std;
  8.  
  9. int num = 0;
  10. char** name = new char*[num];
  11. char** phone = new char*[num];
  12. bool yes = true;
  13. int taskNumber;
  14.  
  15. //struct cell
  16. //{
  17. //  int num = 0;
  18. //};
  19. //
  20. //struct NamePhone
  21. //{
  22. //  cell number;
  23. //  char** name = new char*[number.num];
  24. //  char** phone = new char*[number.num];
  25. //};
  26.  
  27. HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
  28.  
  29. void list();
  30. int newList();
  31. void addList();
  32. void deleteList();
  33. void Edit(int& num);
  34. void searchList();
  35. void saveList();
  36. char* getString();
  37. void addName(char**& names, char**& phone, char* name, char* phone2, int& num);
  38. void addName2(char**& names, char**& phone, char* name, char* phone2);
  39.  
  40. void main()
  41. {
  42.  
  43.    
  44.     /*if (!fin.is_open())
  45.     {
  46.         cout << "no" << endl;
  47.     }
  48.     else
  49.     {
  50.         cout << "yes" << endl;
  51.         char ch;
  52.         while (fin.get(ch))
  53.         {
  54.             cout << ch;
  55.         }
  56.     }
  57.     Sleep(5000);*/
  58.  
  59.     /*getline(f, s);
  60.     f.close();
  61.     cout << s << endl;*/
  62.  
  63.     FILE* fileNum = fopen("data.bin", "rb");
  64.     fscanf(fileNum, "%i", &num);
  65.     fclose(fileNum);
  66.  
  67.     string file = "name.bin";
  68.     ifstream fin;
  69.     fin.open(file);
  70.  
  71.     string str;
  72.     int i = 0;
  73.     while (!fin.eof())
  74.     {
  75.         fin >> name[i];
  76.         i++;
  77.     }
  78.     fin.close();
  79.  
  80.     /*for (int i = 0; i < num; i++)
  81.     {
  82.         fin >> name[i];
  83.     }
  84.     for (int i = 0; i < num; i++)
  85.     {
  86.         fin >> phone[i];
  87.     }*/
  88.     /*if (num != 0) {
  89.         file = fopen("name.bin", "rb");
  90.         for (int i = 0; i < num; i++)
  91.         {
  92.             fscanf(file, "%s\n", name[i]);
  93.         }
  94.         fclose(file);
  95.  
  96.         file = fopen("number.bin", "rb");
  97.         for (int i = 0; i < num; i++)
  98.         {
  99.             fscanf(file, "%s\n", phone[i]);
  100.         }
  101.         fclose(file);
  102.     }*/
  103.  
  104.     while (true)
  105.     {
  106.         int selection = newList();
  107.  
  108.         system("cls");
  109.         switch (selection)
  110.         {
  111.         case 0: {
  112.             cout << "List: " << endl << endl;
  113.             list();
  114.             cout << endl;
  115.             break;
  116.         }
  117.         case 1: {
  118.             addList();
  119.             break;
  120.         }
  121.         case 2: {
  122.             deleteList();
  123.             break;
  124.         }
  125.         case 3: {
  126.             Edit(num);
  127.             break;
  128.         }
  129.         case 4: {
  130.             searchList();
  131.             break;
  132.         }
  133.         case 5: {
  134.             saveList();
  135.             break;
  136.         }
  137.         default:
  138.             cout << "Error!" << endl;
  139.             break;
  140.         }
  141.         system("pause");
  142.     }
  143. }
  144.  
  145. void list()
  146. {
  147.     for (int i = 0; i < num; i++)
  148.     {
  149.         cout << i + 1 << ". " << name[i] << "\t" << phone[i] << endl;
  150.     }
  151. }
  152.  
  153. int newList()
  154. {
  155.     char menu[6][30] =
  156.     {
  157.         " List    ",
  158.         " Add     ",
  159.         " Remove  ",
  160.         " Edit    ",
  161.         " Search  ",
  162.         " Save    "
  163.     };
  164.     int selection = 0;
  165.  
  166.     while (true)
  167.     {
  168.         system("cls");
  169.         for (int i = 0; i < 6; i++)
  170.         {
  171.             if (i == selection)
  172.             {
  173.                 SetConsoleTextAttribute(h, 124);
  174.                 cout << menu[i] << endl;
  175.                 SetConsoleTextAttribute(h, 7);
  176.             }
  177.             else
  178.             {
  179.                 cout << menu[i] << endl;
  180.             }
  181.         }
  182.  
  183.         int key = _getch();
  184.         if (key == 224)
  185.         {
  186.             key = _getch();
  187.             if (key == 72)
  188.             {
  189.                 selection--;
  190.                 if (selection < 0)
  191.                     selection = 5;
  192.             }
  193.             if (key == 80)
  194.             {
  195.                 selection++;
  196.                 selection = selection % 6;
  197.             }
  198.         }
  199.         else if (key == 13)
  200.         {
  201.             return selection;
  202.         }
  203.     }
  204. }
  205.  
  206. void addList()
  207. {
  208.     cout << "Enter name: ";
  209.     char* name2 = getString();
  210.     cout << "Enter number: ";
  211.     char* phone2 = getString();
  212.     if (yes)
  213.         addName(name, phone, name2, phone2, num);
  214.     else
  215.         addName2(name, phone, name2, phone2);
  216. }
  217.  
  218. char* getString()
  219. {
  220.     char* name2;
  221.     char buffer[1000];
  222.     cin.getline(buffer, 1000);
  223.     name2 = new char[strlen(buffer) + 1];
  224.     strcpy(name2, buffer);
  225.     return name2;
  226. }
  227.  
  228. void deleteList()
  229. {
  230.     cout << "Remove: " << endl << endl;
  231.     list();
  232.     cout << endl;
  233.     if (num == 0)
  234.         cout << "You list is empty" << endl << endl;
  235.     else
  236.     {
  237.         int taskNumber;
  238.         cout << "Enter task number: ";
  239.         cin >> taskNumber;
  240.         taskNumber--;
  241.         if (taskNumber >= num)
  242.             cout << "Error!" << endl;
  243.         else
  244.         {
  245.             for (int i = taskNumber; i < num; i++)
  246.             {
  247.                 if (i + 1 < num)
  248.                 {
  249.                     strcpy(name[i], name[i + 1]);
  250.                     strcpy(phone[i], phone[i + 1]);
  251.                 }
  252.                 else if (i + 1 == num)
  253.                 {
  254.                     strcpy(name[i], " ");
  255.                     strcpy(phone[i], " ");
  256.                 }
  257.             }
  258.             num--;
  259.         }
  260.         cin.ignore();
  261.     }
  262. }
  263.  
  264. void addName(char**& name, char**& phone, char* name2, char* phone2, int& num)
  265. {
  266.     char** temp = new char*[num + 1];
  267.     for (int i = 0; i < num; i++)
  268.     {
  269.         temp[i] = name[i];
  270.     }
  271.     delete[] name;
  272.     name = temp;
  273.     name[num] = name2;
  274.  
  275.     char** temp2 = new char*[num + 1];
  276.     for (int i = 0; i < num; i++)
  277.     {
  278.         temp2[i] = phone[i];
  279.     }
  280.     delete[] phone;
  281.     phone = temp2;
  282.     phone[num] = phone2;
  283.     num++;
  284. }
  285.  
  286. void addName2(char**& name, char**& phone, char* name2, char* phone2)
  287. {
  288.     name[taskNumber] = name2;
  289.     phone[taskNumber] = phone2;
  290.     yes = true;
  291. }
  292.  
  293. void Edit(int& num)
  294. {
  295.     cout << "Edit: " << endl << endl;
  296.     list();
  297.     cout << endl;
  298.     if (num == 0)
  299.         cout << "You list is empty" << endl << endl;
  300.     else
  301.     {
  302.         cout << "Enter task number: ";
  303.         cin >> taskNumber;
  304.         taskNumber--;
  305.  
  306.         if (taskNumber >= num || taskNumber < 0)
  307.             cout << "Error!" << endl;
  308.         else
  309.         {
  310.             strcpy(name[taskNumber], " ");
  311.             strcpy(phone[taskNumber], " ");
  312.         }
  313.         cin.ignore();
  314.         yes = false;
  315.         addList();
  316.     }
  317. }
  318.  
  319. void searchList()
  320. {
  321.  
  322. }
  323.  
  324. void saveList()
  325. {
  326.     FILE* file = fopen("data.bin", "wb");
  327.     char buffer[10];
  328.     _itoa(num, buffer, 10);
  329.     fputs(buffer, file);
  330.     fclose(file);
  331.    
  332.     file = fopen("name.bin", "wb");
  333.     for (int i = 0; i < num; i++)
  334.     {
  335.         fprintf(file, "%s\n", name[i]);
  336.     }
  337.     fclose(file);
  338.  
  339.     file = fopen("number.bin", "wb");
  340.     for (int i = 0; i < num; i++)
  341.     {
  342.         fprintf(file, "%s\n", phone[i]);
  343.     }
  344.     fclose(file);
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement