中缀表达式转后缀表达式

package org.structure.stack;

import java.util.ArrayList;
import java.util.Stack;
/**
 * 中缀表达式转化成后缀表达式的过程
 * @author cjj_1
 * @date 2020-08-07 15:05
 */
public class ToSuffix {
    public static void main(String[] args) {
        Stack<String> s1= new Stack<String>(); //数栈
        Stack<String> s2= new Stack<String>();//操作符栈
        String expression  = "5 + 6 * ( 9 - 7 ) - 2";
        String[] suffixArr = expression.split(" ");
        ArrayList<String> list = new ArrayList<String>();
        int num1;
        int num2;
        String oper;
        int res ;
        //将数组转换成 list
        for(int i =0 ;i< suffixArr.length;i++){
            list.add(suffixArr[i]);
        }
        //开始遍历表达式
        for(int i =0;i<list.size();i++){
            String item = list.get(i);
            if(item.matches("\\d+"))
                s1.push(item);
            else if(!isOper(item)) {
                throw new RuntimeException(item + ":是非法字符");
            }else if (item.equals(")")){
                while (true){
                    if(s2.peek().equals("(")){
                        s2.pop();
                        break;
                    }else {
                        oper = s2.pop();
//                        num1 = Integer.valueOf(s1.pop());
//                        num2 = Integer.valueOf(s1.pop());
//                        res = calcalator(num1,num2,oper);
                        s1.push(oper);
                    }
                }
                continue;
            }else if(s2.empty()|| item.equals("(")||priority(item)>=priority(s2.peek())){
                s2.push(item);
            }else if(priority(item)<priority(s2.peek())){
                oper = s2.pop();
                s1.push(oper);
                while (priority(item)<priority(s2.peek())){
                    oper = s2.pop();
                    s1.push(oper);
                }
                s2.push(item);
            }
        }
        while (!s2.isEmpty()){
            s1.push(s2.pop());
        }
        s1.forEach(s->{
            System.out.print(s+" ");
        });
        //输出结果为:5,6,9,7,-,*,2,-,+, 是我们的后缀表达式

    }

    /**
     * 运算数据
     * @param num1
     * @param num2
     * @param oper
     * @return
     */
    public static int calcalator(int num1,int num2,String oper){
        int res=0;
        switch (oper){
            case "*":
                res = num2*num1;
                break;
            case "/":
                res = num2/num1;
                break;
            case "+":
                res = num2+num1;
                break;
            case "-":
                res = num2-num1;
                break;
            default:
                res=-1;
                break;
        }

        return  res;

    }

    /*
     * 比较操作符的优先级
     * @author cjj_1
     * @date 2020-08-06 11:50:31
     * @param oper
     * @return
     **/
    public static int priority(String oper){
        if(oper.equals("*") || oper.equals("/"))
            return 1;
        else if(oper.equals("+") || oper.equals("-"))
            return 0;
        else
            return  -1;
    }
    //是否是操作符
    public static  boolean isOper(String oper){
        if(oper.equals("+")||oper.equals("-")||oper.equals("*")||oper.equals("/")||oper.equals("(")||oper.equals(")"))
            return Boolean.TRUE;
        return Boolean.FALSE;
    }
    //是否是左括号
    public static  boolean isLeftBracket(String oper){
        if(oper.equals("("))
            return Boolean.TRUE;
        return Boolean.FALSE;
    }
    //是否是右括号
    public static  boolean isRightBracket(String oper){
        if(oper.equals(")"))
            return Boolean.TRUE;
        return Boolean.FALSE;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值