Advertisement
ilian_test

Untitled

Sep 20th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace Stack_Sum
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.            
  12.             Stack<int> numbers = new Stack<int>();
  13.             string input = Console.ReadLine();
  14.             string[] number = Regex.Split(input, @"\D+");
  15.             foreach (string value in number)
  16.             {
  17.                 if (!string.IsNullOrEmpty(value))
  18.                 {
  19.                     int i = int.Parse(value);
  20.                     numbers.Push(i);
  21.                 }
  22.             }
  23.  
  24.             while (true)
  25.             {
  26.                 string option = Console.ReadLine();
  27.                 switch (option.ToLower())
  28.                 {
  29.                 case "add":
  30.                         int addToStack = Int32.Parse(Console.ReadLine());
  31.                         for (int i = 0; i < 2; i++)
  32.                         {
  33.                             numbers.Push(addToStack);
  34.                         }
  35.                         continue;
  36.                 case "remove":
  37.                         int popItem = Convert.ToInt32(Console.ReadLine());
  38.                         if (popItem < numbers.Count)
  39.                         {
  40.                             for (int i = 0; i < popItem; i++)
  41.                             {
  42.                                 numbers.Pop();
  43.                             }
  44.                         }
  45.                  
  46.                             continue;
  47.                 case "end":
  48.                         int[] arr = numbers.ToArray();
  49.                         int sum = 0;
  50.                         for(int i = 0; i <arr.Length; i++)
  51.                         {
  52.                             sum += arr[i];  
  53.                         }
  54.                         Console.WriteLine(sum);
  55.  
  56.                         break;
  57.                 }              
  58.             }
  59.         }
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement