Advertisement
Guest User

Untitled

a guest
May 27th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. // Z3.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include<iostream>
  6.  
  7. using namespace std;
  8. struct spis
  9. {
  10. int inf;
  11. spis*prev, *next;
  12. };
  13. void create(spis**, spis**, int);
  14. void output(spis*);
  15. void del(spis**);
  16. void sort(spis*);
  17. void task_3(spis*);
  18. void merge(spis*, spis*);
  19. void zad3();
  20. void zad4();
  21. void zad5();
  22. int menu();
  23. void(*masf[])() = { zad3,zad4,zad5 };
  24. int main()
  25. {
  26. setlocale(0, "");
  27. while (1)
  28. (*masf[menu()])();
  29. system("pause");
  30. return 0;
  31. }
  32. int menu()
  33. {
  34. int flag;
  35. cout << "1 - Задание 3" << endl;
  36. cout << "2 - Задание 4" << endl;
  37. cout << "3 - Задание 5" << endl;
  38. cout << "4 - Выход" << endl;
  39. cin >> flag;
  40. if (flag == 4)
  41. exit(0);
  42. return flag - 1;
  43. }
  44. void zad3()
  45. {
  46. spis*begin = NULL, *end = NULL;
  47. int k, n;
  48. do
  49. {
  50. cout << "Введите число: ";
  51. cin >> n;
  52. create(&begin, &end, n);
  53. cout << "Продолжить? (1 - да , 0 - нет): ";
  54. cin >> k;
  55. } while (k != 0);
  56. cout << "Исходный список: ";
  57. output(begin);
  58. task_3(begin);
  59. del(&begin);
  60. }
  61. void zad4()
  62. {
  63. spis*begin1 = NULL, *end1 = NULL, *begin2 = NULL, *end2 = NULL;
  64. int k, n;
  65. cout << "Список 1:" << endl;
  66. do
  67. {
  68. cout << "Введите число: ";
  69. cin >> n;
  70. create(&begin1, &end1, n);
  71. cout << "Продолжить? (1 - да , 0 - нет): ";
  72. cin >> k;
  73. } while (k != 0);
  74. cout << "Список 2:" << endl;
  75. do
  76. {
  77. cout << "Введите число: ";
  78. cin >> n;
  79. create(&begin2, &end2, n);
  80. cout << "Продолжить? (1 - да , 0 - нет): ";
  81. cin >> k;
  82. } while (k != 0);
  83. cout << "Исходный список M1: ";
  84. sort(begin1);
  85. output(begin1);
  86. cout << "Исходный список M2: ";
  87. sort(begin2);
  88. output(begin2);
  89. cout << "Новый список M: ";
  90. del(&begin1);
  91. del(&begin2);
  92. }
  93. void zad5()
  94. {
  95.  
  96. }
  97. void create(spis**begin, spis**end, int n)
  98. {
  99. spis*t = new spis;
  100. if (*begin == NULL)
  101. {
  102. t->inf = n;
  103. t->next = t->prev = NULL;
  104. *begin = *end = t;
  105. }
  106. else
  107. {
  108. t->inf = n;
  109. t->prev = *end;
  110. t->next = NULL;
  111. (*end)->next = t;
  112. *end = t;
  113. }
  114. }
  115. void del(spis**begin)
  116. {
  117. spis *t;
  118. while (*begin != NULL)
  119. {
  120. t = *begin;
  121. *begin = (*begin)->next;
  122. delete t;
  123. }
  124. }
  125. void output(spis*begin2)
  126. {
  127. spis*t;
  128. t = begin2;
  129. while (t != NULL)
  130. {
  131. cout << t->inf << " ";
  132. t = t->next;
  133. }
  134. cout << endl;
  135. }
  136. void task_3(spis*begin)
  137. {
  138. spis*t;
  139. int sum = 0, n = 0;
  140. t = begin;
  141. while (t != NULL)
  142. {
  143. n++;
  144. sum += t->inf;
  145. t = t->next;
  146. }
  147. cout << "Сумма: " << sum / n << endl;
  148. }
  149. void sort(spis *begin)
  150. {
  151. spis *t;
  152. t = begin->next;
  153. int x;
  154. while (t)
  155. {
  156. x = t->inf;
  157. spis *b;
  158. b = t->prev;
  159. while (b != NULL && x < b->inf)
  160. {
  161. b->next->inf = b->inf;
  162. b = b->prev;
  163. }
  164. if (b == NULL) begin->inf = x;
  165. else b->next->inf = x;
  166. t = t->next;
  167. }
  168. }
  169. void merge(spis*begin1, spis*begin2)
  170. {
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement