Advertisement
dxoraxs

Untitled

Apr 13th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. using System;
  2.  
  3. namespace homework
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             float[] numbers = new float[0];
  10.             string cinCommand="";
  11.             while (cinCommand!="sum")
  12.             {
  13.                 Console.Clear();
  14.                 Console.SetCursorPosition(0, 1);
  15.                 Console.WriteLine("Вводите числа. Если хотите суммировать их, введите \"sum\". Для сортировки введите \"sort\". Для выхода нажмите \"Enter\"");
  16.                 if (numbers.Length > 0)
  17.                 {
  18.                     Console.Write("Цифры в массиве: ");
  19.                     for (int i = 0; i < numbers.Length; i++)
  20.                         Console.Write(numbers[i] + ", ");
  21.                 }
  22.                 Console.SetCursorPosition(0, 0);
  23.                 cinCommand = Console.ReadLine();
  24.                 switch (cinCommand)
  25.                 {
  26.                     case "sum":
  27.                         float summ=0;
  28.                         for(int i=0;i<numbers.Length;i++)
  29.                         {
  30.                             summ += numbers[i];
  31.                         }
  32.                         Console.SetCursorPosition(0, 3);
  33.                         Console.WriteLine("Сумма = "+summ);
  34.                         break;
  35.                     case "sort":
  36.                         for (int j = 0; j < numbers.Length - 1; j++)
  37.                         {
  38.                             for (int k = j + 1; k < numbers.Length; k++)
  39.                             {
  40.                                 if (numbers[j] < numbers[k])
  41.                                 {
  42.                                     float temp = numbers[k];
  43.                                     numbers[k] = numbers[j];
  44.                                     numbers[j] = temp;
  45.                                 }
  46.                             }
  47.                         }
  48.                         break;
  49.                     case "":
  50.                         Console.SetCursorPosition(0, 3);
  51.                         return;
  52.                     default:
  53.                         bool flag = true;
  54.                         for (int i =0; i<cinCommand.Length;i++)
  55.                         {
  56.                             if (cinCommand[i] < '0' || cinCommand[i] > '9')
  57.                             {
  58.                                 flag = false;
  59.                                 Console.SetCursorPosition(0, 2);
  60.                                 Console.WriteLine("Введено некорректное число");
  61.                                 Console.ReadKey();
  62.                             }
  63.                         }
  64.                         if (!flag) break;
  65.                         float[] mas = new float[numbers.Length+1];
  66.                         for (int i =0; i< numbers.Length;i++)
  67.                         {
  68.                             mas[i] = numbers[i];
  69.                         }
  70.                         mas[mas.Length-1] = float.Parse(cinCommand);
  71.                         numbers = mas;
  72.                         break;
  73.                 }
  74.             }
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement