【无标题】

该代码实现了一个Java程序,它使用运算符栈和操作数栈来计算给定的数学表达式。程序遍历输入的字符串表达式,处理数字和运算符,遵循运算符优先级进行计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入代码片

import java.util.Stack;

public class ExpressionCalculator {
public static double calculateExpression(String expression) {
// 创建运算符栈和操作数栈
Stack operatorStack = new Stack<>();
Stack operandStack = new Stack<>();

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

        // 忽略空格
        if (ch == ' ') {
            continue;
        }

        // 如果是数字,则解析整个数字并将结果入操作数栈
        if (Character.isDigit(ch) || ch == '.') {
            StringBuilder sb = new StringBuilder();
            while (i < expression.length() && (Character.isDigit(expression.charAt(i)) || expression.charAt(i) == '.')) {
                sb.append(expression.charAt(i));
                i++;
            }
            i--;

            double num = Double.parseDouble(sb.toString());
            operandStack.push(num);
        } else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
            // 如果是运算符,则判断运算符栈是否为空
            if (!operatorStack.isEmpty()) {
                char topOperator = operatorStack.peek();
                // 当前运算符的优先级小于等于栈顶运算符的优先级时,进行计算
                while (isOperator(topOperator) && comparePrecedence(ch, topOperator) <= 0) {
                    operatorStack.pop();
                    double operand2 = operandStack.pop();
                    double operand1 = operandStack.pop();
                    double result = applyOperator(topOperator, operand1, operand2);
                    operandStack.push(result);

                    if (!operatorStack.isEmpty()) {
                        topOperator = operatorStack.peek();
                    } else {
                        break;
                    }
                }
            }

            // 将当前运算符入运算符栈
            operatorStack.push(ch);
        }
    }

    // 处理剩余的运算符
    while (!operatorStack.isEmpty()) {
        char operator = operatorStack.pop();
        double operand2 = operandStack.pop();
        double operand1 = operandStack.pop();
        double result = applyOperator(operator, operand1, operand2);
        operandStack.push(result);
    }

    // 返回最终结果
    return operandStack.pop();
}

// 判断字符是否为运算符
private static boolean isOperator(char ch) {
    return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}

// 比较两个运算符的优先级
private static int comparePrecedence(char op1, char op2) {
    if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) {
        return 1;
    } else if ((op1 == '+' || op1 == '-') && (op2 == '*' || op2 == '/')) {
        return -1;
    } 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 operand1 / operand2;
        default
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值