java思路实现逆波兰表达式 day5

import java.util.*;
        //给定的逆波兰表达式计算结果
        //先转成list
        /*
        第一步先把Sting转换成list
        2开始转换为后缀表达式
        3计算
         */
public class nibolan {
    public static void main(String[] args) {
    
        String a = "5-6+1*2-3";
        List<String> list = new ArrayList<>();
        list=zhuanhuan(a);
        list=parseSuffixExpreesionList(list);
        int x=jsuan(list);
        System.out.println(list);
        System.out.println(x);
   
 }

//中缀转成后缀
    /*
    思路 1.还是先转成list 方便操作
    2.建立二个堆栈 ,,要自己做符号比较次序
    3.数字压s2
    4符号 分情况:
            1空或者是‘(’或者是比堆栈的次序高 则直接入
            2di的话就把s1出栈放入到s2直到 比较的比堆栈的高  
            结束后把s1的push到s2
            s2逆序就是
     */
    public static List<String> parseSuffixExpreesionList(List<String> ls) {
        //定义两个栈
        Stack<String> s1 = new Stack<String>(); // 符号栈
        //说明:因为s2 这个栈,在整个转换过程中,没有pop操作,而且后面我们还需要逆序输出
        //因此比较麻烦,这里我们就不用 Stack<String> 直接使用 List<String> s2
        //Stack<String> s2 = new Stack<String>(); // 储存中间结果的栈s2
        List<String> s2 = new ArrayList<String>(); // 储存中间结果的Lists2

        //遍历ls
        for (String item : ls) {
            //如果是一个数,加入s2
            if (item.matches("\\d+")) {
                s2.add(item);
            } else if (item.equals("(")) {
                s1.push(item);
            } else if (item.equals(")")) {
                //如果是右括号“)”,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为止,此时将这一对括号丢弃
                while (!s1.peek().equals("(")) {
                    s2.add(s1.pop());//peek是看一眼
                }
                s1.pop();//!!! 将 ( 弹出 s1栈, 消除小括号
            } else {
                //当item的优先级小于等于s1栈顶运算符, 将s1栈顶的运算符弹出并加入到s2中,再次转到(4.1)与s1中新的栈顶运算符相比较
                //问题:我们缺少一个比较优先级高低的方法
                while (s1.size() != 0 && Operation.getValue(s1.peek()) >= Operation.getValue(item)) {
                    s2.add(s1.pop());
                }
                //还需要将item压入栈
                s1.push(item);
            }
        }

        //将s1中剩余的运算符依次弹出并加入s2
        while (s1.size() != 0) {
            s2.add(s1.pop());
        }

        return s2; //注意因为是存放到List, 因此按顺序输出就是对应的后缀表达式对应的List

    }


    public static List<String> zhuanhuan(String s) {
        List<String> list = new ArrayList<>();
        int i = 0;
        String str;
        char c;//每次取出一个字符进行比较
        do {
            if ((c = s.charAt(i)) < 48 || ((c = s.charAt(i)) > 57)) {
                list.add("" + c);//符号直接加入list注意加入的是要字符所以 是“”+c
                i++;//‘0’=48,‘9’=57
            } else {
                str = "";//注意
                //检查是否为多位数利用 c是否在48到57之间或者使用正则表达式判断
                while (i < s.length() && (c = s.charAt(i)) >= 48 && ((c = s.charAt(i)) <= 57)) {
                    str += c;
                    i++;//判断下一个

                }
                list.add(str);//出while 后就把str加入到list 注意 每次都要把str清0即“”
            }

        } while (i < s.length());
        return list;
    }

    
    
    /*后缀表达式的计算思路
    1.遇到数字就直接加入到栈中
    2.遇到运算符就pop二个数字进行运算
    3.然后把结果继续加入到栈 直到剩下一个
    
     */
    public static int jsuan(List<String> is) {
        Stack<String> stack = new Stack<>();
        for (String item : is) {
            if (item.matches("\\d+")) {
                stack.push(item);//
            } else {
                int num2 = Integer.parseInt(stack.pop());//转换成int
                int num1 = Integer.parseInt(stack.pop());
                int res = 0;
                if (item.equals("+")) {
                    res = num1 + num2;
                } else if (item.equals("-")) {
                    res = num1 - num2;
                } else if (item.equals("*")) {
                    res = num1 * num2;
                } else if (item.equals("/")) {
                    res = num1 / num2;
                } else {
                    throw new RuntimeException("youcuo");
                }
                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 result = 0;
            switch (operation) {
                case "+":
                    result = ADD;
                    break;
                case "-":
                    result = SUB;
                    break;
                case "*":
                    result = MUL;
                    break;
                case "/":
                    result = DIV;
                    break;
                default:
                    System.out.println("不存在该运算符" + operation);
                    break;
            }
            return result;
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值