illiden

DynamicMassiv

Dec 3rd, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.46 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 DynamicMassiv
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] array = new int[0];
  14.             string inputNumbers = "";
  15.             bool exit = false;
  16.             string number = "";
  17.  
  18.             while (!exit)
  19.             {
  20.                 Console.Clear();
  21.                 Console.WriteLine("Доступные команды: \n\n" +
  22.                             "1. Mass - создать новый массив чисел \n\n" +
  23.                             "2. AddMass - добавить числа к массиву \n\n" +
  24.                             "3. SortMn - отсортировать от меньшего к большему \n\n" +
  25.                             "4. SortMx - отсортировать от большего к меньшему \n\n" +
  26.                             "5. Sum - вывести сумму элементов массива \n\n" +
  27.                             "6. Show - вывести элементы массива \n\n" +
  28.                             "7. Exit - выход\n");
  29.  
  30.                 switch (Console.ReadLine())
  31.                 {
  32.                     case "1":
  33.                     case "Mass":
  34.                         int[] newArray = new int[0];
  35.                         array = new int[0];
  36.                         Console.WriteLine("\nВведите числа массива через пробел: \n");
  37.                         inputNumbers = Console.ReadLine() + " ";
  38.  
  39.                         for (int i = 0; i < inputNumbers.Length; i++)
  40.                         {
  41.                             if (Convert.ToString(inputNumbers[i]) == " " && i == 0)
  42.                             {
  43.                                 continue;
  44.                             }
  45.                             if (Convert.ToString(inputNumbers[i]) != " ")
  46.                             {
  47.                                 number += inputNumbers[i];
  48.                                 continue;
  49.                             }
  50.                             if (Convert.ToString(inputNumbers[i]) == " " && Convert.ToString(inputNumbers[i - 1]) == " ")
  51.                             {
  52.                                 continue;
  53.                             }
  54.                             newArray = new int[array.Length + 1];
  55.                             for (int length = 0; length < array.Length; length++)
  56.                             {
  57.                                 newArray[length] = array[length];
  58.                             }
  59.                             newArray[newArray.Length - 1] = Convert.ToInt32(number);
  60.                             array = newArray;
  61.                             number = "";
  62.                         }
  63.                         break;
  64.                     case "2":
  65.                     case "AddMass":
  66.                         Console.WriteLine("\nВведите через пробел числа, которые вы хотите добавить в массив: \n");
  67.                         inputNumbers = Console.ReadLine() + " ";
  68.  
  69.                         for (int i = 0; i < inputNumbers.Length; i++)
  70.                         {
  71.                             if (Convert.ToString(inputNumbers[i]) == " " && i == 0)
  72.                             {
  73.                                 continue;
  74.                             }
  75.                             if (Convert.ToString(inputNumbers[i]) != " ")
  76.                             {
  77.                                 number += inputNumbers[i];
  78.                                 continue;
  79.                             }
  80.                             if (Convert.ToString(inputNumbers[i]) == " " && Convert.ToString(inputNumbers[i - 1]) == " ")
  81.                             {
  82.                                 continue;
  83.                             }
  84.                             newArray = new int[array.Length + 1];
  85.                             for (int length = 0; length < array.Length; length++)
  86.                             {
  87.                                 newArray[length] = array[length];
  88.                             }
  89.                             newArray[newArray.Length - 1] = Convert.ToInt32(number);
  90.                             array = newArray;
  91.                             number = "";
  92.                         }
  93.                         break;
  94.                     case "3":
  95.                     case "SortMn":
  96.                         bool sortMn = true;
  97.                         int reservMn = 0;
  98.                         while (sortMn)
  99.                         {
  100.                             sortMn = false;
  101.                             for (int i = 0; i < array.Length - 1; i++)
  102.                             {
  103.                                 if (array[i] > array[i + 1])
  104.                                 {
  105.                                     reservMn = array[i + 1];
  106.                                     array[i + 1] = array[i];
  107.                                     array[i] = reservMn;
  108.                                     sortMn = true;
  109.                                 }
  110.                             }
  111.                         }
  112.                         Console.WriteLine("\nМассив отсортирован от меньшего к большему\n");
  113.                         Console.ReadKey();
  114.                         break;
  115.                     case "4":
  116.                     case "SortMx":
  117.                         bool sortMx = true;
  118.                         int reservMx = 0;
  119.                         while (sortMx)
  120.                         {
  121.                             sortMx = false;
  122.                             for (int i = 0; i < array.Length - 1; i++)
  123.                             {
  124.                                 if (array[i] < array[i + 1])
  125.                                 {
  126.                                     reservMx = array[i + 1];
  127.                                     array[i + 1] = array[i];
  128.                                     array[i] = reservMx;
  129.                                     sortMx = true;
  130.                                 }
  131.                             }
  132.                         }
  133.                         Console.WriteLine("\nМассив отсортирован от большего к меньшему\n");
  134.                         Console.ReadKey();
  135.                         break;
  136.                     case "5":
  137.                     case "Sum":
  138.                         int sum = 0;
  139.                         for (int i = 0; i < array.Length; i++)
  140.                         {
  141.                             sum += array[i];
  142.                         }
  143.                         Console.WriteLine("\nСумма: " + sum);
  144.                         Console.ReadKey();
  145.                         break;
  146.                     case "6":
  147.                     case "Show":
  148.                         Console.WriteLine("\nВаши числа:\n");
  149.                         for (int i = 0; i < array.Length; i++)
  150.                         {
  151.                             Console.Write(array[i] + " ");
  152.                         }
  153.                         Console.ReadKey();
  154.                         break;
  155.                     case "7":
  156.                     case "Exit":
  157.                         exit = true;
  158.                         break;
  159.                     default:
  160.                         Console.WriteLine("Я не знаю такой команды!");
  161.                         break;
  162.  
  163.                 }
  164.             }
  165.            
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment