bogdan_obukhovskii

6 задача

Jun 21st, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. // Задание 6, Смольянинова У-195 😋
  2.  
  3. #include <iostream>
  4. #include <queue>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. //звонок
  10. struct Zvonok {
  11. int nomer; //кто звонил
  12. int time; //время в минутах
  13. Zvonok* next; //следующий звонок в списке
  14.  
  15. Zvonok(int n, int t)
  16. {
  17. nomer = n;
  18. time = t;
  19. next = nullptr;
  20. }
  21. };
  22.  
  23. //звонки абонента
  24. struct Zvonki {
  25. Zvonok* begin; //начало списка
  26. Zvonok* end; //конец списка
  27.  
  28. void print()
  29. {
  30. Zvonok* temp = begin;
  31.  
  32. while (temp)
  33. {
  34. cout << "Номер " << temp->nomer << " ; " << temp->time << "минут ; " << temp->time * 5 << "руб. стоил разговор " << endl;
  35. temp = temp->next;
  36. }
  37. delete temp;
  38. }
  39.  
  40. };
  41.  
  42. //абонент
  43. struct Abonent {
  44. int nomer; //номер
  45. string fio;
  46. Zvonki* zvonki;
  47. Abonent* next; //следующий абонент в списке
  48.  
  49. Abonent(int n, string f) {
  50. nomer = n; fio = f; next = nullptr;
  51. zvonki = new Zvonki;
  52. zvonki->begin = nullptr;
  53. zvonki->end = nullptr;
  54. }
  55. };
  56.  
  57. //список абонентов
  58. struct Abonenti {
  59. Abonent* begin; //начало списка
  60. Abonent* end; //конец списка
  61.  
  62. void print()
  63. {
  64. Abonent* temp = begin;
  65.  
  66. while (temp)
  67. {
  68. cout << "Номер " << temp->nomer << " ; ФИО " << temp->fio << endl;
  69. if (temp->zvonki)
  70. {
  71. cout << "Список звонков " << temp->nomer << endl;
  72. temp->zvonki->print();
  73. }
  74. temp = temp->next;
  75. }
  76. delete temp;
  77. }
  78.  
  79. Abonent* poisk(int num)
  80. {
  81. Abonent* temp = begin;
  82. while (temp)
  83. {
  84. if (temp->nomer == num)
  85. return temp;
  86. temp = temp->next;
  87. }
  88. delete temp;
  89. }
  90. };
  91.  
  92. void sortASC(Abonenti* list)
  93. {
  94. Abonent* begin = list->begin;
  95. Abonent* end = list->end;
  96.  
  97. Abonent* root = begin;
  98. Abonent* new_root = nullptr;
  99.  
  100. while (root)
  101. {
  102. Abonent* node = root;
  103. root = root->next;
  104.  
  105. if (!new_root || node->nomer < new_root->nomer)
  106. {
  107. node->next = new_root;
  108. new_root = node;
  109. }
  110. else
  111. {
  112. Abonent* current = new_root;
  113. while (current->next && !(node->nomer < current->next->nomer))
  114. {
  115. current = current->next;
  116. }
  117.  
  118. node->next = current->next;
  119. current->next = node;
  120. }
  121. }
  122. delete root;
  123. begin = new_root;
  124. }
  125.  
  126.  
  127. //добавить абонента в список
  128. void addAbonent(Abonent* temp, Abonenti* list)
  129. {
  130. if (!list->begin)
  131. {
  132. list->begin = temp;
  133. list->end = temp;
  134. return;
  135. }
  136. if (list->begin == list->end)
  137. {
  138. list->end = temp;
  139. list->begin->next = list->end;
  140. return;
  141. }
  142. list->end->next = temp;
  143. list->end = temp;
  144. }
  145.  
  146. //добавить звонок в список
  147. void addZvonok(Zvonok* temp, Zvonki* list)
  148. {
  149. if (!list->begin)
  150. {
  151. list->begin = temp;
  152. list->end = temp;
  153. return;
  154. }
  155. if (list->begin == list->end)
  156. {
  157. list->end = temp;
  158. list->begin->next = list->end;
  159. return;
  160. }
  161. list->end->next = temp;
  162. list->end = temp;
  163. }
  164.  
  165. int main()
  166. {
  167. setlocale(0, "rus");
  168. Abonent* a1 = new Abonent(7582, "Говфывфыв");
  169. Abonent* a2 = new Abonent(3421, "Говфывфыв");
  170. Abonent* a3 = new Abonent(8999, "Говфывфыв");
  171.  
  172. Abonenti* list = new Abonenti;
  173. list->begin = nullptr;
  174. list->end = nullptr;
  175.  
  176. addAbonent(a1, list);
  177. addAbonent(a2, list);
  178. addAbonent(a3, list);
  179.  
  180. cout << "1: сортировать" << endl;
  181. cout << "2: вывести список со всеми разговорами" << endl;
  182. cout << "3: ввести номер и врея разговора" << endl;
  183.  
  184. Abonent* result;
  185. Zvonok* temp;
  186.  
  187. while (true)
  188. {
  189. int input;
  190. cin >> input;
  191.  
  192. switch (input)
  193. {
  194. case 1:
  195. sortASC(list);
  196. break;
  197. case 2:
  198. list->print();
  199. break;
  200. case 3:
  201. cout << "введите номер и время" << endl;
  202. int num; int t;
  203. cin >> num >> t;
  204. temp = new Zvonok(num, t);
  205. result = list->poisk(num);
  206. if (result)
  207. addZvonok(temp, result->zvonki);
  208. break;
  209. }
  210.  
  211. system("pause");
  212. }
Advertisement
Add Comment
Please, Sign In to add comment