后綴表達式(逆波蘭表達式)計算器


package datastructure.stack;

import java.util.*;

/**
 * <h3>netty_lecture</h3>
 * <p>逆波蘭計算器</p>
 *  1+((2+3)*4)-5  ==> 1 2 3 + 4 * + 5 1
 * @author : myron
 * @date : 2020-03-18 23:48
 **/
public class PolandNotation {

    private static final int MINAS = 0;
    private static final int PLUS = 0;
    private static final int MULTI = 1;
    private static final int DIV = 1;

    public static void main(String[] args){
        String expression = "1,+,(,(,200,+,3,),*,9,),-,5";
        List<String> list = middleExpressionToList(expression);
        List<String> suffixExp = middleExpToSuffixExp(list);
        System.out.println("中綴表達式:"+ list);
        System.out.println("后綴表達式:" + suffixExp);
        int result = calculate(suffixExp);
        System.out.println("結果為:" + result);
    }


    /**
     * 中綴表達式轉List
     * @param expression
     * @return
     */
    public static List<String> middleExpressionToList(String expression){
        String[] split = expression.split(",");
        List<String> list = Arrays.asList(split);
        return list;
    }

    /**
     * 中綴表達式轉后綴表達式list
     * @param middleExpList
     * @return
     */
    public static List<String> middleExpToSuffixExp(List<String> middleExpList){
        Stack<String> s1 = new Stack<>();
        List<String>  s2 = new ArrayList<>();

        for (String oper:middleExpList){
            int priority = getPriority(oper);
            if(oper.matches("\\d+")){
                s2.add(oper);
            }else if("(".equals(oper)) {
                s1.push(oper);
            } else if(")".equals(oper)){
                while(!"(".equals(s1.peek())){
                    s2.add(s1.pop());
                }
                s1.pop();
            }else{
                while(s1.size() != 0 && priority <= getPriority(s1.peek())){
                    s2.add(s1.pop());
                }
                s1.push(oper);
            }
          }

        while(s1.size() != 0){
            s2.add(s1.pop());
        }
        return s2;
    }
    /**
     * 計算方法
     * @param list :后綴表達式
     * @return
     */
    public static int calculate(List<String>list){
        //創建棧
        Stack<String> stack = new Stack<>();
        //遍歷表達式list
        for(String str: list){
            /**正則匹配,是數字,入棧*/
            if(str.matches("\\d+")){
              stack.push(str);
            /**運算符,則彈出兩位數,進行運算*/
            }else{
                int num1 = Integer.parseInt(stack.pop());
                int num2 = Integer.parseInt(stack.pop());
                int result = 0;
                if(str.equals("+")){
                    result = num1 + num2;
                }else if(str.equals("-")){
                    result = num2 - num1;
                }else if(str.equals("*")){
                    result = num1 * num2;
                }else if(str.equals("/")){
                    result = num2/num1;
                }else{
                    throw new RuntimeException("不支持該符號!");
                }
                //預算結果入棧
                stack.push(result + "");
            }
        }
        //返回運算結果
        return Integer.parseInt(stack.pop());
    }

    /**
     * 獲取符號的優先級
     */
    public static int getPriority(String oper){
        if (oper.equals("+")) {
            return PLUS;
        }else if(oper.equals("-")){
            return MINAS;
        }else if(oper.equals("*")){
            return MULTI;
        }else if(oper.equals("/")){
            return DIV;
        }
        return -1;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM