Java编程:栈的应用实例——逆波兰计算器

逆波兰计算器

我们完成一个逆波兰计算器,要求完成如下任务:

  1. 输入一个逆波兰表达式(后缀表达式),使用栈(Stack), 计算其结果
  2. 支持小括号和多位数整数,因为这里我们主要讲的是数据结构,因此计算器进行简化,只支持对整数的计算。
  3. 思路分析:针对后缀表达式3 4 + 5 × 6 -
    1)从左至右扫描,将3和4压入堆栈;
    2)遇到+运算符,因此弹出4和3(4为栈顶元素,3为次顶元素),计算出3+4的值,得7,再将7入栈;
    3)将5入栈;
    4)接下来是×运算符,因此弹出5和7,计算出7×5=35,将35入栈;
    5)将6入栈;
    6)最后是-运算符,计算出35-6的值,即29,由此得出最终结果

代码完成

package stack;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class PolandNotation {
    
    
    public static void main(String[] args) {
    
    

        /* 逆波兰计算器的验证代码 */
        /*// 先定义一个逆波兰表达式
        // (3+4)*5-6 => 34+5*6-
        // 为了方便,逆波兰表达式中的数字和符号使用空格隔开
        String suffixExpression = "3 4 + 5 * 6 - ";
        // 思路
        // 1. 先将"3 4 + 5 * 6 - "放到一个ArrayList中
        // 2. 将ArrayList传递给一个方法,遍历配合栈完成计算
        List<String> rpnList = getListString(suffixExpression);
        System.out.println(rpnList);

        int res = calculate(rpnList);
        System.out.println("计算的结果是:" + res);*/

        /*中缀转后缀测试*/
        // 完成将一个中缀表达式转成后缀表达式的功能
        // 说明
        // 1. 1+((2+3)*4)-5 转成 1 2 3 + 4 * + 5 -
        // 2. 因为直接对str进行操作不方便,因此先将"1+((2+3)*4)-5"转成其对应的中缀表达式的List
        //    即:"1+((2+3)*4)-5" => ArrayList[1,+,(,(,2,+,3,),*,4,),-,5]
        // 3. 将得到的一个中缀表达式对应的List => 后缀表达式对应的List
        //    即:ArrayList[1,+,(,(,2,+,3,),*,4,),-,5] => ArrayList[1,2,3,+,4,*,+,5,-]

        String exception = "1+((2+3)*4)-5";
        List<String> list = toInFixExpressionList(exception);
        System.out.println("中缀表达式对应的List:" + list);

        List<String> list1 = parseSuffixExpressionList(list);
        System.out.println("后缀表达式对应的List:" + list1);

        System.out.println(calculate(list1));
    }

    // 将得到的一个中缀表达式对应的List => 后缀表达式对应的List
    // 即:ArrayList[1,+,(,(,2,+,3,),*,4,),-,5] => ArrayList[1,2,3,+,4,*,+,5,-]
    public static List<String> parseSuffixExpressionList(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>();
        // 遍历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());
                }
                s1.pop();   // 将"("小括号弹出s1栈,消除小括号
            } else {
    
    
                // 当item优先级小于等于s1栈顶运算符优先级,将s1栈顶的运算符弹出并加入s2,重复该工作
                // 问题:缺少一个比较优先级高度的方法
                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> toInFixExpressionList(String s) {
    
    
        // 定义一个List,存放中缀表达式对应的内容
        List<String> ls = new ArrayList<String>();
        int index = 0;   // 索引,用于遍历中缀表达式字符串
        String str = "";    // 对多位数的拼接工作
        char c = ' ';   // 每遍历到一个字符,就放入到c中
        do {
    
    
            // 如果c是一个非数组,就需要加入到ls中
            c = s.charAt(index);
            if (c < 48 || c > 57) {
    
     //非数字
                ls.add("" + c);
                index++;
            } else {
    
      // 如果是数字,需要考虑多位数拼接
                str = ""; // 先将str清空    '0'=>[48] '9'=>[57]
                while (index < s.length() && (c = s.charAt(index)) >= 48 && (c = s.charAt(index)) <= 57) {
    
    
                    str += c;
                    index++;
                }
                ls.add(str);
            }
        } while (index < s.length());
        return ls;
    }

    //

    // 将逆波兰表达式依次将数据和运算符放入ArrayList中
    public static List<String> getListString(String suffixExpression) {
    
    
        // 将suffixExpression分割
        String[] split = suffixExpression.split(" ");
        List<String> list = new ArrayList<String>();
        for (String ele : split) {
    
    
            list.add(ele);
        }
        return list;
    }

    // 完成对逆波兰表达式运算
    /*
     * 1)从左至右扫描,将3和4压入堆栈
     * 2)遇到 + 运算符,因此弹出4和3(4为栈顶元素,3位次顶元素),计算出3+4的值,得7,将7入栈
     * 3)将5入栈
     * 4)加下来是 × 运算符,因此弹出5和7,计算7*5=35,将35入栈
     * 5)将6入栈
     * 6)最后是 - 运算符,计算35-6的值,即29,由此得到最终答案
     * */
    public static int calculate(List<String> ls) {
    
    
        // 创建栈
        Stack<String> stack = new Stack<String>();
        // 遍历ls
        for (String item : ls) {
    
    
            // 使用正则表达式取出数组
            if (item.matches("\\d+")) {
    
       // 匹配的是多位数
                stack.push(item);
            } else {
    
    
                // pop出两个数,并运算,再入栈
                int num2 = Integer.parseInt(stack.pop());
                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("运算符有误");
                }
                // res 入栈
                stack.push("" + res);
            }
        }
        // 最后留在stack中的数据就是运算结果
        return Integer.parseInt(stack.pop());
    }
}

// 编写一个类 Operation 返回一个运算符对应的优先级
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("不存在该运算符");
                break;
        }
        return result;
    }
}

完整版

说明

完整版的逆波兰计算器,功能包括

  1. 支持 + - * / ( )
  2. 多位数,支持小数,
  3. 兼容处理, 过滤任何空白字符,包括空格、制表符、换页符
    逆波兰计算器完整版考虑的因素较多,下面给出完整版代码供同学们学习,其基本思路和前面一样,也是使用到:中缀表达式转后缀表达式。

代码

package com.atguigu.reversepolishcal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
import java.util.regex.Pattern;

public class ReversePolishMultiCalc {
    
    

	 /**
     * 匹配 + - * / ( ) 运算符
     */
    static final String SYMBOL = "\\+|-|\\*|/|\\(|\\)";

    static final String LEFT = "(";
    static final String RIGHT = ")";
    static final String ADD = "+";
    static final String MINUS= "-";
    static final String TIMES = "*";
    static final String DIVISION = "/";

    /**
     * 加減 + -
     */
    static final int LEVEL_01 = 1;
    /**
     * 乘除 * /
     */
    static final int LEVEL_02 = 2;

    /**
     * 括号
     */
    static final int LEVEL_HIGH = Integer.MAX_VALUE;


    static Stack<String> stack = new Stack<>();
    static List<String> data = Collections.synchronizedList(new ArrayList<String>());

    /**
     * 去除所有空白符
     * @param s
     * @return
     */
    public static String replaceAllBlank(String s ){
    
    
        // \\s+ 匹配任何空白字符,包括空格、制表符、换页符等等, 等价于[ \f\n\r\t\v]
        return s.replaceAll("\\s+","");
    }

    /**
     * 判断是不是数字 int double long float
     * @param s
     * @return
     */
    public static boolean isNumber(String s){
    
    
        Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
        return pattern.matcher(s).matches();
    }

    /**
     * 判断是不是运算符
     * @param s
     * @return
     */
    public static boolean isSymbol(String s){
    
    
        return s.matches(SYMBOL);
    }

    /**
     * 匹配运算等级
     * @param s
     * @return
     */
    public static int calcLevel(String s){
    
    
        if("+".equals(s) || "-".equals(s)){
    
    
            return LEVEL_01;
        } else if("*".equals(s) || "/".equals(s)){
    
    
            return LEVEL_02;
        }
        return LEVEL_HIGH;
    }

    /**
     * 匹配
     * @param s
     * @throws Exception
     */
    public static List<String> doMatch (String s) throws Exception{
    
    
        if(s == null || "".equals(s.trim())) throw new RuntimeException("data is empty");
        if(!isNumber(s.charAt(0)+"")) throw new RuntimeException("data illeagle,start not with a number");

        s = replaceAllBlank(s);

        String each;
        int start = 0;

        for (int i = 0; i < s.length(); i++) {
    
    
            if(isSymbol(s.charAt(i)+"")){
    
    
                each = s.charAt(i)+"";
                //栈为空,(操作符,或者 操作符优先级大于栈顶优先级 && 操作符优先级不是( )的优先级 及是 ) 不能直接入栈
                if(stack.isEmpty() || LEFT.equals(each)
                        || ((calcLevel(each) > calcLevel(stack.peek())) && calcLevel(each) < LEVEL_HIGH)){
    
    
                    stack.push(each);
                }else if( !stack.isEmpty() && calcLevel(each) <= calcLevel(stack.peek())){
    
    
                    //栈非空,操作符优先级小于等于栈顶优先级时出栈入列,直到栈为空,或者遇到了(,最后操作符入栈
                    while (!stack.isEmpty() && calcLevel(each) <= calcLevel(stack.peek()) ){
    
    
                        if(calcLevel(stack.peek()) == LEVEL_HIGH){
    
    
                            break;
                        }
                        data.add(stack.pop());
                    }
                    stack.push(each);
                }else if(RIGHT.equals(each)){
    
    
                    // ) 操作符,依次出栈入列直到空栈或者遇到了第一个)操作符,此时)出栈
                    while (!stack.isEmpty() && LEVEL_HIGH >= calcLevel(stack.peek())){
    
    
                        if(LEVEL_HIGH == calcLevel(stack.peek())){
    
    
                            stack.pop();
                            break;
                        }
                        data.add(stack.pop());
                    }
                }
                start = i ;    //前一个运算符的位置
            }else if( i == s.length()-1 || isSymbol(s.charAt(i+1)+"") ){
    
    
                each = start == 0 ? s.substring(start,i+1) : s.substring(start+1,i+1);
                if(isNumber(each)) {
    
    
                    data.add(each);
                    continue;
                }
                throw new RuntimeException("data not match number");
            }
        }
        //如果栈里还有元素,此时元素需要依次出栈入列,可以想象栈里剩下栈顶为/,栈底为+,应该依次出栈入列,可以直接翻转整个stack 添加到队列
        Collections.reverse(stack);
        data.addAll(new ArrayList<>(stack));

        System.out.println(data);
        return data;
    }

    /**
     * 算出结果
     * @param list
     * @return
     */
    public static Double doCalc(List<String> list){
    
    
        Double d = 0d;
        if(list == null || list.isEmpty()){
    
    
            return null;
        }
        if (list.size() == 1){
    
    
            System.out.println(list);
            d = Double.valueOf(list.get(0));
            return d;
        }
        ArrayList<String> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
    
    
            list1.add(list.get(i));
            if(isSymbol(list.get(i))){
    
    
                Double d1 = doTheMath(list.get(i - 2), list.get(i - 1), list.get(i));
                list1.remove(i);
                list1.remove(i-1);
                list1.set(i-2,d1+"");
                list1.addAll(list.subList(i+1,list.size()));
                break;
            }
        }
        doCalc(list1);
        return d;
    }

    /**
     * 运算
     * @param s1
     * @param s2
     * @param symbol
     * @return
     */
    public static Double doTheMath(String s1,String s2,String symbol){
    
    
        Double result ;
        switch (symbol){
    
    
            case ADD : result = Double.valueOf(s1) + Double.valueOf(s2); break;
            case MINUS : result = Double.valueOf(s1) - Double.valueOf(s2); break;
            case TIMES : result = Double.valueOf(s1) * Double.valueOf(s2); break;
            case DIVISION : result = Double.valueOf(s1) / Double.valueOf(s2); break;
            default : result = null;
        }
        return result;

    }

    public static void main(String[] args) {
    
    
        //String math = "9+(3-1)*3+10/2";
        String math = "12.8 + (2 - 3.55)*4+10/5.0";
        try {
    
    
            doCalc(doMatch(math));
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }

}

猜你喜欢

转载自blog.csdn.net/KaiSarH/article/details/108735132