Java

java算术表达式求值怎么写

小亿
73
2023-10-23 16:13:21
栏目: 编程语言

要求解一个算术表达式的值,可以使用栈来实现。具体步骤如下:

  1. 创建两个栈,一个用于存储操作符(operatorStack),一个用于存储操作数(numberStack)。
  2. 遍历表达式的每个字符,进行如下操作:
    • 如果字符是空格,则忽略。
    • 如果字符是数字,则将其转换为数字,并将其压入numberStack。
    • 如果字符是’(',则将其压入operatorStack。
    • 如果字符是’)‘,则从operatorStack中弹出操作符,直到遇到’('。对于每个弹出的操作符,从numberStack中弹出两个操作数进行运算,将结果压入numberStack。
    • 如果字符是运算符,分两种情况处理:
      • 如果operatorStack为空,或者栈顶操作符是’(',则将运算符直接压入operatorStack。
      • 否则,将当前运算符和栈顶操作符进行比较,如果当前运算符的优先级小于等于栈顶操作符,则从operatorStack中弹出操作符,从numberStack中弹出两个操作数进行运算,将结果压入numberStack。重复此操作,直到当前运算符的优先级大于栈顶操作符,或者operatorStack为空。然后将当前运算符压入operatorStack。
  3. 当遍历完表达式后,从operatorStack中依次弹出操作符,从numberStack中弹出两个操作数进行运算,将结果压入numberStack。重复此操作,直到operatorStack为空。
  4. 最后,numberStack中剩下的唯一元素就是表达式的值。

以下是一个示例代码:

import java.util.Stack;

public class ExpressionEvaluation {
    public static double evaluateExpression(String expression) {
        Stack<Character> operatorStack = new Stack<>();
        Stack<Double> numberStack = new Stack<>();

        for (int i = 0; i < expression.length(); i++) {
            char c = expression.charAt(i);

            if (c == ' ') {
                continue;
            } else if (Character.isDigit(c)) {
                StringBuilder sb = new StringBuilder();
                while (i < expression.length() && (Character.isDigit(expression.charAt(i)) || expression.charAt(i) == '.')) {
                    sb.append(expression.charAt(i));
                    i++;
                }
                i--;

                double number = Double.parseDouble(sb.toString());
                numberStack.push(number);
            } else if (c == '(') {
                operatorStack.push(c);
            } else if (c == ')') {
                while (!operatorStack.isEmpty() && operatorStack.peek() != '(') {
                    char operator = operatorStack.pop();
                    double operand2 = numberStack.pop();
                    double operand1 = numberStack.pop();
                    double result = applyOperator(operator, operand1, operand2);
                    numberStack.push(result);
                }
                operatorStack.pop();  // 弹出左括号
            } else if (isOperator(c)) {
                while (!operatorStack.isEmpty() && operatorStack.peek() != '(' && getPrecedence(c) <= getPrecedence(operatorStack.peek())) {
                    char operator = operatorStack.pop();
                    double operand2 = numberStack.pop();
                    double operand1 = numberStack.pop();
                    double result = applyOperator(operator, operand1, operand2);
                    numberStack.push(result);
                }
                operatorStack.push(c);
            }
        }

        while (!operatorStack.isEmpty()) {
            char operator = operatorStack.pop();
            double operand2 = numberStack.pop();
            double operand1 = numberStack.pop();
            double result = applyOperator(operator, operand1, operand2);
            numberStack.push(result);
        }

        return numberStack.pop();
    }

    private static boolean isOperator(char c) {
        return c == '+' || c == '-' || c == '*' || c == '/';
    }

    private static int getPrecedence(char operator) {
        if (operator == '+' || operator == '-') {
            return 1;
        } else if (operator == '*' || operator == '/') {
            return 2;
        } else {
            return 0;
        }
    }

    private static double applyOperator(char operator, double operand1, double operand2) {
        switch (operator) {
            case '+': return operand1 + operand2;
            case '-': return operand1 - operand2;
            case '*': return operand1 * operand2;
            case '/': return operand

0
看了该问题的人还看了