fork download
  1. using System;
  2.  
  3. namespace Lab2;
  4.  
  5. class Program
  6. {
  7. public static void Main(string[] _)
  8. {
  9. while (true)
  10. {
  11. if (SelectTask(RequestTask()) is Action task) task();
  12. else break;
  13. }
  14. }
  15.  
  16. private static int RequestTask()
  17. {
  18. const string requestText =
  19. "Select task:\n" +
  20. "0 - exit\n" +
  21. "1 - task 1 (variant 15)\n" +
  22. "2 - task 2 (variant 34)\n" +
  23. "3 - task 3 (variant 38)\n" +
  24. "4 - additional 1\n" +
  25. "5 - additional 2\n" +
  26. "6 - additional 3\n" +
  27. "7 - additional 4\n" +
  28. "> ";
  29.  
  30. Console.Write(requestText);
  31. return IO.ReadInt();
  32. }
  33.  
  34. private static Action? SelectTask(int choice)
  35. {
  36. return choice switch
  37. {
  38. 0 => null,
  39. 1 => Tasks.Task1,
  40. 2 => Tasks.Task2,
  41. 3 => Tasks.Task3,
  42. 4 => Tasks.DoAdditional1,
  43. 5 => Tasks.DoAdditional2,
  44. 6 => Tasks.DoAdditional3,
  45. 7 => Tasks.DoAdditional4,
  46. _ => () => { Console.WriteLine($"Invalid task: {choice}"); }
  47. };
  48. }
  49. }
  50.  
  51. static class IO
  52. {
  53. public static int ReadInt(string message = "")
  54. {
  55. if (message != "") Console.Write($"{message}\n> ");
  56.  
  57. return TryReadInt() switch
  58. {
  59. int value => value,
  60. _ => ReadInt(message),
  61. };
  62. }
  63.  
  64. public static int? TryReadInt()
  65. {
  66. var input = Console.ReadLine() ?? "";
  67.  
  68. try
  69. {
  70. return int.Parse(input);
  71. }
  72. catch
  73. {
  74. Console.WriteLine($"Invalid input: '{input}'");
  75. return null;
  76. }
  77. }
  78.  
  79. public static double ReadDouble(string message = "")
  80. {
  81. if (message != "") Console.Write($"{message}\n> ");
  82.  
  83. return TryReadDouble() switch
  84. {
  85. double value => value,
  86. _ => ReadDouble(message),
  87. };
  88. }
  89.  
  90. public static double? TryReadDouble()
  91. {
  92. var input = Console.ReadLine() ?? "";
  93.  
  94. try
  95. {
  96. return double.Parse(input);
  97. }
  98. catch
  99. {
  100. Console.WriteLine($"Invalid input: '{input}'");
  101. return null;
  102. }
  103. }
  104. }
  105.  
  106. enum Loop
  107. {
  108. For,
  109. While,
  110. DoWhile,
  111. GoTo,
  112. }
  113.  
  114. static class LoopHelper
  115. {
  116. public static Loop RequestFromUser()
  117. {
  118. const string requestText =
  119. "Select loop:\n" +
  120. "1 - for\n" +
  121. "2 - while\n" +
  122. "3 - do while\n" +
  123. "> ";
  124.  
  125. Console.Write(requestText);
  126. return IO.TryReadInt() switch
  127. {
  128. 1 => Loop.For,
  129. 2 => Loop.While,
  130. 3 => Loop.DoWhile,
  131. _ => RequestFromUser(),
  132. };
  133. }
  134. }
  135.  
  136. class Tasks
  137. {
  138. // Варіант 15
  139. //
  140. // Дана послідовність з n цілих чисел. Знайти кількість елементів цієї
  141. // послідовності, кратних її першому елементу.
  142. public static void Task1()
  143. {
  144. static void TaskHelper(int first, ref int count)
  145. {
  146. int num = IO.ReadInt();
  147. if (num % first == 0) count++;
  148. }
  149.  
  150. const bool isFirstNumCounts = true;
  151.  
  152. var loop = LoopHelper.RequestFromUser();
  153. int n = IO.ReadInt("Input number of the elements");
  154.  
  155. if (n <= 0)
  156. {
  157. Console.WriteLine("N must be natural");
  158. return;
  159. }
  160.  
  161. int first = IO.ReadInt("Input elements...");
  162. int count = isFirstNumCounts ? 1 : 0;
  163. int i = 1;
  164.  
  165. switch (loop)
  166. {
  167. case Loop.For:
  168. for (; i < n; ++i) TaskHelper(first, ref count);
  169. break;
  170.  
  171. case Loop.While:
  172. while (i < n)
  173. {
  174. TaskHelper(first, ref count);
  175. ++i;
  176. }
  177. break;
  178.  
  179. case Loop.DoWhile:
  180. if (i < n)
  181. {
  182. do
  183. {
  184. TaskHelper(first, ref count);
  185. ++i;
  186. } while (i < n);
  187. }
  188. break;
  189.  
  190. case Loop.GoTo:
  191. begin:
  192. if (i < n)
  193. {
  194. TaskHelper(first, ref count);
  195. ++i;
  196. goto begin;
  197. }
  198. break;
  199. }
  200.  
  201. Console.WriteLine($"Count is {count}");
  202. }
  203.  
  204. // Варіант 34
  205. //
  206. // Дана послідовність цілих чисел, за якою слідує 0. Визначити, яких чисел
  207. // в цій послідовності більше: додатних чи від’ємних.
  208. public static void Task2()
  209. {
  210. static bool TaskHelper(ref int positives, ref int negatives)
  211. {
  212. var num = IO.ReadInt();
  213. if (num == 0) return false;
  214. if (num > 0) ++positives;
  215. else ++negatives;
  216. return true;
  217. }
  218.  
  219. var negativeCount = 0;
  220. var positiveCount = 0;
  221. var loop = LoopHelper.RequestFromUser();
  222.  
  223. Console.WriteLine("Input elements...");
  224.  
  225. switch (loop)
  226. {
  227. case Loop.For:
  228. for (; ; )
  229. {
  230. if (!TaskHelper(ref positiveCount, ref negativeCount))
  231. {
  232. break;
  233. }
  234. }
  235. break;
  236.  
  237. case Loop.While:
  238. while (true)
  239. {
  240. if (!TaskHelper(ref positiveCount, ref negativeCount))
  241. {
  242. break;
  243. }
  244. }
  245. break;
  246.  
  247. case Loop.DoWhile:
  248. bool isNotZero;
  249. do
  250. {
  251. isNotZero = TaskHelper(ref positiveCount, ref negativeCount);
  252. }
  253. while (isNotZero);
  254. break;
  255.  
  256. case Loop.GoTo:
  257. begin:
  258. if (TaskHelper(ref positiveCount, ref negativeCount))
  259. {
  260. goto begin;
  261. }
  262. break;
  263. }
  264.  
  265. Console.WriteLine(
  266. negativeCount == positiveCount
  267. ? $"Positive & Negative numbers count is equal ({positiveCount} = {negativeCount})"
  268. : positiveCount > negativeCount
  269. ? $"Positive numbers count is higher ({positiveCount} > {negativeCount})"
  270. : $"Negative numbers count is higher ({positiveCount} < {negativeCount})"
  271. );
  272. }
  273.  
  274. // Варіант 38
  275. //
  276. // S = 15 + 17 – 19 + 21 + 23 – 25 + ..., де всього n доданків.
  277. public static void Task3()
  278. {
  279. const int x0 = 15;
  280.  
  281. static void TaskHelper(int i, ref int result)
  282. {
  283. var xi = x0 + (i - 1) * 2;
  284. result += (i % 3 == 0) ? -xi : xi;
  285. }
  286.  
  287. var loop = LoopHelper.RequestFromUser();
  288. var n = IO.ReadInt("Input N");
  289. var result = 0;
  290. int i = 1;
  291.  
  292. switch (loop)
  293. {
  294. case Loop.For:
  295. for (; i <= n; ++i)
  296. {
  297. TaskHelper(i, ref result);
  298. }
  299. break;
  300.  
  301. case Loop.While:
  302. while (i <= n)
  303. {
  304. TaskHelper(i, ref result);
  305. ++i;
  306. }
  307. break;
  308.  
  309. case Loop.DoWhile:
  310. if (i <= n)
  311. {
  312.  
  313. do
  314. {
  315. TaskHelper(i, ref result);
  316. ++i;
  317. } while (i <= n);
  318. }
  319. break;
  320.  
  321. case Loop.GoTo:
  322. begin:
  323. if (i <= n)
  324. {
  325. TaskHelper(i, ref result);
  326. ++i;
  327. goto begin;
  328. }
  329. break;
  330. }
  331.  
  332. Console.WriteLine(result);
  333. }
  334.  
  335. // Додаткове 1
  336. //
  337. // S = √(3 + √(6 + √(9 + ... √(3n))))
  338. public static void DoAdditional1()
  339. {
  340. static double PrintExpr(System.IO.TextWriter? w, double num, double max)
  341. {
  342. if (num >= max)
  343. {
  344. w?.Write($"√{num}");
  345. return Math.Sqrt(num);
  346. }
  347.  
  348. w?.Write($"√({num} + ");
  349. var next = PrintExpr(w, num + 3.0, max);
  350. w?.Write(")");
  351.  
  352. return Math.Sqrt(num + next);
  353. }
  354.  
  355. static void TaskHelper(int num, ref double result)
  356. {
  357. result = num + Math.Sqrt(result);
  358. }
  359.  
  360. var loop = LoopHelper.RequestFromUser();
  361. var n = IO.ReadInt("Input N");
  362. var result = 0.0;
  363. var num = 3 * n;
  364.  
  365. switch (loop)
  366. {
  367. case Loop.For:
  368. for (; num >= 3; num -= 3)
  369. {
  370. TaskHelper(num, ref result);
  371. }
  372. break;
  373.  
  374. case Loop.While:
  375. while (num >= 3)
  376. {
  377. TaskHelper(num, ref result);
  378. num -= 3;
  379. }
  380. break;
  381.  
  382. case Loop.DoWhile:
  383. if (num >= 3)
  384. {
  385. do
  386. {
  387. TaskHelper(num, ref result);
  388. num -= 3;
  389. }
  390. while (num >= 3);
  391. }
  392. break;
  393.  
  394. case Loop.GoTo:
  395. begin:
  396. if (num >= 3)
  397. {
  398. TaskHelper(num, ref result);
  399. num -= 3;
  400. goto begin;
  401. }
  402. break;
  403. }
  404.  
  405. // var buf = new StringWriter();
  406. // var recResult = TaskHelper(buf, 3.0, 3.0 * n);
  407. // Console.WriteLine(buf);
  408.  
  409. result = Math.Sqrt(result);
  410. Console.WriteLine(result);
  411. }
  412.  
  413. // Додаткове 2
  414. //
  415. // S = sin(x + sin(2x − sin(3x + sin(4x + sin(5x − sin(6x + ...) ...)
  416. //
  417. // * до sin(nx) включно
  418. // * на кожні три рази двічі відбувається додавання, один раз віднімання
  419. public static void DoAdditional2()
  420. {
  421. static void TaskHelper(int i, double x, ref double result)
  422. {
  423. result = (i + 1) % 3 == 0
  424. ? i * x - Math.Sin(result)
  425. : i * x + Math.Sin(result);
  426. }
  427.  
  428. var loop = LoopHelper.RequestFromUser();
  429. var n = IO.ReadInt("Input N");
  430. var x = IO.ReadDouble("Input X");
  431. var result = 0.0;
  432. var i = n;
  433.  
  434. switch (loop)
  435. {
  436. case Loop.For:
  437. for (; i >= 1; --i)
  438. {
  439. TaskHelper(i, x, ref result);
  440. }
  441. break;
  442.  
  443. case Loop.While:
  444. while (i >= 1)
  445. {
  446. TaskHelper(i, x, ref result);
  447. --i;
  448. }
  449. break;
  450.  
  451. case Loop.DoWhile:
  452. if (i >= 1)
  453. {
  454. do
  455. {
  456. TaskHelper(i, x, ref result);
  457. --i;
  458. }
  459. while (i >= 1);
  460. }
  461. break;
  462.  
  463. case Loop.GoTo:
  464. begin:
  465. if (i >= 1)
  466. {
  467. TaskHelper(i, x, ref result);
  468. --i;
  469. goto begin;
  470. }
  471. break;
  472. }
  473.  
  474. result = Math.Sin(result);
  475. Console.WriteLine(result);
  476. }
  477.  
  478. // Додаткове 3
  479. //
  480. // S = sin(x + cos(2x + sin(3x + cos(4x + sin(5x + cos(6x + ...) ...)
  481. //
  482. // * до sin(nx) чи cos(nx) включно
  483. // * sin(nx) чи cos(nx) залежить від парності n
  484. public static void DoAdditional3()
  485. {
  486. static void TaskHelper(int i, double x, ref double? result)
  487. {
  488. var tmp = i % 2 == 0
  489. ? (result is double a ? Math.Sin(a) : 0)
  490. : (result is double b ? Math.Cos(b) : 0);
  491. result = i * x + tmp;
  492. }
  493.  
  494. var loop = LoopHelper.RequestFromUser();
  495. var n = IO.ReadInt("Input N");
  496. var x = IO.ReadDouble("Input X");
  497. var i = n;
  498. double? result = null;
  499.  
  500. switch (loop)
  501. {
  502. case Loop.For:
  503. for (; i >= 1; --i)
  504. {
  505. TaskHelper(i, x, ref result);
  506. }
  507. break;
  508.  
  509. case Loop.While:
  510. while (i >= 1)
  511. {
  512. TaskHelper(i, x, ref result);
  513. --i;
  514. }
  515. break;
  516.  
  517. case Loop.DoWhile:
  518. if (i >= 1)
  519. {
  520. do
  521. {
  522. TaskHelper(i, x, ref result);
  523. --i;
  524. }
  525. while (i >= 1);
  526. }
  527. break;
  528.  
  529. case Loop.GoTo:
  530. begin:
  531. if (i >= 1)
  532. {
  533. TaskHelper(i, x, ref result);
  534. --i;
  535. goto begin;
  536. }
  537. break;
  538. }
  539.  
  540. result = result is double r ? Math.Sin(r) : 0;
  541. Console.WriteLine(result);
  542. }
  543.  
  544. // Додаткове 4
  545. //
  546. // S = sin(x + cos(2x − sin(3x + cos(4x + sin(5x − cos(6x + ...) ...)
  547. //
  548. // * до sin(nx) чи cos(nx) включно, sin(nx) чи cos(nx) залежить від парності n
  549. // * на кожні три рази двічі відбувається додавання, один раз віднімання
  550. public static void DoAdditional4()
  551. {
  552. static void TaskHelper(int i, double x, ref double? result)
  553. {
  554. var tmp = i % 2 == 0
  555. ? (result is double a ? Math.Sin(a) : 0)
  556. : (result is double b ? Math.Cos(b) : 0);
  557. result = (i + 1) % 3 == 0
  558. ? i * x - tmp
  559. : i * x + tmp;
  560. }
  561.  
  562. var loop = LoopHelper.RequestFromUser();
  563. var n = IO.ReadInt("Input N");
  564. var x = IO.ReadDouble("Input X");
  565. var i = n;
  566. double? result = null;
  567.  
  568. switch (loop)
  569. {
  570. case Loop.For:
  571. for (; i >= 1; --i)
  572. {
  573. TaskHelper(i, x, ref result);
  574. }
  575. break;
  576.  
  577. case Loop.While:
  578. while (i >= 1)
  579. {
  580. TaskHelper(i, x, ref result);
  581. --i;
  582. }
  583. break;
  584.  
  585. case Loop.DoWhile:
  586. if (i >= 1)
  587. {
  588. do
  589. {
  590. TaskHelper(i, x, ref result);
  591. --i;
  592. }
  593. while (i >= 1);
  594. }
  595. break;
  596.  
  597. case Loop.GoTo:
  598. begin:
  599. if (i >= 1)
  600. {
  601. TaskHelper(i, x, ref result);
  602. --i;
  603. goto begin;
  604. }
  605. break;
  606. }
  607.  
  608. result = result is double r ? Math.Sin(r) : 0;
  609. Console.WriteLine(result);
  610. }
  611. }
  612.  
  613.  
Success #stdin #stdout 0.08s 30340KB
stdin
Standard input is empty
stdout
Select task:
0 - exit
1 - task 1 (variant 15)
2 - task 2 (variant 34)
3 - task 3 (variant 38)
4 - additional 1
5 - additional 2
6 - additional 3
7 - additional 4
>