Untitled

 avatar
unknown
java
2 years ago
3.6 kB
6
Indexable
    public static double evaluate(String expression) {
        Stack<Double> operandStack = new Stack<>();
        Stack<Character> operatorStack = new Stack<>();
        
        for (int i = 0; i < expression.length(); i++) {
            char c = expression.charAt(i);
            
            if (Character.isDigit(c) || c == '.') {
                // Parse the number and push it onto the operand stack
                int j = i;
                while (j < expression.length() && (Character.isDigit(expression.charAt(j)) || expression.charAt(j) == '.')) {
                    j++;
                }
                double num = Double.parseDouble(expression.substring(i, j));
                operandStack.push(num);
                i = j - 1;
            } else if (c == '(') {
                // Push the opening parenthesis onto the operator stack
                operatorStack.push(c);
            } else if (c == ')') {
                // Evaluate the expression inside the parentheses and push the result onto the operand stack
                while (!operatorStack.isEmpty() && operatorStack.peek() != '(') {
                    char operator = operatorStack.pop();
                    double b = operandStack.pop();
                    double a = operandStack.pop();
                    double result = applyOperator(operator, a, b);
                    operandStack.push(result);
                }
                operatorStack.pop(); // Pop the opening parenthesis from the operator stack
            } else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^') {
                // Evaluate any higher-precedence operators on the operator stack and push the current operator onto the stack
                while (!operatorStack.isEmpty() && getPrecedence(c) <= getPrecedence(operatorStack.peek())) {
                    char operator = operatorStack.pop();
                    double b = operandStack.pop();
                    double a = operandStack.pop();
                    double result = applyOperator(operator, a, b);
                    operandStack.push(result);
                }
                operatorStack.push(c);
            }
        }
        
        // Evaluate any remaining operators on the operator stack
        while (!operatorStack.isEmpty()) {
            char operator = operatorStack.pop();
            double b = operandStack.pop();
            double a = operandStack.pop();
            double result = applyOperator(operator, a, b);
            operandStack.push(result);
        }
        
        // The result should be the only value left on the operand stack
        return operandStack.pop();
    }
    
    private static int getPrecedence(char operator) {
        switch (operator) {
            case '+':
            case '-':
                return 1;
            case '*':
            case '/':
                return 2;
            case '^':
                return 3;
            default:
                throw new IllegalArgumentException("Invalid operator: " + operator);
        }
    }
    
    private static double applyOperator(char operator, double a, double b) {
        switch (operator) {
            case '+':
                return a + b;
            case '-':
                return a - b;
            case '*':
                return a * b;
            case '/':
                return a / b;
            case '^':
                return Math.pow(a, b);
            default:
                throw new IllegalArgumentException("Invalid operator: " + operator);
        }
    }

Editor is loading...