bogdan_obukhovskii

6 задача

May 11th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.86 KB | None | 0 0
  1. /*
  2.     Весь код писал сам, List самодельный! (на шаблонах, т.к. для каждого задания очень много писать с этими списками)
  3.     Немного не успел доделать задуманное, подсписки должны были быть реализованными
  4.     у дочернего класса List для списка абонентов, сейчас у самого List'а
  5. */
  6.  
  7. #include <iostream>
  8. #include <string>
  9. #include <chrono>
  10.  
  11. using namespace std;
  12.  
  13. //элемент списка с любым типом ключа для сортировки
  14. template <typename KEY>
  15. class ListElement
  16. {
  17. public:
  18.     ListElement() { }
  19.  
  20.     ListElement(KEY k)
  21.     {
  22.         key = k;
  23.         next = nullptr;
  24.     }
  25.  
  26.     virtual void print()
  27.     {
  28.         cout << "Ключ: " << key << endl;
  29.     }
  30.  
  31.     KEY key; //ключ
  32.     ListElement<KEY>* next; //следующий
  33. };
  34.  
  35. //звонки, наследуют ListElement
  36. class Call : public ListElement<string>
  37. {
  38. public:
  39.     Call(string k, int t)
  40.     {
  41.         key = k;
  42.         duration = t;
  43.         price = PRICE_PER_MINUTE * (t / 60);
  44.         timestamp = chrono::system_clock::now();
  45.     }
  46.  
  47.     void print()
  48.     {
  49.         cout << "--> Телефон: " << key << ", ";
  50.         cout << "Время: " << duration / 3600 << " часов " << duration / 60 << " минут " << duration % 60 << " секунд, ";
  51.         cout << "Цена: " << price << " рублей." << endl;
  52.     }
  53.  
  54.     Call* next;
  55. protected:
  56.     chrono::system_clock::time_point timestamp; //метка времени (конец разговора в UNIX TIMESTAMP)
  57.     int duration; //длительность в секундах
  58.     double price; //цена звонка
  59.     const double PRICE_PER_MINUTE = 10.6; //цена за минуту
  60. };
  61.  
  62. //односвязный список, совместимый с ListElement
  63. template <typename T>
  64. class List
  65. {
  66. public:
  67.     List() : begin(nullptr), end(nullptr) { }
  68.  
  69.     void addToBack(T* temp)
  70.     {
  71.         if (!begin)
  72.         {
  73.             begin = temp;
  74.             end = temp;
  75.             return;
  76.         }
  77.         if (begin == end)
  78.         {
  79.             end = temp;
  80.             begin->next = end;
  81.             return;
  82.         }
  83.         end->next = temp;
  84.         end = temp;
  85.     }
  86.  
  87.     void sortASC()
  88.     {
  89.         if (!begin && begin == end)
  90.         {
  91.             cout << "В этом списке слишком мало элементов для сортировки" << endl;
  92.             return;
  93.         }
  94.  
  95.         T* root = begin;
  96.         T* new_root = nullptr;
  97.  
  98.         while (root)
  99.         {
  100.             T* node = root;
  101.             root = root->next;
  102.  
  103.             if (!new_root || node->key < new_root->key)
  104.             {
  105.                 node->next = new_root;
  106.                 new_root = node;
  107.             }
  108.             else
  109.             {
  110.                 T* current = new_root;
  111.                 while (current->next && !(node->key < current->next->key))
  112.                 {
  113.                     current = current->next;
  114.                 }
  115.  
  116.                 node->next = current->next;
  117.                 current->next = node;
  118.             }
  119.         }
  120.         delete root;
  121.         begin = new_root;
  122.     }
  123.  
  124.     T* findByKey(string key)
  125.     {
  126.         T* temp = begin;
  127.         while (temp)
  128.         {
  129.             if (temp->key == key) return temp;
  130.             temp = temp->next;
  131.         }
  132.         delete temp;
  133.     }
  134.  
  135.     void printASC()
  136.     {
  137.         if (!this) return;
  138.         T* temp = begin;
  139.  
  140.         while (temp)
  141.         {
  142.             temp->print();
  143.             temp = temp->next;
  144.         }
  145.         delete temp;
  146.     }
  147.  
  148.     T* begin;
  149.     T* end;
  150. };
  151.  
  152. //абоненты, наследуют ListElement
  153. class Abonent : public ListElement<string>
  154. {
  155. public:
  156.     Abonent(string k, string p) : subList(nullptr)
  157.     {
  158.         key = k;
  159.         personalInfo = p;
  160.     }
  161.  
  162.     void print()
  163.     {
  164.         cout << "Номер: " << key << ", ";
  165.         cout << "ФИО: " << personalInfo << endl;
  166.         subList->printASC();
  167.     }
  168.  
  169.     void addToSubList(Call* el)
  170.     {
  171.         if (!subList)
  172.             subList = new List<Call>;
  173.  
  174.         subList->addToBack(el);
  175.     }
  176.  
  177.     List<Call>* subList; //список звонков
  178.     Abonent* next;
  179.  
  180. protected:
  181.     string personalInfo; //ФИО
  182. };
  183.  
  184. class Menu
  185. {
  186. public:
  187.     bool isAlwaysRender;
  188.  
  189.     Menu(bool r, List<Abonent>*& abonents) : isAlwaysRender(r)
  190.     {
  191.         if (isAlwaysRender)
  192.             while (isAlwaysRender) render(abonents);
  193.         else
  194.             render(abonents);
  195.     }
  196.  
  197. protected:
  198.     void render(List<Abonent>*& abonents)
  199.     {
  200.         cout << endl << "****" << endl;
  201.         cout << "Вывести все номера: 1" << endl;
  202.         cout << "Ввести разговор:    2" << endl;
  203.         cout << "Отсортировать:      3" << endl;
  204.         cout << "Выйти из меню:    999" << endl;
  205.         cout << "Ожидание ввода..." << endl;
  206.         userPick(abonents);
  207.     }
  208.     void userPick(List<Abonent>*& abonents)
  209.     {
  210.         string input;
  211.         cin >> input;
  212.  
  213.         int cmd = atoi(input.c_str());
  214.         if (!cmd)
  215.         {
  216.             cout << "Команда должна быть числом!" << endl;
  217.             return;
  218.         }
  219.  
  220.         switch (cmd)
  221.         {
  222.         case 1:
  223.             abonents->printASC();
  224.             break;
  225.         case 2:
  226.         {
  227.             cout << "Введите существующий номер и время!" << endl;
  228.             string num; cin >> num;
  229.             int t; cin >> t;
  230.             Call* c = new Call(num, t);
  231.             Abonent* temp = abonents->findByKey(c->key);
  232.             if (temp)
  233.                 temp->addToSubList(c);
  234.             break;
  235.         }
  236.         case 3:
  237.             abonents->sortASC();
  238.             break;
  239.         case 999:
  240.             isAlwaysRender = false;
  241.             break;
  242.         default:
  243.             cout << "Такой команды нет!" << endl;
  244.             break;
  245.         }
  246.     }
  247. };
  248.  
  249. int main()
  250. {
  251.     setlocale(0, "rus");
  252.  
  253.     List<Abonent>* abonents = new List<Abonent>();
  254.  
  255.     Abonent* a1 = new Abonent("8999", "Обуховский Б.И.");
  256.     Abonent* a2 = new Abonent("7774", "Кукуев И.О.");
  257.     Abonent* a3 = new Abonent("8997", "Улюкаев И.О.");
  258.     Abonent* a4 = new Abonent("8996", "Забоев И.О.");
  259.     Abonent* a5 = new Abonent("7775", "Кабуркин И.О.");
  260.     Abonent* a6 = new Abonent("7300", "Кукуев И.О.");
  261.     Abonent* a7 = new Abonent("7330", "Кукуев И.О.");
  262.  
  263.     abonents->addToBack(a1);
  264.     abonents->addToBack(a2);
  265.     abonents->addToBack(a3);
  266.     abonents->addToBack(a4);
  267.     abonents->addToBack(a5);
  268.     abonents->addToBack(a6);
  269.     abonents->addToBack(a7);
  270.  
  271.     Menu* menu = new Menu(true, abonents);
  272.  
  273.     system("pause");
  274.     return 0;
  275. }
Add Comment
Please, Sign In to add comment