Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Task6
  8. {
  9. class Program
  10. {
  11. static int[] tempArray;
  12. static void Main(string[] args)
  13. {
  14. /*
  15. Сделайте 3 функции:
  16. Удаление элемента из массива.
  17. Добавление элемента в массив.
  18. Перенос одного массива в другой.
  19. */
  20. int[] array = new int[20];
  21. Random rand = new Random();
  22.  
  23. for (int i = 0; i < array.GetLength(0); i++)
  24.  
  25. array[i] = rand.Next(0, 100);
  26.  
  27. Console.WriteLine("Массив элементов:\n");
  28.  
  29. for (int i = 0; i < array.GetLength(0); i++)
  30.  
  31. {
  32. Console.Write(array[i] + " | ");
  33. }
  34.  
  35. Console.WriteLine("\nДействия с массивом:\ndelete - удалить элемент\nadd - добавить элемент\ntransfer - перенести в новый массив\nesc - выход\n\n");
  36.  
  37. string command = " ";
  38. bool b = true;
  39.  
  40. while (b)
  41. {
  42. Console.WriteLine("Введите команду: ");
  43. command = Console.ReadLine();
  44.  
  45. switch (command.ToLower())
  46. {
  47. case "add": //Как сделать чтобы при команде эдд всегда добавлялся плюс 1 эдемент? у меня добавляется один раз, а потом заменяется последний
  48. Console.Write("Напишите число, которое хотите добавить - ");
  49. int number = Convert.ToInt32(Console.ReadLine());
  50. AddNumber(array, number);
  51. array = new int[array.Length + 1];
  52. break;
  53. case "delete": //как сделать чтобы создавался новый массив без этого элемента? у меня получается, что удаляется значение (выводит 0)
  54. Console.Write("число, которое хотите удалить из массива - ");
  55. number = Convert.ToInt32(Console.ReadLine());
  56. DeleteNumber(array, number);
  57. break;
  58. case "transfer":
  59. Console.Write("введите название нового массива - ");
  60. string newArray = Console.ReadLine();
  61. Console.WriteLine("Новый массив ");
  62. ChangeArray(array);
  63. break;
  64. case "esc":
  65. b = false;
  66. Console.WriteLine("Пока!\n");
  67. break;
  68.  
  69. }
  70. }
  71. }
  72.  
  73. static void AddNumber(int[] array, int newNumber)
  74. {
  75. int[] tempArray = new int [array.Length + 1];
  76. for (int i = 0; i < array.Length; i++)
  77.  
  78. {
  79. tempArray[i] = array[i];
  80. if (i == array.Length - 1)
  81. {
  82. tempArray[i + 1] = newNumber;
  83. }
  84. }
  85.  
  86. for(int i = 0; i < tempArray.Length; i++)
  87. {
  88. Console.Write(tempArray[i] + " | ");
  89. //return tempArray[i];
  90. }
  91. Console.WriteLine();
  92. }
  93.  
  94. static void DeleteNumber (int[] array, int deletedNumber)
  95. {
  96. int[] tempArray = new int[array.Length];
  97. for (int i = 0; i < array.Length; i++)
  98.  
  99. {
  100. if (array[i] == deletedNumber)
  101. {
  102. i+=1;
  103. }
  104. tempArray[i] = array[i];
  105. }
  106.  
  107. for (int i = 0; i < tempArray.Length; i++)
  108. {
  109. Console.Write(tempArray[i] + " | ");
  110. }
  111. Console.WriteLine();
  112. }
  113. static void ChangeArray (int[] array)
  114. {
  115. for (int i = 0; i < array.Length; i++)
  116. {
  117. Random rand = new Random();
  118. int j1 = rand.Next(0, array.Length);
  119. int j2 = rand.Next(0, array.Length);
  120.  
  121. while (j1 == j2)
  122. {
  123. j1 = rand.Next(0, array.Length);
  124. j2 = rand.Next(0, array.Length);
  125. }
  126.  
  127. int temp = array[j1];
  128. array[j1] = array[j2];
  129. array[j2] = temp;
  130. }
  131.  
  132. for (int i = 0; i < array.Length; i++)
  133. {
  134. Console.Write(array[i] + " | ");
  135. }
  136. Console.WriteLine();
  137. }
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement