Advertisement
Vlad_Savitskiy

Dynamic array

Apr 8th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace CSLightFirst
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             bool isExit = false;
  11.             int[] data = new int[1];
  12.             int currentIndex = 0;
  13.             string userInput;
  14.  
  15.             while (!isExit)
  16.             {
  17.                 Console.Write("Введите число или команду (sum и exit): ");
  18.                 userInput = Console.ReadLine();
  19.                 switch (userInput)
  20.                 {
  21.                     case "sum":
  22.                         Console.WriteLine($"\nСумма массива: {data.Sum()}\n");
  23.                         break;
  24.                     case "exit":
  25.                         isExit = true;
  26.                         break;
  27.                     default:
  28.                         if (currentIndex > data.Length - 1)
  29.                         {
  30.                             int[] temp = new int[data.Length + 3];
  31.                             for (int i = 0; i < data.Length; i++)
  32.                                 temp[i] = data[i];
  33.  
  34.                             temp[currentIndex] = int.Parse(userInput);
  35.                             data = temp;
  36.                             currentIndex++;
  37.                         }
  38.                         else
  39.                         {
  40.                             data[currentIndex] = int.Parse(userInput);
  41.                             currentIndex++;
  42.                         }
  43.                         break;
  44.                 }
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement