-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRPN.cs
43 lines (41 loc) · 1.2 KB
/
RPN.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
36
37
38
39
40
41
42
43
using System;
using System.Collections.Generic;
using System.Text;
namespace Abstraction
{
static class RPN
{
static public int Converter(string input)
{
stack<int> myStack = new stack<int>(10);
foreach ( string i in input.Split(' '))
{
try
{
myStack.Push(Convert.ToInt32(i));
}
catch
{
int num2 = Convert.ToInt32(myStack.Pop());
int num1 = Convert.ToInt32(myStack.Pop());
switch (i)
{
case "+":
myStack.Push(num1 + num2);
break;
case "-":
myStack.Push(num1 - num2);
break;
case "/":
myStack.Push(num1 / num2);
break;
case "*":
myStack.Push(num1 * num2);
break;
}
}
}
return myStack.Pop();
}
}
}