Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- namespace Stack_Sum
- {
- class Program
- {
- static void Main(string[] args)
- {
- Stack<int> numbers = new Stack<int>();
- string input = Console.ReadLine();
- string[] number = Regex.Split(input, @"\D+");
- foreach (string value in number)
- {
- if (!string.IsNullOrEmpty(value))
- {
- int i = int.Parse(value);
- numbers.Push(i);
- }
- }
- while (true)
- {
- string option = Console.ReadLine();
- switch (option.ToLower())
- {
- case "add":
- int addToStack = Int32.Parse(Console.ReadLine());
- for (int i = 0; i < 2; i++)
- {
- numbers.Push(addToStack);
- }
- continue;
- case "remove":
- int popItem = Convert.ToInt32(Console.ReadLine());
- if (popItem < numbers.Count)
- {
- for (int i = 0; i < popItem; i++)
- {
- numbers.Pop();
- }
- }
- continue;
- case "end":
- int[] arr = numbers.ToArray();
- int sum = 0;
- for(int i = 0; i <arr.Length; i++)
- {
- sum += arr[i];
- }
- Console.WriteLine(sum);
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement