• 一、简介

    • 栈的英文为(stack)
    • 栈是一个先入后出(FILO-First In Last Out)的有序列表
    • 栈(stack)是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表。 允许插入和删除的一端, 为变化的一端, 称为栈顶(Top), 另一端为固定的一端, 称为栈底(Bottom)。
    • 根据栈的定义可知, 最先放入栈中元素在栈底, 最后放入的元素在栈顶, 而删除元素刚好相反, 最后放入的元素最先删除, 最先放入的元素最后删除
    • 图解方式说明出栈(pop)入栈(push)的概念

           

    二、数组模拟栈

      源码: 模拟栈

    1,思路

    • maxSize :栈的大小(数组的大小)
    • arr :用来模拟栈的数组
    • top :指向当前栈顶元素,初始值为 -1 ,表示栈空
    • 判断栈满:top == maxSize ,即已经到达数组最后一个位置
    • 判断栈空:top == -1
    • 入栈:arr[++top] = arr;
    • 出栈:return arr[top–] ;

            

    2,代码实现

    class Stack {
    
        private int maxSize;
        private int[] arr;
        private int top = -1;
    
        public Stack(int maxSize) {
            this.maxSize = maxSize;
            this.arr = new int[maxSize];
        }
    
        /**
         * 往栈中push数据
         */
        public void push(int val) {
            if (isFull()) {
                System.out.println("栈满了,无法插入数据!");
                return;
            }
            arr[++top] = val;
        }
    
        /**
         * 判断满
         */
        public boolean isFull() {
            return top == maxSize - 1;
        }
    
        /**
         * 获取
         */
        public int pop() {
            if (isEmpty()) {
                throw new RuntimeException("栈空了,无法取到数据");
            }
            return arr[top--];
        }
    
        /**
         * 打印栈
         */
        public void show() {
            for (int i = top; i >= 0; i--) {
                System.out.printf("arr[%d] = %d
    ", i, arr[i]);
            }
        }
    
        /**
         * 判空
         */
        public boolean isEmpty() {
            return top < 0;
        }
    
    }
    View Code

    三、实现综合计算器(中缀表达式)

      源码:中缀表达式

    1,思路

    栈分为两个栈:
        数栈(numStack):存储表达式中的数字
        符号栈(operStack):存储表达式中的符号
    扫描表达式(这里并没有考虑括号):
        对于数:扫描到数,则直接压入数栈
        对于运算符:扫描到运算符,分为如下几种情况:
            如果符号栈为空,则直接入栈
            如果符号栈不为空:
                如果当前扫描到的运算符的优先级 <= 符号栈栈顶的运算符的优先级
                  1)从数栈中弹出两个数,根据符号栈栈顶的运算符进行运算(优先级高,就先算出来)
                  2)然后将计算的结果压入栈中
                  3)再将当前运算符压入符号栈
                如果当前扫描到的运算符的优先级 > 符号栈栈顶的运算符的优先级,说明上次的运算符优先级较低,直接压入符号栈                                    

             

    2,代码实现

      测试主类

    public static void main(String[] args) {
        String expression = "7*2*100/5+3-4"; // 18//如何处理多位数的问题?
        ArrayStack nums = new ArrayStack(10);
        ArrayStack opers = new ArrayStack(10);
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0; //运算符
        int res = 0;
        char ch = ' ';
        String str = "";//多位数数值
        while (true) {
            ch = expression.substring(index, index + 1).charAt(0); //获取当前的字符
            if (opers.isOper(ch)) { //扫描到符号
                if (opers.isEmpty()) {
                    //符号栈为空就直接入栈
                    opers.push(ch);
                } else {
                    //查看符号栈栈顶的优先级
                    int stackpri = opers.priority(opers.peek());
                    //查看当前符号的优先级
                    int chpri = opers.priority(ch);
                    //如果当前符号优先级 小于等于 符号栈栈顶的优先级 则计算数字栈的两个数字并压栈到数字栈 将当前符合压入符号栈
                    if (chpri <= stackpri) {
                        num2 = nums.pop();
                        num1 = nums.pop();
                        oper = opers.pop();
                        res = nums.cal(num1, num2, oper);
                        nums.push(res);
                        opers.push(ch);
                    } else {
                        //如果当前符号 大于 符号栈栈顶的优先级  则直接入栈
                        opers.push(ch);
                    }
                }
            } else {
                //扫描到数字
                str += ch ;
                if (index == (expression.length() - 1)) {
                    nums.push(Integer.parseInt(str));
                } else {
                    char next = expression.substring(index + 1, index + 2).charAt(0);
                    //如果下一个char是符号
                    if (opers.isOper(next)) {
                        nums.push(Integer.parseInt(str));
                        str = "";
                    }
                }
            }
            if (index + 1 >= expression.length()) {
                break;
            }
            index++;
        }
    
        num2 = nums.pop();
        num1 = nums.pop();
        int result = nums.cal(num1, num2, opers.pop());
        System.out.println("最终结果为:" + result);
    }
    View Code

      自定义数组栈

    class ArrayStack {
    
        private int maxSize;
        private int[] arr;
        private int top = -1;
    
        public ArrayStack(int maxSize) {
            this.maxSize = maxSize;
            this.arr = new int[maxSize];
        }
    
        /**
         * 往栈中push数据
         */
        public void push(int val) {
            if (isFull()) {
                System.out.println("栈满了,无法插入数据!");
                return;
            }
            arr[++top] = val;
        }
    
        /**
         * 判断满
         */
        public boolean isFull() {
            return top == maxSize - 1;
        }
    
        /**
         * 获取
         */
        public int pop() {
            if (isEmpty()) {
                throw new RuntimeException("栈空了,无法取到数据");
            }
            return arr[top--];
        }
    
        /**
         * 打印栈
         */
        public void show() {
            for (int i = top; i >= 0; i--) {
                System.out.printf("arr[%d] = %d
    ", i, arr[i]);
            }
        }
    
        /**
         * 判空
         */
        public boolean isEmpty() {
            return top < 0;
        }
    
        /**
         * 查看
         */
        public int peek() {
            if (isEmpty()) {
                throw new RuntimeException("栈空了,无法取到数据");
            }
            return arr[top];
        }
    
        public boolean isOper(char ch) {
            return ch == '*' || ch == '/' || ch == '+' || ch == '-';
        }
    
        /**
         * 比较运算符的优先级
         */
        public int priority(int oper) {
            if ('*' == oper || '/' == oper) {
                return 1;
            } else if ('-' == oper || '+' == oper) {
                return 0;
            } else {
                return -1;
            }
        }
    
        public int cal(int num1, int num2, int oper) {
            int res = 0;
            switch (oper) {
                case '*':
                    res = num1 * num2;
                    break;
                case '/':
                    res = num1 / num2;
                    break;
                case '+':
                    res = num1 + num2;
                    break;
                case '-':
                    res = num1 - num2;
                    break;
            }
            return res;
        }
    }
    View Code

    四、逆波兰计算器(后缀表达式)

      源码:逆波兰计算器

    1,思路

    计算: 4 5 * 8 - 60 + 8 2 / +
    1,将字符串按照空格切分成list
    2,定义数值栈nums。遍历list集合,如果是数值直接放入nums中;如果是字符则从nums数值栈中取出栈顶的两个数进行计算,将计算结果压入栈中
    3,最终计算结果则为nums数值栈中的栈顶元素(有且仅有一个)

    2,代码实现

    /**
     * 计算后缀表达式结果
     */
    private static int calresult(List<String> ls) {
        Stack<Integer> nums = new Stack<>();
        for (String s : ls) {
            if (s.matches("\d+")) {
                nums.push(Integer.parseInt(s));
            } else {
                int num2 = nums.pop();
                int num1 = nums.pop();
                int res = cal(num1, num2, s);
                nums.push(res);
            }
        }
        return nums.pop();
    }
    
    /**
     * 计算两个数的结果
     */
    private static int cal(int num1, int num2, String s) {
        int res = 0;
        switch (s) {
            case "+":
                res = num1 + num2;
                break;
            case "-":
                res = num1 - num2;
                break;
            case "*":
                res = num1 * num2;
                break;
            case "/":
                res = num1 / num2;
                break;
        }
        return res;
    }
    View Code

    五、中缀表达式转后缀表达式

      源码:中缀转后缀

    1,思路

    1,初始化两个栈:运算符栈s1和储存中间结果的栈s22,从左至右扫描中缀表达式;
    3,遇到操作时,将其压s24,遇到运算符时,比较其与s1栈顶运算符的优先级:
        a.如果s1为空或栈顶运算符为左括号“(”,则直接将此运算符入s1栈
        b.如果s1不为空且当前符号优先级比栈顶运算符的高,也将此运算符入s1栈
        c.如果s1不为空,且当前符号优先级小于等于栈顶运算符,将 s1 栈顶的运算符弹出压入到 s2 中,再次转到(重复4-a.b.c)与 s1 中新的栈顶运算符相比较
    5,遇到括号时:
        a.如果是左括号“(”,则直接压入 s1
        b.如果是右括号“)”,则依次弹出 s1 栈顶的运算符,并压入 s2直到遇到左括号为止,此时将这一对括号丢弃
    6重复步骤 25,直到中缀表达式的最右边
    7将s1中剩余的运算符依次弹出并压入s2
    8,依次弹出s2中的元素并输出,结果的逆序即为中缀表达式对应的后缀表达式

                                      

    2,代码实现

    /**
     * 中缀表达式转后缀表达式
     */
    private static List<String> toSufixExpressionList(List<String> infixExpressionList) {
        java.util.Stack<String> s1 = new java.util.Stack<String>();
        List<String> s2 = new ArrayList<>();
        for (String s : infixExpressionList) {
            if (s.matches("\d+")) {
                s2.add(s);
            } else if (s.equals("(")) {
                s1.push(s);
            } else if (s.equals(")")) {
                while (true) {
                    String oper = s1.pop();
                    if (oper.equals("(")) {
                        break;
                    } else {
                        s2.add(oper);
                    }
                }
            } else {
                //运算符
                while (true) {
                    //如果 s1 为空,或栈顶运算符为左括号“(”,则直接将此运算符入栈;
                    if (s1.empty() || "(".equals(s1.peek())) {
                        s1.push(s);
                        break;
                    }
                    int topPri = priority(s1.peek());
                    int curPri = priority(s);
                    //若优先级比栈顶运算符的高,也将运算符压入 s1;
                    if (curPri > topPri) {
                        s1.push(s);
                        break;
                    } else {
                        //若优先级比栈顶运算符的小于等于 将 s1 栈顶的运算符弹出并压入到 s2 中,再次转到(4-1)与 s1 中新的栈顶运算符相比较
                        String topOper = s1.pop();
                        s2.add(topOper);
                    }
                }
            }
        }
    
        //将 s1 中剩余的运算符依次弹出并压入 s2
        while (!s1.empty()) {
            s2.add(s1.pop());
        }
        return s2;
    }
    
    /**
     * 获取优先级
     */
    private static int priority(String s) {
        if ("/".equals(s) || "*".equals(s)) {
            return 1;
        } else if ("+".equals(s) || "-".equals(s)) {
            return 0;
        } else {
            return -1;
        }
    }
    View Code

    六、完整版逆波兰计算器

      源码:完整版逆波兰

    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 java.util.Stack<String> stack = new java.util.Stack<>();
        static List<String> data = Collections.synchronizedList(new ArrayList<String>());
    
        /**
         * 去除所有空白符
         * @param s
         * @return
         */
        public static String replaceAllBlank(String s ){
            // \s+ 匹配任何空白字符,包括空格、制表符、换页符等等, 等价于[ f
    
    	v]
            return s.replaceAll("\s+","");
        }
    
        /**
         * 判断是不是数字 int double long float
         * @param s
         * @return
         */
        public static boolean isNumber(String s){
            Pattern pattern = java.util.regex.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();
            }
        }
    
    }
    View Code
  • 相关阅读:
    塔 · 第 二 条 约 定
    nyoj 325
    塔 · 第 一 条 约 定
    大一上
    Django之ORM
    mysql概念
    数据库索引
    使用pymysql进行数据库的增删改查
    sql注入攻击
    pymysql
  • 原文地址:https://www.cnblogs.com/bbgs-xc/p/14034326.html
Copyright © 2020-2023  润新知