逆波兰表达式(后缀表达式)

将中缀表达式转成后缀表达式

//1.先将字符串转为中缀表达式对应的List,因为传进来的会是字符串 "30 4 + 5 * 6 -"
    public static List<String> toFixExpressionList(String s){
        List<String> list = new ArrayList<>();
        int i = 0;//用于遍历中缀字符串
        String str;//做对多位数的拼接
        char c;//每遍历字符,放入到c中
        do {
            if ((c=s.charAt(i))<48||(c=s.charAt(i))>57){
                list.add(c+"");
                i++;
            }else {
                str="";
                while (i<s.length()&&(c = s.charAt(i))>=48 && (c=s.charAt(i))<=57){
                    str+=c;
                    i++;
                }
                list.add(str);
            }
        }while (i<s.length());
        return list;
    }

将中缀list转为后缀list

public static List<String> parseSuffixExpression(List<String> list){
        //定义两个栈
        Stack<String> s1 = new Stack<>();//符号栈
        List<String> s2 = new ArrayList<>();//中间结果栈
        //遍历传入集合
        for (String s : list) {
            //如果是一个数,加入s2
            if (s.matches("\\d+")){
                s2.add(s);
            }else if (s.equals("(")){//左括号直接入栈
                s1.push(s);
            }else if (s.equals(")")){//有括号,则依次弹出s1栈顶的运算符,压入s2,直到遇到左括号为止
                while (!s1.peek().equals("(")){//没看到左括号之前一直弹出
                    s2.add(s1.pop());
                }
                s1.pop();//将左括号弹出s1
            }else {
                //当s优先级小于等于s1栈顶优先级,将s1栈顶的运算符弹出并加入s2
                while (s1.size()!=0 && Operation.getValue(s1.peek())>=Operation.getValue(s)){
                    s2.add(s1.pop());
                }
                //还需要将s压入栈中
                s1.push(s);
            }
        }
        //将s1中剩余的运算符依次压入s2中
        while (s1.size()!=0){
            s2.add(s1.pop());
        }
        return s2;

    }

进行计算

//将转换成逆波兰表达式的list进行计算
public static int cal(List<String> list){
        //创建一个栈
        Stack<String> stack = new Stack<>();
        //遍历list
        for (String s : list) {
            if (s.matches("\\d+")){//匹配多位数
                //直接入栈
                stack.push(s);
            }else {
                //pop出两个数,并运算,再入栈
                int num1 = Integer.parseInt(stack.pop());
                int num2 = Integer.parseInt(stack.pop());
                int res = 0;
                if (s.equals("+")){
                    res = num2+num1;
                }else if (s.equals("-")){
                    res = num2-num1;
                }else if (s.equals("*")){
                    res = num2*num1;
                }else if (s.equals("/")){
                    res = num2/num1;
                }else {
                    throw new RuntimeException("符号错误");
                }
                stack.push(res+"");
            }
        }
        return Integer.parseInt(stack.pop());
    }

优先级的判断

//返回一个运算符对应的优先级
class Operation{
    private static int ADD= 1;
    private static int SUB= 1;
    private static int MUL= 2;
    private static int DIV= 2;

    //返回对应的优先级数字
    public static int getValue(String operation){
        int res = 0;
        switch (operation){
            case "+":
                res = ADD;
                break;
            case "-":
                res = SUB;
                break;
            case "*":
                res = MUL;
                break;
            case "/":
                res = DIV;
                break;
            default:
                System.out.println("不存在的运算符");
                break;
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值