-
Notifications
You must be signed in to change notification settings - Fork 1
/
EvalRPNClass.cs
35 lines (34 loc) · 1002 Bytes
/
EvalRPNClass.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Async
{
public class EvalRPNClass
{
public int EvalRPN(string[] tokens)
{
var value = new Stack<int>();
foreach (var s in tokens)
{
if (s != "+" && s != "-" && s != "*" && s != "/")
{
value.Push(Convert.ToInt32(s));
continue;
}
var a = value.Pop();
var b = value.Pop();
switch (s)
{
case "+": value.Push(b + a); break;
case "-": value.Push(b - a); break;
case "*": value.Push(b * a); break;
case "/": value.Push(b / a); break;
default: break;
}
}
return value.Pop();
}
}
}