中缀表达式转后缀表达式

发布时间 2023-03-23 15:22:08作者: 为TT
  1.  

     

  2. 代码实现
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Stack;

    public class text1 {
    public static void main(String[] args) {
    //先定义一个逆波兰表达式,为了方便,用空格分隔开
    String s = "3 4 + 5 * 6 -";
    //思路:先将3 4 + 5 * 6 -放到一个ArrayList的集合中去
    //将ArrayList传进一个方法,遍历ArrayList配合栈完成计算

    List<String> List = getList(s);
    System.out.println(List);
    int res = calculate(List);
    System.out.println("计算结果为" + res);
    //完成中缀表达式转后缀表达式
    //放到一个集合中去
    String s1 = "1+((2+3)*4)-5";
    List<String> ls = change(s1);
    System.out.println("中缀表达式为"+ls);
    List<String> parse = parse(ls);
    System.out.println("后缀表达式为"+parse);
    int ress=calculate(parse);
    System.out.println(ress);





    }

    //先定义一个逆波兰表达式,先将3 4 + 5 * 6 -放到一个ArrayList的集合中去
    public static List<String> getList(String s) {
    //将s分割
    String[] split = s.split(" ");
    List<String> list = new ArrayList<String>();
    for (String ele : split) {
    list.add(ele);
    }
    return list;
    }

    //计算
    public static int calculate(List<String> list) {
    //创建栈
    Stack<String> stack = new Stack<>();
    //遍历list
    for (String item : list) {
    if (item.matches("\\d+")) {
    stack.push(item);
    } else {
    //pop出两个数和一个符号进行运算
    int num1 = Integer.parseInt(stack.pop());
    int num2 = Integer.parseInt(stack.pop());
    int res = 0;
    if (item.equals("+")) {
    res = num1 + num2;
    } else if (item.equals("-")) {
    res = num2 - num1;
    } else if (item.equals("*")) {
    res = num1 * num2;
    } else if (item.equals("/")) {
    res = num2 / num1;
    } else {
    throw new RuntimeException("运算符有误");
    }
    stack.push("" + res);
    }

    }
    //返回栈的最后一个结果
    return Integer.parseInt(stack.pop());
    }

    //转化
    public static List<String> change(String s) {
    //创建一个空的集合去接收
    List<String> list = new ArrayList<String>();
    int i = 0;//指针
    String str;//对多位数的拼接
    char c;//遍历到每一个字符,就放入c
    //如果c是一个非数字,就转入到list
    //0【48】-9【57】对应的码为
    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;
    }
    //中缀表达式转后缀表达式
    public static List<String> parse(List<String> sw){
    //符号栈
    Stack<String> stack = new Stack<>();
    //数栈用集合表示
    List<String> list = new ArrayList<String>();
    for (String item:sw){
    //如果是一个数
    if (item.matches("\\d+")){
    list.add(item);
    }else if (item.equals("(")){
    stack.push(item);
    }else if (item.equals(")")){
    //依次将stack中的符号弹到list中,直到遇到左括号
    while (!stack.peek().equals("(")){
    list.add(stack.pop());
    }
    stack.pop();//将"(“弹出栈,这个十分重要


    }else {
    //比较优先级问题
    while (stack.size()!=0&&opertion .value(stack.peek())>=opertion.value(item)){
    list.add(stack.pop());
    }
    //将运算符压入栈
    stack.push(item);
    }

    }
    //将剩余的运算符压入list
    while (stack.size()!=0){
    list.add(stack.pop());
    }
    //不需要逆向输出,应为用的是list
    return list;
    }
    }
    //编写一个类来比较运算符的优先级
    class opertion{

    private static int add=1;
    private static int sub=1;
    private static int div=2;
    private static int mlp=2;
    public static int value(String oper){
    int res=0;
    switch (oper){

    case "+":
    res= add;
    break;
    case "-":
    res= sub;
    break;
    case "*":
    res= mlp;
    break;
    case "/":
    res= div;
    break;
    default:
    System.out.println("运算符错误");
    break;
    }
    return res;
    }
    }