@Test public void testEvalRpn() { Assert.assertEquals(123, evalRpn("123")); Assert.assertEquals(3, evalRpn("1,2,+")); Assert.assertEquals(60, evalRpn("1,2,+,4,5,x,x")); Assert.assertEquals(-4, evalRpn("1,1,+,-2,x")); Assert.assertEquals(0, evalRpn("4,6,/,2,/")); }
public static int evalRpn(String expr) { final String basicRpnPattern = "-?\\d+"; if (expr != null && expr.matches(basicRpnPattern)) { return Integer.valueOf(expr); } Stack<String> rpnStack = new Stack<String>(); String[] tokens = expr.split(","); int value = 0; for (String token : tokens) { if (token.matches(basicRpnPattern)) { rpnStack.push(token); } else { int operand2 = Integer.valueOf(rpnStack.pop()); int operand1 = Integer.valueOf(rpnStack.pop()); value = eval(operand1, operand2, token); rpnStack.push(String.valueOf(value)); } } value = Integer.valueOf(rpnStack.pop()); LOGGER.info("Expression \"{}\" evaluated to {}.", expr, value); return value; }
PracticeQuestionsCh8 { public static int evalRpn(String expr) { final String basicRpnPattern = "-?\\d+"; if (expr != null && expr.matches(basicRpnPattern)) { return Integer.valueOf(expr); } Stack<String> rpnStack = new Stack<String>(); String[] tokens = expr.split(","); int value = 0; for (String token : tokens) { if (token.matches(basicRpnPattern)) { rpnStack.push(token); } else { int operand2 = Integer.valueOf(rpnStack.pop()); int operand1 = Integer.valueOf(rpnStack.pop()); value = eval(operand1, operand2, token); rpnStack.push(String.valueOf(value)); } } value = Integer.valueOf(rpnStack.pop()); LOGGER.info("Expression \"{}\" evaluated to {}.", expr, value); return value; } }
PracticeQuestionsCh8 { public static int evalRpn(String expr) { final String basicRpnPattern = "-?\\d+"; if (expr != null && expr.matches(basicRpnPattern)) { return Integer.valueOf(expr); } Stack<String> rpnStack = new Stack<String>(); String[] tokens = expr.split(","); int value = 0; for (String token : tokens) { if (token.matches(basicRpnPattern)) { rpnStack.push(token); } else { int operand2 = Integer.valueOf(rpnStack.pop()); int operand1 = Integer.valueOf(rpnStack.pop()); value = eval(operand1, operand2, token); rpnStack.push(String.valueOf(value)); } } value = Integer.valueOf(rpnStack.pop()); LOGGER.info("Expression \"{}\" evaluated to {}.", expr, value); return value; }  }
PracticeQuestionsCh8 { public static int evalRpn(String expr) { final String basicRpnPattern = "-?\\d+"; if (expr != null && expr.matches(basicRpnPattern)) { return Integer.valueOf(expr); } Stack<String> rpnStack = new Stack<String>(); String[] tokens = expr.split(","); int value = 0; for (String token : tokens) { if (token.matches(basicRpnPattern)) { rpnStack.push(token); } else { int operand2 = Integer.valueOf(rpnStack.pop()); int operand1 = Integer.valueOf(rpnStack.pop()); value = eval(operand1, operand2, token); rpnStack.push(String.valueOf(value)); } } value = Integer.valueOf(rpnStack.pop()); LOGGER.info("Expression \"{}\" evaluated to {}.", expr, value); return value; }  static int evalRpn(String expr); }
PracticeQuestionsCh8 { public static int evalRpn(String expr) { final String basicRpnPattern = "-?\\d+"; if (expr != null && expr.matches(basicRpnPattern)) { return Integer.valueOf(expr); } Stack<String> rpnStack = new Stack<String>(); String[] tokens = expr.split(","); int value = 0; for (String token : tokens) { if (token.matches(basicRpnPattern)) { rpnStack.push(token); } else { int operand2 = Integer.valueOf(rpnStack.pop()); int operand1 = Integer.valueOf(rpnStack.pop()); value = eval(operand1, operand2, token); rpnStack.push(String.valueOf(value)); } } value = Integer.valueOf(rpnStack.pop()); LOGGER.info("Expression \"{}\" evaluated to {}.", expr, value); return value; }  static int evalRpn(String expr); static final Logger LOGGER; }