MeGaLiGhT14

3.6 Расширение массива

Oct 11th, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Lesson3_6
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             var array = new int[0];
  10.             var arrayComands = new string[3] { "sum" , "sort" , "exit" };
  11.  
  12.             Console.WriteLine($"Вывод суммы: \"{arrayComands[0]}\"\n" +
  13.                               $"Вывод чисел в порядке убывания: \"{arrayComands[1]}\"\n" +
  14.                               $"Выход из программы: \"{arrayComands[2]}\"\n");
  15.  
  16.             while(true)
  17.             {
  18.                 Console.Write("Введите число(команду): ");
  19.                 string userInput = Console.ReadLine().ToLower();
  20.  
  21.                 if (userInput == arrayComands[0])
  22.                 {
  23.                     int sumNumbers = 0;
  24.                     for (int i = 0; i < array.Length; i++)
  25.                     {
  26.                         sumNumbers += array[i];
  27.                     }
  28.  
  29.                     Console.WriteLine($"\nСумма чисел: {sumNumbers} \n");
  30.                 }
  31.                 else if (userInput == arrayComands[1])
  32.                 {
  33.                     Console.WriteLine($"\nПорядок чисел по убыванию:");
  34.  
  35.                     int reserveNumber;
  36.                     for (int j = 1; j < array.Length; j++)
  37.                     {
  38.                         for (int i = 0; i < array.Length - j; i++)
  39.                         {
  40.                             if (array[i] < array[i + 1])
  41.                             {
  42.                                 reserveNumber = array[i];
  43.                                 array[i] = array[i + 1];
  44.                                 array[i + 1] = reserveNumber;
  45.                             }
  46.                         }
  47.                     }
  48.  
  49.                     for (int i = 0; i < array.Length; i++)
  50.                     {
  51.                         Console.Write(array[i] + " ");
  52.                     }
  53.                     Console.WriteLine("\n");
  54.                 }
  55.                 else if (userInput == arrayComands[2])
  56.                 {
  57.                     break;
  58.                 }
  59.                 else
  60.                 {
  61.                     int inputNumber = Convert.ToInt32(userInput);
  62.  
  63.                     var arrayNew = new int[array.Length + 1];
  64.                     for (int i = 0; i < array.Length; i++)
  65.                     {
  66.                         arrayNew[i] = array[i];
  67.                     }
  68.                     arrayNew[array.Length] = inputNumber;
  69.                     array = arrayNew;
  70.                 }
  71.             }
  72.         }
  73.     }
  74. }
Add Comment
Please, Sign In to add comment