java算术表达式求值怎么写

54次阅读
没有评论

共计 2727 个字符,预计需要花费 7 分钟才能阅读完成。

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

  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

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-16发表,共计2727字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)