Advertisement
pol9na

Untitled

Mar 13th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 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 Study
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.Write("Введите количество элементов массива:\t");
  14.             bool exit=true;
  15.             int elementsCount = int.Parse(Console.ReadLine());
  16.             int[] myArray = new int[elementsCount];
  17.             int sum=0,temp;
  18.             string Command;
  19.             for (int i = 0; i < myArray.Length; i++)
  20.             {
  21.                 Console.Write($"Введите элемент массива под индексом {i}:\t");
  22.                 myArray[i] = int.Parse(Console.ReadLine());
  23.             }
  24.             Console.WriteLine("Исходный массив");
  25.             for (int i = 0; i < myArray.Length; i++)
  26.             {
  27.                 Console.Write(myArray[i]+" ");
  28.             }
  29.             Console.WriteLine();
  30.             while (exit) {
  31.                 Console.WriteLine("Введите команду");
  32.                 Console.WriteLine("Введите SUM для сложения элементов массива.Введите SORT для сортировки массива. Введите ESC для выхода из программы");
  33.                 Command = Console.ReadLine();
  34.                 switch (Command)
  35.                 {
  36.                     case "SUM":
  37.                         for (int i = 0; i < myArray.Length; i++)
  38.                         {
  39.                             sum += myArray[i];
  40.                         }
  41.                         Console.WriteLine(sum);
  42.                         break;
  43.                     case "SORT":
  44.                         for (int i = 0; i < myArray.Length; i++)
  45.                         {
  46.                             for (int j=i;j<myArray.Length;j++)
  47.                                 if (myArray[j] < myArray[i])
  48.                                 {
  49.                                     temp = myArray[i];
  50.                                     myArray[i] = myArray[j];
  51.                                     myArray[j] = temp;
  52.                                 }
  53.                         }
  54.                         Console.WriteLine();
  55.                         Console.Write("Массив после сортировки:\t");
  56.                         for (int i = 0; i < myArray.Length; i++)
  57.                         {
  58.                             Console.Write(myArray[i] + " ");
  59.                         }
  60.                         break;
  61.                      case "ESC":
  62.                         exit = false;
  63.                         break;
  64.                 }
  65.                 Console.ReadKey();
  66.             }
  67.     }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement