数据结构-逆波兰计算器

package com.cubemonkey.stack;

/**
 * @author CubeMonkey
 * @create 2020-06-05 13:52
 */
public class PolandNotation {
    /**
     * 计算指定运算符
     * @param num1 被操作数
     * @param num2 操作数
     * @param oper 运算符
     * @return
     */
    public static int calc(int num1, int num2, char oper){
        int num = 0;
        switch (oper){
            case '+':
                num = num1 + num2;
                break;
            case '-':
                num = num1 - num2;
                break;
            case '*':
                num = num1 * num2;
                break;
            case '/':
                num = num1 / num2;
                break;
            default:
                throw new RuntimeException("oper Error");
        }
        return num;
    }

    public static int calculateInfixExpression(String infix){
        String suffixExpression = parseSuffixExpression(infix);
        return calculate(suffixExpression);
    }

    /**
     * 将中缀表达式转化为后缀表达式
     * @param infixExpression
     * @return
     */
    public static String parseSuffixExpression(String infixExpression){
        StringBuffer suffixExpression = new StringBuffer();
        LinkedStack<Character> operateStack = new LinkedStack<>();
        int index = 0;
        int length = infixExpression.length();
        char ch;
        while(index < length){
            ch = infixExpression.charAt(index);
            if(ch >= '0' && ch <= '9'){
                int num = ch - '0';
                while(index + 1 < length && infixExpression.charAt(index+1) >= '0' && infixExpression.charAt(index+1) <= '9'){
                    num = num*10 + infixExpression.charAt(++index)-'0';
                }
                suffixExpression.append(num + " ");
            }else{
                switch (ch){
                    case '+':
                    case '-':
                        while(!operateStack.isEmpty() && operateStack.peek() != '(' && (operateStack.peek() == '-' || operateStack.peek() == '+')){
                            char operate = operateStack.pop();
                            suffixExpression.append(operate + " ");
                        }
                        operateStack.push(ch);
                        break;
                    case '*':
                    case '/':
                        while(!operateStack.isEmpty() && operateStack.peek() != '('){
                            char operate = operateStack.pop();
                            suffixExpression.append(operate+" ");
                        }
                        operateStack.push(ch);
                        break;
                    case '(':
                        operateStack.push(ch);
                        break;
                    case ')':
                        while(!operateStack.isEmpty() && operateStack.peek() != '('){
                            char operate = operateStack.pop();
                            suffixExpression.append(operate + " ");
                        }
                        operateStack.pop();
                        break;
                    default:
                        break;
                }
            }
            index++;
        }
        while(operateStack.getLength() > 1){
            suffixExpression.append(operateStack.pop() + " ");
        }
        suffixExpression.append(operateStack.pop());
        return suffixExpression.toString();
    }

    /**
     * 逆波兰计算器
     * @param suffixExpression
     * @return
     */
    public static int calculate(String suffixExpression){
        int index = 0;
        int length = suffixExpression.length();
        LinkedStack<Integer> stack = new LinkedStack<>();
        char ch;
        while(index < length){
            ch = suffixExpression.charAt(index);
            if(ch >= '0' && ch <= '9'){
                int num = ch - '0';
                while(index+1 < length && suffixExpression.charAt(index+1) >= '0' && suffixExpression.charAt(index+1) <= '0'){
                    num = num*10 + suffixExpression.charAt(++index)-'0';
                }
                stack.push(num);
            }else if(ch == '+' || ch == '-' || ch == '*' || ch == '/'){
                int num2 = stack.pop();
                int num1 = stack.pop();
                int res = calc(num1, num2, ch);
                stack.push(res);
            }
            index++;
        }

        return stack.pop();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rex·Lin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值