栈结构+逆波兰式实现四则运算

import java.util.Stack;

public class Calculator {

    public static void main(String[] args) {
        String s = "  11 +2*((30 -6/3-  4)*6) -5";
        System.out.println(calculate(s));

    }

    public static int calculate(String s) {
        Stack<String> stack = new Stack<>();
        String[] postfix = infixToPostfix(format(s));
        for (String element : postfix) {
            if (isDigit(element)) stack.push(element);
            else {
                String num2 = stack.pop();
                String num1 = stack.pop();
                stack.push(operate(num1, num2, element));
            }
        }
        return Integer.parseInt(stack.pop());
    } //计算表达式

    public static String operate(String s1, String s2, String operator) {
        int num1 = Integer.parseInt(s1);
        int num2 = Integer.parseInt(s2);
        switch (operator) {
            case "+":
                return num1 + num2 + "";
            case "-":
                return num1 - num2 + "";
            case "*":
                return num1 * num2 + "";
            case "/":
                return num1 / num2 + "";
        }
        return null;
    } //进行四则运算

    public static String[] format(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch >= '0' && ch <= '9') {
                sb.append(ch);
            } else {
                sb.append(' ');
                sb.append(ch);
                sb.append(' ');
            }
        }
        return sb.toString().split("\\s+");
    } //格式化字符串

    public static String[] infixToPostfix(String[] infix) {
        StringBuilder postfix = new StringBuilder();
        Stack<String> opStack = new Stack<>();
        for (String s : infix) {
            if (isDigit(s)) postfix.append(s).append(" ");
            else if (s.equals("(")) opStack.push(s);
            else if (s.equals(")")) {
                while (!opStack.peek().equals("(")) {
                    postfix.append(opStack.pop()).append(" ");
                }
                opStack.pop();
            }
            else {
                while (!opStack.isEmpty() && priority(s) <= priority(opStack.peek())) {
                    postfix.append(opStack.pop()).append(" ");
                }
                opStack.push(s);
            }
        }
        return postfix.append(opStack.pop()).toString().split("\\s+");
    } //中缀转后缀

    public static boolean isDigit(String s) {
        try {
            Integer.parseInt(s);
            return true;
        } catch (Exception e) {
            return false;
        }
    } //判断是否为数字

    public static int priority(String s) {
        switch (s) {
            case "+":
            case "-":
                return 1;
            case "*":
            case "/":
                return 2;
            default:
                return 0;
        }
    } //判定运算符优先级
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值