亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

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
凤冈县| 遵义市| 寿阳县| 澄迈县| 杭锦后旗| 黄骅市| 吴旗县| 乐业县| 榆树市| 乃东县| 临猗县| 竹溪县| 绥阳县| 维西| 江源县| 济源市| 渑池县| 荃湾区| 盖州市| 呈贡县| 仁怀市| 洛扎县| 镇远县| 平塘县| 泸州市| 桂林市| 鹤峰县| 沾化县| 文水县| 鄯善县| 雅安市| 天长市| 金秀| 西华县| 杨浦区| 军事| 云阳县| 南昌市| 绥江县| 建昌县| 建始县|