MiroslavKisov

SimpleCalculator

Jan 15th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SimpleCalculator
  6. {
  7.     public class StartUp
  8.     {
  9.         public static void Main()
  10.         {
  11.             string input = Console.ReadLine();
  12.             var reminder = input.Split(' ');
  13.             Stack<string> stack = new Stack<string>(reminder.Reverse());
  14.             int currentSum = int.Parse(stack.Pop());
  15.             while (stack.Count > 0)
  16.             {
  17.                 var op = stack.Pop();
  18.                 string currentNumber = stack.Pop();
  19.                 if (op == "+")
  20.                 {
  21.                     currentSum += int.Parse(currentNumber);
  22.                 }
  23.                 else
  24.                 {
  25.                     currentSum -= int.Parse(currentNumber);
  26.                 }
  27.             }
  28.             Console.WriteLine(currentSum);
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment