Advertisement
Andreyan_Boyadzhiev

Untitled

Jan 16th, 2019
82
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.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02._Stack_Sum
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var input = Console.ReadLine().Split().Select(int.Parse).ToArray();
  12.             Stack<int> stack = new Stack<int>(input);
  13.             var commandInfo = Console.ReadLine().ToLower();
  14.  
  15.             while (commandInfo != "end")
  16.             {
  17.                 var tokens = commandInfo.Split();
  18.                 var command = tokens[0].ToLower();
  19.                 if (command == "add")
  20.                 {
  21.  
  22.                     int number1 = int.Parse(tokens[1]);
  23.                     int number2 = int.Parse(tokens[2]);
  24.                     stack.Push(number1);
  25.                     stack.Push(number2);
  26.  
  27.                 }
  28.                 else if (command == "remove")
  29.                 {
  30.                     var countOfRemovedNums = int.Parse(tokens[1]);
  31.                     if (stack.Count < countOfRemovedNums)
  32.                     {
  33.                         commandInfo = Console.ReadLine().ToLower(); // това тук е пропуснато
  34.                         continue;
  35.                     }
  36.  
  37.                     for (int i = 0; i < countOfRemovedNums; i++)
  38.                     {
  39.                         stack.Pop();
  40.                     }
  41.                 }
  42.                 commandInfo = Console.ReadLine().ToLower();
  43.             }
  44.             var sum = stack.Sum();
  45.             Console.WriteLine($"Sum: {sum}");
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement