Advertisement
Alexandr0v

Untitled

Sep 21st, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. namespace _2.StackSum
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
  8.  
  9.             Stack<int> numbers = new Stack<int>();
  10.  
  11.             foreach (var num in input)
  12.             {
  13.                 numbers.Push(num);
  14.             }
  15.  
  16.             while (true)
  17.             {
  18.                 string[] arguments = Console.ReadLine().Split();
  19.                 string command = arguments[0];
  20.  
  21.                 if (command == "end")
  22.                     break;
  23.  
  24.                 if (command == "add")
  25.                 {
  26.                     int firstNumber = int.Parse(arguments[1]);
  27.                     int secondNumber = int.Parse(arguments[2]);
  28.  
  29.                     numbers.Push(firstNumber);
  30.                     numbers.Push(secondNumber);
  31.                 }
  32.                 else if (command == "remove")
  33.                 {
  34.                     int count = int.Parse(arguments[1]);
  35.  
  36.                     if (count <= numbers.Count)
  37.                     {
  38.                         for (int i = 0; i < count; i++)
  39.                         {
  40.                             numbers.Pop();
  41.                         }
  42.                     }
  43.                 }
  44.             }
  45.             Console.WriteLine($"Sum: {numbers.Sum()}");
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement