Advertisement
TwinFrame

EnterNumUser_Sum_Sort

Jan 15th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Clight_13_EnterUderWord
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. bool openMenu = true;
  10. int stepNumUser = 0;
  11. string currentWord = "";
  12. int currentNum;
  13. string[] wordsUser = new string[0];
  14. int[] numUser = new int[0];
  15. int tempElement;
  16.  
  17. while (openMenu == true)
  18. {
  19. Console.SetCursorPosition(0, 20);
  20. Console.Write(" sum - выводит сумму введенных значений\n" +
  21. " sort - выводит значения по возрастанию (только для чисел)\n" +
  22. " del - удаляет (обнуляет) ввод\n" +
  23. " exit - выход");
  24.  
  25. Console.SetCursorPosition(0, 0);
  26. Console.Write($"Введи своё {stepNumUser + 1} значение: ");
  27. currentWord = Console.ReadLine();
  28.  
  29. if (currentWord == "sum")
  30. {
  31. int sum = 0;
  32. for (int i = 0; i < numUser.Length; i++)
  33. {
  34. sum += numUser[i];
  35. }
  36. Console.Write("\n\nВаши значения: ");
  37. for (int i = 0; i < numUser.Length; i++)
  38. {
  39. Console.Write(numUser[i] + ", ");
  40. }
  41. Console.Write("\n\nСумма значений: " + sum);
  42. Console.ReadKey();
  43. }
  44. else
  45. {
  46. if (currentWord == "sort")
  47. {
  48. // Сортировка по возрастанию.
  49. int[] numSortUser = new int[numUser.Length];
  50. for (int i = 0; i < numSortUser.Length; i++)
  51. {
  52. numSortUser[i] = numUser[i];
  53. }
  54. int stepLenght = 0;
  55. for (int j = 0; j < numSortUser.Length - 1; j++)
  56. {
  57. for (int i = 0; i < numSortUser.Length - 1 - stepLenght; i++)
  58. {
  59. if (numSortUser[i] > numSortUser[i + 1])
  60. {
  61. tempElement = numSortUser[i + 1];
  62. numSortUser[i + 1] = numSortUser[i];
  63. numSortUser[i] = tempElement;
  64. }
  65. }
  66. stepLenght++;
  67. }
  68. Console.Write("\n\nВаши отсортированные значения: ");
  69. for (int i = 0; i < numSortUser.Length; i++)
  70. {
  71. Console.Write(numSortUser[i] + ", "); // Как избавиться от последней не нужной запятой при выводе sum?
  72. }
  73. Console.ReadKey();
  74. }
  75. else
  76. {
  77. if (currentWord == "del")
  78. {
  79. // Обнуление массива
  80. numUser = new int[0];
  81. stepNumUser = 0;
  82. }
  83. else
  84. {
  85. if (currentWord == "exit")
  86. {
  87. openMenu = false;
  88. }
  89. else
  90. {
  91. // Заполняем массив по мере ввода пользователем.
  92. currentNum = Convert.ToInt32(currentWord);
  93. int[] numTemp = new int[numUser.Length + 1];
  94. for (int i = 0; i < numUser.Length; i++)
  95. {
  96. numTemp[i] = numUser[i];
  97. }
  98. numTemp[numTemp.Length - 1] = currentNum;
  99. numUser = numTemp;
  100. stepNumUser++;
  101. }
  102. }
  103. }
  104. }
  105. Console.Clear();
  106. }
  107. Console.WriteLine("\n\nЖдем вас снова!");
  108. Console.Write("\nВаша последовательность напоследок: ");
  109. for (int i = 0; i < numUser.Length; i++)
  110. {
  111. Console.Write(numUser[i] + ", "); // Как избавиться от последней не нужной запятой при выводе sum?
  112. }
  113. Console.ReadKey();
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement