逆波兰记

逆波兰记法中,操作符置于操作数的后面。例如表达“三加四”时,写作“3 4 +”,而不是“3 + 4”。如果有多个操作符,操作符置于第二个操作数的后面,所以常规中缀记法的“3 - 4 + 5”在逆波兰记法中写作3 4 - 5 +”:先3减去4,再加上5。使用逆波兰记法的一个好处是不需要使用括号。
请编写Java函数计算逆波兰表达式的结果。
输入一个逆波兰表达式,
1、操作数类型为正的double数,符合java的double常量规范,不同的操作数以空格或者运算符分开;
2、支持四则运算,即运算符包括+,-,*,/。
输出为计算结果
如输入为 “1 2 + 3 *”,则输出为“9.0”

import java.util.Stack;

public class JavaTest {

    private final static double Delta = 1e-6;

    public static class JavaTestException extends Exception {
        private static final long serialVersionUID = 1584895723070259373L;
    }


    public static class RPNSyntaxErrorException extends JavaTestException {
        private static final long serialVersionUID = 9109750478481891787L;

        public RPNSyntaxErrorException() {
        }
    }

    public static class DevideByZeroException extends JavaTestException {
        private static final long serialVersionUID = 2599554592065887836L;

        public DevideByZeroException() {
        }
    }

    public double calculate(String rpn) throws JavaTestException {
        if (rpn == null || rpn.length() == 0) {
            throw new RPNSyntaxErrorException();
        }

        String lastStr = rpn.replaceAll("([\\+-])", " $1 ").replaceAll(" +", " ").trim();
        String splits[] = lastStr.split(" ");
        Stack<Double> stack = new Stack<Double>();
        int length = splits.length;
        double temp;
        int index = 0;

        String tempStr;
        while (index < length) {
            tempStr = splits[index++];
            switch (tempStr) {
                case "+":

                    temp = stack.pop();
                    if (stack.isEmpty()) {
                        throw new RPNSyntaxErrorException();
                    }
                    stack.push(stack.pop() + temp);
                    break;
                case "-":
                    temp = stack.pop();
                    if (stack.isEmpty()) {
                        throw new RPNSyntaxErrorException();
                    }
                    stack.push(stack.pop() - temp);
                    break;
                case "*":
                    temp = stack.pop();
                    if (stack.isEmpty()) {
                        throw new RPNSyntaxErrorException();
                    }
                    stack.push(stack.pop() * temp);
                    break;
                case "/":
                    temp = stack.pop();
                    if (stack.isEmpty()) {
                        throw new RPNSyntaxErrorException();
                    }

                    if (Math.abs(temp) < Delta) {
                        throw new DevideByZeroException();
                    }

                    try {
                        stack.push(stack.pop() / temp);
                    } catch (Exception e) {
                        throw new DevideByZeroException();
                    }
                    break;
                default:
                    try {
                        temp = Double.valueOf(tempStr);
                        stack.push(temp);
                    } catch (Exception e) {
                        throw new RPNSyntaxErrorException();
                    }
                    break;
            }

        }

        return stack.peek();
    }
}

 

posted @ 2018-10-23 09:23  牧 天  阅读(136)  评论(0)    收藏  举报