A suffix expression like this: inverse Polish expression
["2", "1", "+", "3", "*"]--((2 + 1) * 3)-9["4", "5", "+", "/", "+", "+", "4 + (5/13)" 4
The compiler prefers to compute pop two objects from the stack (stack) and then proceed to push back, then continue with this calculation.
The algorithm is also very simple
public static void Main (string[] args) {System.out.println (CALRPN ("4", "13", "5", "/", "+")); public static int Calrpn (String ... tokens) {int ret = 0; String operators = "+-*/"; stack<string> stack = new stack<string> (); for (String T:tokens) {if (!operators.contains (t)) {Stack.push (t) ;} else {int a = integer.valueof (Stack.pop ()); int b = integer.valueof (Stack.pop ()); int v = 0;if (t.equals ("+")) {v = a + b;} else if (t.equals ("-")) {v = a-B;} else if (t.equals ("*")) {v = a * b;} else if (t.equals ("/")) {v = A/b;} Stack.push (string.valueof (v));}} ret = integer.valueof (Stack.pop ()); return ret;}
Inverse Poland (Reverse Polish Notation)