Advertisement
Toxotsist

Сиаод 3 Релиз

May 19th, 2021
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct Bus
  7. {
  8.     string way_id;
  9.     int time;
  10.     string way;
  11.     string bus_id;
  12.     string cost;
  13.     string date;
  14. };
  15.  
  16. struct Node
  17. {
  18.     Bus data;
  19.     Node* next;
  20.     Node* prev;
  21. };
  22.  
  23. void print(Node* list)
  24. {
  25.     if (list != NULL)
  26.     {
  27.         Node* t = list;
  28.         while (t != NULL)
  29.         {
  30.             cout << t->data.bus_id << " " << t->data.way << " " << t->data.date << " " << t->data.cost << endl;
  31.             t = t->next;
  32.         }
  33.     }
  34. }
  35.  
  36. void add(Node** list, Bus bus)
  37. {
  38.     Node* n = new Node();
  39.     n->data = bus;
  40.     if (*list == NULL)
  41.     {
  42.         n->next = NULL;
  43.         n->prev = NULL;
  44.         *list = n;
  45.     }
  46.     else
  47.     {
  48.         Node* t = *list;
  49.         while (t->next != NULL && t->data.bus_id.compare(bus.bus_id) != 0)
  50.         {
  51.             t = t->next;
  52.         }
  53.         if (t->data.bus_id.compare(bus.bus_id) == 0)
  54.         {
  55.             if (t->data.date < bus.date)
  56.             {
  57.                 if (t == *list)
  58.                 {
  59.                     n->next = *list;
  60.                     n->prev = NULL;
  61.                     (*list)->prev = n;
  62.                     *list = n;
  63.                 }
  64.                 else
  65.                 {
  66.                     n->prev = t->prev;
  67.                     n->next = t;
  68.                     if (t->prev != NULL)
  69.                         t->prev->next = n;
  70.                     t->prev = n;
  71.                 }
  72.             }
  73.             else
  74.             {
  75.                 while (t->next != NULL && t->data.bus_id.compare(bus.bus_id) == 0 && t->data.date >= bus.bus_id)
  76.                 {
  77.                     t = t->next;
  78.                 }
  79.                 if (t->data.bus_id.compare(bus.bus_id) == 0 && t->data.date < bus.date || t->data.bus_id.compare(bus.bus_id) != 0)
  80.                 {
  81.                     n->prev = t->prev;
  82.                     n->next = t;
  83.                     t->prev->next = n;
  84.                     t->prev = n;
  85.                 }
  86.                 else
  87.                 {
  88.                     n->next = t->next;
  89.                     n->prev = t;
  90.                     t->next = n;
  91.                 }
  92.             }
  93.         }
  94.         else
  95.         {
  96.             n->next = NULL;
  97.             n->prev = t;
  98.             t->next = n;
  99.         }
  100.     }
  101. }
  102.  
  103. void pushBackById(Node* list, string b_id) {
  104.    
  105.     Node* n = new Node;
  106.     if (list == NULL) {
  107.         n->next = NULL;
  108.         n->prev = NULL;
  109.         list = n;
  110.     }
  111.     else {
  112.         Node* l = list;
  113.         while (l->next != NULL && l->data.bus_id.compare(b_id)) {
  114.            l = l->next;
  115.         }
  116.         l->next = new Node;
  117.     }
  118. };
  119.  
  120. Node* search(Node* list, string id)
  121. {
  122.     Node* t = list;
  123.     while (t != NULL && t->data.bus_id.compare(id) != 0)
  124.     {
  125.         t = t->next;
  126.     }
  127.     return t;
  128. }
  129.  
  130. int cnt(Node* list, string id)
  131. {
  132.     int cont = 0;
  133.     Node* t = list;
  134.     while (t != NULL && t->data.bus_id.compare(id) == 0)
  135.     {
  136.         cont++;
  137.         t = t->next;
  138.     }
  139.     return cont;
  140. }
  141.  
  142. void remove(Node** list, Node* bus_list)
  143. {
  144.     if (*list == bus_list->next)
  145.     {
  146.         while (*list != NULL && (*list)->data.bus_id.compare(bus_list->data.bus_id) == 0)
  147.         {
  148.             Node* rm = *list;
  149.             *list = (*list)->next;
  150.             if ((*list) != NULL)
  151.                 (*list)->prev = NULL;
  152.             delete rm;
  153.         }
  154.     }
  155.     else
  156.     {
  157.         Node* t = *list;
  158.         while (t->next != bus_list->next)
  159.         {
  160.             t = t->next;
  161.         }
  162.         Node* rm;
  163.         while (t->next != NULL && t->next->data.bus_id.compare(bus_list->data.bus_id) == 0)
  164.         {
  165.             rm = t->next;
  166.             if (t->next->next != NULL)
  167.                 t->next->next->prev = t;
  168.             t->next = t->next->next;
  169.             delete rm;
  170.         }
  171.     }
  172. }
  173.  
  174. int main()
  175. {
  176.     setlocale(LC_ALL, "Russian");
  177.     Node* list = NULL; //пустой список
  178.     int command = -1;
  179.     //работа со списком через меню
  180.     while (command != 0)
  181.     {
  182.         cout << "\n1 - Добавить новый автобус\n";
  183.         cout << "2 - Вывод списка в прямом порядке\n";
  184.         cout << "3 - Удалить автобус заданного маршрута\n";
  185.         cout << "4 - Найти маршрутку\n";
  186.         cout << "0 - Выход\n";
  187.         cout << "\nВведите номер команды: ";
  188.         cin >> command;
  189.         cin.ignore();
  190.         switch (command)
  191.         {
  192.         case 1:
  193.         {
  194.             Bus bus;
  195.             cout << "\nВведите номер автобуса: ";
  196.             getline(cin, bus.bus_id);
  197.             cout << "Введите путь: ";
  198.             getline(cin, bus.way);
  199.             cout << "Введите время отправления: ";
  200.             cin >> bus.date;
  201.             add(&list, bus);
  202.             break;
  203.         }
  204.         case 2:
  205.         {
  206.             cout << "\nСписок в прямом порядке\n";
  207.             print(list);
  208.             break;
  209.         }
  210.         case 3:
  211.         {
  212.             string bus_id;
  213.             cout << "\nВведите модель для удаления: ";
  214.             getline(cin, bus_id);
  215.             Node* s = search(list, bus_id); //выполняем поиск
  216.             if (s != NULL) //если поиск успешный
  217.             {
  218.                 //список для удаления
  219.                 Node* rm_list = new Node();
  220.                 rm_list->data.bus_id = bus_id;
  221.                 rm_list->next = s;
  222.                 remove(&list, rm_list);
  223.                 cout << "\nУдаление выполнено\n";
  224.             }
  225.             else
  226.             {
  227.                 cout << "\nАвтомобили заданной марки не найдены!\n";
  228.             }
  229.             break;
  230.         }
  231.         case 4:
  232.         {
  233.             cout << "\nВведите номер маршрутки:\n";
  234.             string bus_id;
  235.             getline(cin, bus_id);
  236.             int s = cnt(list, bus_id);
  237.             cout << "\nМаршруток: " << s << endl;
  238.         }
  239.         case 0:
  240.         {
  241.             break;
  242.         }
  243.         default:
  244.         {
  245.             break;
  246.         }
  247.         }
  248.     }
  249. }
  250.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement