Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/472553834980450305
deleted 5 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Java implementation to calculate Calculating infix expression with no parenthesis

This is my javaJava implementation (O(n)\$O(n)\$) about how to calculate infix expression with no parenthesis. 

For example, when we input "3 + 4 * 4 / 8", we will get a double type answer is 5.0. Here to make the algorithm simple, we don't consider any parenthesis in the expression. Below is my code. 

I think my implementation is kind of tedious and long. Can anyone give me some advice to makeon making it more cleancleaner?

Java implementation to calculate infix expression with no parenthesis

This is my java implementation (O(n)) about how to calculate infix expression with no parenthesis. For example, when we input "3 + 4 * 4 / 8", we will get a double type answer is 5.0. Here to make the algorithm simple, we don't consider any parenthesis in the expression. Below is my code. I think my implementation is kind of tedious long. Can anyone give me some advice to make it more clean?

Calculating infix expression with no parenthesis

This is my Java implementation (\$O(n)\$) about how to calculate infix expression with no parenthesis. 

For example, when we input "3 + 4 * 4 / 8", we will get a double type answer is 5.0. Here to make the algorithm simple, we don't consider any parenthesis in the expression. 

I think my implementation is kind of tedious and long. Can anyone give me some advice on making it cleaner?

Source Link
JoJo
  • 213
  • 2
  • 5

Java implementation to calculate infix expression with no parenthesis

This is my java implementation (O(n)) about how to calculate infix expression with no parenthesis. For example, when we input "3 + 4 * 4 / 8", we will get a double type answer is 5.0. Here to make the algorithm simple, we don't consider any parenthesis in the expression. Below is my code. I think my implementation is kind of tedious long. Can anyone give me some advice to make it more clean?

public double computeInfixExpr(String input) {
        String[] expr = input.split(" ");
        int i = 0;
        double operLeft = Integer.valueOf(expr[i++]);
        while (i < expr.length) {
            String operator = expr[i++];
            double operRight = Double.valueOf(expr[i++]);
            switch (operator) {
                case "*":
                    operLeft = operLeft * operRight;
                    break;
                case "/":
                    operLeft = operLeft / operRight;
                    break;
                case "+":
                    while (i < expr.length) {
                        String operator2 = expr[i++];
                        if (operator2.equals("+") || operator2.equals("-")) {
                            i--;
                            break;
                        }
                        if (operator2.equals("*")) {
                            operRight = operRight * Double.valueOf(expr[i++]);
                        }
                        if (operator2.equals("/")) {
                            operRight = operRight / Double.valueOf(expr[i++]);
                        }
                    }
                    operLeft = operLeft + operRight;
                    break;
                case "-":
                    while (i < expr.length) {
                        String operator2 = expr[i++];
                        if (operator2.equals("+") || operator2.equals("-")) {
                            i--;
                            break;
                        }
                        if (operator2.equals("*")) {
                            operRight = operRight * Double.valueOf(expr[i++]);
                        }
                        if (operator2.equals("/")) {
                            operRight = operRight / Double.valueOf(expr[i++]);
                        }
                    }
                    operLeft = operLeft - operRight;
                    break;
            }
        }
        return operLeft;
    }