fork download
  1. using System;
  2. using System.Diagnostics.Metrics;
  3. using System.Xml.Serialization;
  4.  
  5. namespace Лабораторна_робота_5
  6. {
  7. internal class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11.  
  12. int val = 1;
  13. do
  14. {
  15. Console.WriteLine("Choose the variant:{1-task5},{2-task25},{3-task38}");
  16. Console.WriteLine("If you want to stop function press 0");
  17. int choise = int.Parse(Console.ReadLine());
  18.  
  19.  
  20. switch (choise)
  21.  
  22. {
  23.  
  24. // Вибір функції
  25. case 1:
  26. Block_1();
  27. break;
  28. case 2:
  29. Block_2();
  30. break;
  31. case 3:
  32. Block_3();
  33. break;
  34.  
  35. case 0:
  36. val = 0;
  37. break;
  38.  
  39. default:
  40. Console.WriteLine("Невірний вибір. Спробуйте ще раз.");
  41. break;
  42. }
  43. } while (val != 0);
  44. }
  45.  
  46. static void Block_1()
  47. {
  48. int check = 1;
  49. do
  50.  
  51. {
  52. Console.WriteLine("Choose the type of cycle {1-for}, {2-while}, {3-do_while}");
  53. Console.WriteLine("If you want to stop block press 0");
  54. int choise = int.Parse(Console.ReadLine()); // Вибираємо серед варіантів
  55.  
  56. int counter = 0;
  57. int index = 1;
  58.  
  59.  
  60. switch (choise)
  61. {
  62. case 1:
  63. Console.Write("Введіть діапазон чисел: ");
  64. int n = int.Parse(Console.ReadLine());
  65.  
  66. for (int i = 1; i <= n; i++)
  67. {
  68. Console.Write($"Введіть число {i}: ");
  69. int x = int.Parse(Console.ReadLine());
  70. if (i % 2 != 0)
  71. counter += x;
  72. }
  73. Console.WriteLine(counter);
  74.  
  75. break;
  76.  
  77. case 2:
  78. Console.Write("Введіть діапазон чисел: ");
  79. int k = int.Parse(Console.ReadLine());
  80. while (index <= k)
  81. {
  82. Console.Write($"Введіть число {index}: ");
  83. int x = int.Parse(Console.ReadLine());
  84. if (index % 2 != 0)
  85. counter += x;
  86. index++;
  87. }
  88. Console.WriteLine(counter);
  89.  
  90. break;
  91.  
  92. case 3:
  93. Console.Write("Введіть діапазон чисел: ");
  94. int f = int.Parse(Console.ReadLine());
  95. do
  96. {
  97. Console.Write($"Введіть число {index}: ");
  98. int x = int.Parse(Console.ReadLine());
  99. if (index % 2 != 0)
  100. counter += x;
  101.  
  102. index++;
  103. } while (index <= f);
  104. Console.WriteLine(counter);
  105. break;
  106.  
  107. case 0:
  108. check = 0;
  109. break;
  110.  
  111. default:
  112. Console.WriteLine("Невірний вибір. Спробуйте ще раз.");
  113. break;
  114.  
  115. }
  116.  
  117.  
  118. } while (check != 0);
  119. }
  120.  
  121. static void Block_2()
  122. {
  123. int check = 1;
  124. Console.Write("Введіть перше число: ");
  125. int x = int.Parse(Console.ReadLine());
  126. do
  127. {
  128. Console.WriteLine("Choose the type of cycle {1-for}, {2-while}, {3-do_while}");
  129. Console.WriteLine("If you want to stop block press 0");
  130. int choise = int.Parse(Console.ReadLine());
  131.  
  132.  
  133. int min = x;
  134. int index = 1;
  135. int val = 2;
  136. int j = 1;
  137.  
  138. switch (choise)
  139. {
  140. case 1:
  141. for (int i = 2; i <= val; i++, val++)
  142. {
  143. Console.WriteLine($"Min = {min}, index = {index}");
  144. Console.Write("Введіть число: ");
  145. x = int.Parse(Console.ReadLine());
  146.  
  147. if (x == 0)
  148. break;
  149.  
  150. if (x < min)
  151. {
  152. min = x;
  153. index = i;
  154. }
  155. }
  156. Console.WriteLine($"Індекс найменшого числа: {index}");
  157. break;
  158.  
  159. case 2:
  160. while (x != 0)
  161. {
  162. if (min > x)
  163. {
  164. index = j;
  165. min = x;
  166. }
  167.  
  168. Console.Write("Введіть число: ");
  169. x = int.Parse(Console.ReadLine());
  170. j++;
  171. }
  172. Console.WriteLine($"Індекс найменшого числа: {index}");
  173. break;
  174.  
  175. case 3:
  176. do
  177. {
  178. if (min > x)
  179. {
  180. index = j;
  181. min = x;
  182. }
  183. Console.Write("Введіть число: ");
  184. x = int.Parse(Console.ReadLine());
  185. j++;
  186. } while (x != 0);
  187. Console.WriteLine($"Індекс найменшого числа: {index}");
  188. break;
  189.  
  190. case 0:
  191. check = 0;
  192. break;
  193.  
  194. default:
  195. Console.WriteLine("Невірний вибір. Спробуйте ще раз.");
  196. break;
  197.  
  198. }
  199. } while (check != 0);
  200. }
  201. static void Block_3()
  202.  
  203.  
  204. {
  205. int check = 1;
  206. Console.Write("Введіть перше число: ");
  207. int n = int.Parse(Console.ReadLine());
  208.  
  209. do
  210.  
  211. {
  212. Console.WriteLine("Choose the type of cycle {1-for}, {2-while}, {3-do_while}");
  213. Console.WriteLine("If you want to stop block press 0");
  214. int choise = int.Parse(Console.ReadLine());
  215.  
  216.  
  217. int first_num = 15;
  218. int total_sum = 15;
  219. int counter = 2;
  220.  
  221. switch (choise)
  222. {
  223. case 1:
  224. {
  225. for (int j = 1; j < n; j += 1)
  226. {
  227. first_num += 2;
  228.  
  229. if (counter < 3)
  230. {
  231. total_sum += first_num;
  232. Console.WriteLine($" plus total sum:{total_sum}");
  233. }
  234.  
  235.  
  236. if (counter == 3)
  237. {
  238. total_sum -= first_num;
  239. Console.WriteLine($" minus total sum:{total_sum}");
  240. counter = 0;
  241. }
  242.  
  243. counter += 1;
  244. }
  245. }
  246. Console.WriteLine(total_sum);
  247. break;
  248.  
  249. case 2:
  250. {
  251. while (counter <= n)
  252. {
  253. first_num += 2;
  254.  
  255. if (counter % 3 != 0)
  256. total_sum += first_num;
  257.  
  258. else
  259. total_sum -= first_num;
  260.  
  261. counter += 1;
  262. }
  263.  
  264. }
  265. Console.WriteLine(total_sum);
  266. break;
  267.  
  268. case 3:
  269. {
  270. do
  271. {
  272. first_num += 2;
  273.  
  274. if (counter % 3 != 0)
  275. total_sum += first_num;
  276.  
  277. else
  278. total_sum -= first_num;
  279.  
  280. counter += 1;
  281. } while (counter <= n);
  282. Console.WriteLine(total_sum);
  283. break;
  284. }
  285. case 0:
  286. check = 0;
  287. break;
  288.  
  289. default:
  290. Console.WriteLine("Невірний вибір. Спробуйте ще раз.");
  291. break;
  292. }
  293.  
  294. } while (check != 0);
  295. }
  296. }
  297. }
  298. //При роботі з даною лабораторною роботою я створив міні проект,
  299. //завдання якого полягає у написані великого об’єму коду в межах одного проекту.
  300. //Для цього ми використали блоки в яких записані 3 види циклів на кожен блок.
  301. //Велику роль відіграв оператор switch що виконав роль вказівника
  302. //(тобто переміщення між блоками функції та їхніми способами рішення).
  303. //Був використаний цикл While що створив можливість вибору одних і тих самих функцій декілька разів.
  304. //Загалом я закріпив 4 види циклів це While , for , do while , switch .
  305.  
Success #stdin #stdout 0.08s 30572KB
stdin
1
1
3
4
3
2
0
0
stdout
Choose the variant:{1-task5},{2-task25},{3-task38}
If you want to stop function press 0
Choose the type of cycle {1-for}, {2-while}, {3-do_while}
If you want to stop block press 0
Введіть діапазон чисел: Введіть число 1: Введіть число 2: Введіть число 3: 6
Choose the type of cycle {1-for}, {2-while}, {3-do_while}
If you want to stop block press 0
Choose the variant:{1-task5},{2-task25},{3-task38}
If you want to stop function press 0