voldmaks

Динамический массив

Dec 28th, 2020 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 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 ConsoleApp5
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] array = new int[0];
  14.             bool cycleWork = true;
  15.  
  16.             while (cycleWork)
  17.             {
  18.                 int sum = 0;
  19.                 string userInput = Console.ReadLine();
  20.  
  21.                 if (userInput == "exit")
  22.                 {
  23.                     cycleWork = false;
  24.                 }
  25.                 else if (userInput == "sum")
  26.                 {
  27.                     for (int i = 0; i < array.Length; i++)
  28.                     {
  29.                         sum += array[i];
  30.                     }
  31.                     Console.WriteLine(sum);
  32.                 }
  33.                 else
  34.                 {
  35.                     int[] tempArray = new int[array.Length + 1];
  36.                     for (int i = 0; i < array.Length; i++)
  37.                     {
  38.                         tempArray[i] = array[i];
  39.                     }
  40.                     tempArray[tempArray.Length - 1] = Convert.ToInt32(userInput);
  41.                     array = tempArray;
  42.                 }
  43.             }
  44.         }
  45.     }
  46. }
Add Comment
Please, Sign In to add comment