// We need this for the Stack class. import java.util.*; class ExprTreeNode { ExprTreeNode left, right; // The usual pointers. boolean isLeaf; // Is this a leaf? int value; // If so, we'll store the number here. char op; // If not, we need to know which operator. } class ExpressionTree { ExprTreeNode root; public void parseExpression (String expr) { root = parse (expr); } ExprTreeNode parse (String expr) { ExprTreeNode node = new ExprTreeNode (); // Note: expr.charAt(0) is a left paren. // First, find the matching right paren. int m = findMatchingRightParen (expr, 1); String leftExpr = expr.substring (1, m+1); // Bottom-out condition: if (m == expr.length()-1) { // It's at the other end => this is a leaf. String operandStr = expr.substring (1, expr.length()-1); node.isLeaf = true; node.value = getValue (operandStr); return node; } // Otherwise, there's a second operand and an operator. // Find the left paren to match the rightmost right paren. int n = findMatchingLeftParen (expr, expr.length()-2); String rightExpr = expr.substring (n, expr.length()-1); // Recursively parse the left and right substrings. node.left = parse (leftExpr); node.right = parse (rightExpr); node.op = expr.charAt (m+1); return node; } int findMatchingRightParen (String s, int leftPos) { Stack stack = new Stack(); stack.push (s.charAt(leftPos)); for (int i=leftPos+1; i stack = new Stack(); // Use the Scanner class to help us extract numbers or operators: Scanner scanner = new Scanner (postfixExpr); while (scanner.hasNext()) { if (scanner.hasNextInt()) { // It's an operand => push it on the stack. int N = scanner.nextInt(); stack.push (N); } else { // It's an operator => apply the operator to the top two operands String opStr = scanner.next(); int b = stack.pop(); // Note: b is popped first. int a = stack.pop(); if (opStr.equals("+")) { stack.push (a+b); } else if (opStr.equals("-")) { stack.push (a-b); } else if (opStr.equals("*")) { stack.push (a*b); } else if (opStr.equals("/")) { stack.push (a/b); } } } // end-while // Result is on top. return stack.pop(); } } public class ExpressionParser { public static void main (String[] argv) { String s = "((1)+(2))"; test (s); s = "(((1)+(2))*(3))"; test (s); s = "(((35)-((3)*((3)+(2))))/(4))"; test (s); } static void test (String s) { ExpressionTree expTree = new ExpressionTree (); expTree.parseExpression (s); int v = expTree.computeValue (); System.out.println (s + " = " + v); String postStr = expTree.convertToPostfix (); PostfixEvaluator postfixEval = new PostfixEvaluator(); int p = postfixEval.compute (postStr); System.out.println ("Postfix: " + postStr + " postfix eval=" + p); } }