中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

java算術表達式求值怎么寫

小億
71
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
甘谷县| 鹤峰县| 民和| 宜都市| 咸阳市| 金塔县| 吉木乃县| 石渠县| 建宁县| 绥滨县| 彰化市| 沁阳市| 孟连| 宁阳县| 达拉特旗| 茶陵县| 南宁市| 区。| 庆阳市| 图们市| 册亨县| 景东| 尉犁县| 秀山| 当雄县| 广河县| 万源市| 石渠县| 含山县| 横山县| 威远县| 武义县| 昌平区| 库车县| 湘潭县| 娄烦县| 廉江市| 子长县| 滨海县| 吉木乃县| 建昌县|