栈java数组实现,栈实现综合计算器(中缀表达式)

6.栈

6.1栈的介绍

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

在这里插入图片描述

应用场景

  1. 子程序的调用:在跳往子程序前,会先将下个指令的地址存到堆栈中,直到子程序执行完后再将地址取出,以回到原来的程序中。
  2. 处理递归调用:和子程序的调用类似,只是除了储存下一个指令的地址外,也将参数、区域变量等数据存入堆栈中。
  3. 表达式的转换[中缀表达式转后缀表达式]与求值(实际解决)。
  4. 二叉树的遍历。
  5. 图形的深度优(depth 一 first)搜索法。

6.2数组实现栈

在这里插入图片描述

class ArrayStack{
    private int maxSize;
    private int[] stack;
    private int top;
    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
        top = -1;
    }
    public boolean isFull(){return top==maxSize-1;}
    public boolean isEmpty(){return top ==-1;}
    //入栈
    public void push(int value){
        if (isFull())
            throw new RuntimeException("栈满!!!");
        top++;
        stack[top] = value;
    }
    //出栈
    public int pop(){
        if (isEmpty())
            throw new RuntimeException("栈空!!!");
        top--;
        return stack[top+1];
    }
    //从栈顶开始输出栈内的数据
    public void list(){
        for (int i=top;i>=0;i--)
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
    }
}

6.3栈实现综合计算器(中缀表达式)

在这里插入图片描述

代码实现

public class Calculator {
    public static void main(String[] args) {
        String expression = "3-2*6-2";
        ArrayStack2 numStack = new ArrayStack2(10);
        ArrayStack2 operStack = new ArrayStack2(10);
        int index = 0;  //扫描expression的下标
        int num1 = 0;
        int num2 = 0;
        int oper;      //存放操作符
        int res =0;     //存放暂时的运算结果
        char ch = ' ';  //将扫描结果暂存在ch中
        String keepNum = "";  //拼接字符串解决多位数的问题

        while (index<expression.length()){
            //依次获得表达式的每一个字符
            ch = expression.charAt(index);

            //判断字符是否是运算符
            if (operStack.isOper(ch)){
                if (operStack.isEmpty()){//如果是空栈,则直接将运算符入栈
                    operStack.push(ch);
                }else {
                    if (operStack.priority(operStack.peek())>=operStack.priority(ch)){//判断ch与栈顶运算符的优先级谁高
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = operStack.cal(num1,num2,oper);
                        System.out.println(res);
                        numStack.push(res); //将与运算结果放入数栈

                        // 这里不可以直接将运算符进行入栈,因为-和+优先级一样,应该继续算下去
                        // 假如入栈之后,会在后面的while循环中,先算减号,再算前面的加号,单实际上先算后面的减号时,需要减号变加号
                        // 括号前面是减号,括号里面要变号
                        //operStack.push(ch);

                        continue;
                    }else {//如果ch的优先级更高,则将ch压入oper栈中
                        operStack.push(ch);
                    }
                }
            }else {
                //处理多位数的情况
                keepNum+=ch;
                if (index==expression.length()-1){
                    numStack.push(Integer.parseInt(keepNum));
                }else {
                    //当下一位是运算符时将keepNum压入栈中,如果不是则继续for循环,前几位数已经保存在了keepNum中
                    if (operStack.isOper(expression.charAt(index+1))){
                        numStack.push(Integer.parseInt(keepNum));
                        keepNum="";
                    }
                }
            }
            index++;
        }

        //表达式扫描完毕之后,顺序从数栈和符号栈取出数和符号进行运算
        while (!operStack.isEmpty()){
            oper = operStack.pop();
            num1 = numStack.pop();
            num2 = numStack.pop();
            res = operStack.cal(num1,num2,oper);
            numStack.push(res); //将与运算结果放入数栈
            System.out.println(res);
        }
        res = numStack.pop();
        System.out.println(expression+"最后的结果为:"+res);
    }
}
class ArrayStack2{
    private int maxSize;
    private int[] stack;
    private int top;

    public int getMaxSize() {
        return maxSize;
    }

    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
        top = -1;
    }

    public boolean isFull(){
        return top==maxSize-1;
    }
    public boolean isEmpty(){
        return top ==-1;
    }

    //返回栈顶的值,不出栈
    public int peek(){
        return stack[top];
    }

    //入栈
    public void push(int value){
        if (isFull())
            throw new RuntimeException("栈满!!!");
        top++;
        stack[top] = value;
    }
    //出栈
    public int pop(){
        if (isEmpty())
            throw new RuntimeException("栈空!!!");
        top--;
        return stack[top+1];
    }

    //从栈顶开始输出栈内的数据
    public void list(){
        for (int i=top;i>=0;i--)
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
    }

    //返回运算符的优先级,数字越大优先级越高
    public int priority (int oper){
        if (oper=='*'||oper=='/')
            return 1;
        else if (oper=='+'||oper=='-')
            return 0;
        else
            return -1;
    }

    //判断是否是一个运算符
    public boolean isOper(int val){
        return val=='+'||val=='-'||val=='*'||val=='/';
    }

    //执行计算
    public int cal(int num1,int num2,int oper){
        int res = 0; // res 用于存放计算的结果
        switch (oper) {
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;// 注意顺序
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1;
                break;
            default:
                break;
        }
        return res;
    }
}
posted @ 2021-08-18 20:47  文戌  阅读(63)  评论(0编辑  收藏  举报