Java Infix Calculator

What is Infix expression

Infix notation is the common arithmetic and logical formula notation, in which operators are written infix-style between the operands they act on, as the examples below:

 1+2
 3+4

A fully parenthesized infix arithmetic expression is an infix arithmetic expression where every operator and its arguments are contained in parentheses, as seen in following:

(2+3) 
(1+((2+3)∗(4∗5)))

How to Evaluat Infix Expressions Using Generic Stacks

Steps of Evaluating Infix with Fully Parenthesized [^1]

  • Push operands onto the operand stack.
  • Push operators onto the operator stack.
  • Ignore left parentheses.
  • On encountering a right parenthesis, pop an operator, pop the requisite number of operands, and push onto the operand stack the result of applying that operator to those operands.

Steps Simulation

((10 + 5) * 3)
3 10 5 + *
-> (
    skip
-> (
    skip
-> 10
    push 10 into operand stack
-> +
    push + into operator stack
-> 5
    push 10 into operand stack
-> )
    pop out +
    pop out 5, 10
    push 15 into operand stack
-> *
    push * into operator stack
-> )
    pop out *
    pop 15, 3
    push 45 into operand stack

infix_stack_calculator

Implementing Steps

  1. Copy the Stack code on Java Stack Implementation page.
  2. Save the Stack code as Stack.java
  3. Copy the following code and save as InfixEvaluation.java

Solution

/******************************************************************************
 *  Compilation:  javac InfixEvaluation.java
 *  Execution:    java InfixEvaluation arg
 *  Dependencies: Stack.java @link https://mrtan.me/post/20.html
 *  
 *  Java Infix expression Calculator.
 *
 *  % java InfixEvaluation "((1 + 2) / 2)"
 *  1.5
 * 
 ******************************************************************************/
class InfixEvaluation {
    public static void main(String[] args) {
        String[] strArr = args[0].split("");

        System.out.println(calculator(strArr));
    }

    public static double calculator(String[] strArr) {
        Stack<String> operators = new Stack<String>();
        Stack<Double> operands = new Stack<Double>();

        for(String str : strArr) {
            if (str.trim().equals("")) {
                continue;
            }

            switch(str) {
                case "(":
                    break;
                case ")":
                    double right = operands.pop();
                    double left = operands.pop();
                    String operator = operators.pop();
                    double value = 0;
                    switch(operator) {
                        case "+":
                            value = left + right;
                            break;
                        case "-":
                            value = left - right;
                            break;
                        case "*":
                            value = left * right;
                            break;
                        case "/":
                            value = left / right;
                            break;
                        default:
                            break;
                        }
                        operands.push(value);
                    break;
                case "+":
                case "-":
                case "*":
                case "/":
                    operators.push(str);
                    break;
                default:
                    operands.push(Double.parseDouble(str));
                    break;
            }
        }

        return operands.pop();
    }
}

Related Questions

4.3.14 Write a filter InfixToPostfix that converts an arithmetic expression from infix to postfix.