美文网首页算法与数据结构数据结构和算法分析
用栈来实现简易版中缀表达式的计算器

用栈来实现简易版中缀表达式的计算器

作者: shengjk1 | 来源:发表于2020-02-16 10:38 被阅读0次

1.什么是栈
先进后出,元素的删除和插入只能在同一端的一种线性表

2.栈的实现方式
数组和链表都可以,本次使用数组

3.什么是中缀表达式
3+2-1*6+10

4.代码:

/**
 * @author shengjk1
 * @date 2020/2/13
 */
public class Calcaulator {
    public static void main(String[] args) {
        // 中缀表达式
        String expression = "5-1*6+2";
        //创建两个栈 数栈、符号栈
        ArrayStack1 numStack = new ArrayStack1(10);
        ArrayStack1 operStack = new ArrayStack1(10);
        
        //用于遍历
        int index = 0;
        int num1, num2 = 0;
        int oper = 0;
        int res = 0;
        //每次扫描得到的char
        char ch = ' ';
        //用来拼接多位数
        String keepNum = "";
        
        while (true) {
            ch = expression.substring(index, index + 1).charAt(0);
            //判断 ch 是什么,然后做相应的处理
            if (operStack.isOper(ch)) {
                //判断当前的符号栈是否为空
                if (!operStack.isEmpty()) {
                    //如果当前的操作符的优先级小于等于栈中符号的优先级,就需要从数栈中 pop  两个数
                    //在从符号栈中 pop 出一个符号,进行运算,将得到结果,入数栈,然后将当前的操作符入符号栈
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = numStack.cal(num1, num2, oper);
                        //运算的结果入数栈
                        numStack.push(res);
                        //然后将当前的操作符入符号栈
                        operStack.push(ch);
                    } else {
                        //如果当前的操作符大于栈中的操作符,就直接入符号栈
                        operStack.push(ch);
                    }
                } else {
                    //如果为空,直接入符号栈
                    operStack.push(ch);
                }
            } else {
                // 如果是数字则直接入数栈
//              numStack.push(ch - 48);
                //看 index 后一位,如果是数则继续进行扫描,如果不是则入栈
                keepNum += ch;
                if (index == expression.length() - 1) {
                    numStack.push(Integer.parseInt(keepNum));
                } else {
                    if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
                        numStack.push(Integer.parseInt(keepNum));
                        keepNum = "";
                    }
                }
            }
            //让 index +1,并判断是否扫描到 exoression 最后
            index++;
            if (index >= expression.length()) {
                break;
            }
        }
        
        //当表达式扫描完毕,就顺序的从数栈和符号栈中pop出相应的数和符号,并运行
        while (true) {
            if (operStack.isEmpty()) {
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1, num2, oper);
            //运算的结果入数栈
            numStack.push(res);
        }
        System.out.printf("表达式 %s = %d ", expression, numStack.pop());
    }
}

class ArrayStack1 {
    private int maxSize;
    private int[] stack;
    private int top = -1;
    
    public ArrayStack1(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }
    
    
    // 栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }
    
    //栈空
    public boolean isEmpty() {
        return top == -1;
    }
    
    //查看当前栈顶的值
    public int peek() {
        return stack[top];
    }
    
    //入栈
    public void push(int element) {
        if (isFull()) {
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top] = element;
    }
    
    //出栈
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("stack is empty!");
        }
        int temp = stack[top];
//      stack[top]=null; 防止内存泄露
        top--;
        return temp;
    }
    
    //
    public void list() {
        if (isEmpty()) {
            System.out.println("stack is empty");
            return;
        }
        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;
        }
    }
    
    /**
     * @param val
     * @return
     */
    public boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }
    
    /**
     * 计算
     *
     * @param num1
     * @param num2
     * @param oper
     * @return
     */
    public int cal(int num1, int num2, int oper) {
        int res = 0;
        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;
    }
}

5.栈的使用场景:
1.递归
2.方法调用
3.表达式的转化和求值
4.二叉树遍历
5.图的深度优先遍历
6.逆序输出 如 单链表的反转

6.面试题
如何用两个栈达到一个队列的效果

相关文章

  • Python 简单计算器-逆波兰后缀表达式实现

    中缀表达式 后缀表达式 简易计算器,可以通过栈来实现。然而如果直接使用中缀表达式,需要处理括号,而使用后缀表达式则...

  • 机试常用算法和题型-栈和队列专题

    堆栈+ordermap使用括号匹配 堆栈使用简单计算器 栈+队列实现中缀转后缀,计算后缀表达式 栈+队列计算,包括...

  • 用栈来实现简易版中缀表达式的计算器

    1.什么是栈先进后出,元素的删除和插入只能在同一端的一种线性表 2.栈的实现方式数组和链表都可以,本次使用数组 3...

  • 计算器

    使用Java写的一个可以计算+,-,*,/ 的计算器。首先用栈把中缀表达式转化成后缀表达式,再利用栈对后缀表达式求...

  • 算法之逆波兰计算器的分析与实现

    关于使用栈实现的普通计算器我之前已经实践过了,但是使用的是普通的中缀算术表达式的方式实现的,感兴趣可以看这篇文章:...

  • C语言中缀表达式计算器

    本文将介绍中缀表达式计算器的详细写法,是 C语言把中缀表达式转换为后缀表达式 和 C语言逆波兰计算器 的结合 ...

  • 用栈求后缀表达式的值

    类似于用栈求中缀表达式 不同的是 这里只需要一个栈 这个是在中缀表达式上改写的 借鉴 这个是for循环因为i每次循...

  • 栈和队列

    一.栈 栈的作用之一:利用栈后进先出的特点匹配括号,计算带运算符的算法(也就是中缀表达式) 可以把中缀表达式转化为...

  • 数据结构与算法--后缀表达式

    中缀表达式转后缀表达式 中缀表达式转后缀表达式的思路步骤分析。 初始化一个栈和一个队列,运算符栈 S1 和存储中间...

  • 中缀表达式

    学习算法,开始想到用栈了,但是没有解决优先级的问题。上网一查,原来中缀表达式更适合计算机阅读,转换成中缀表达式后,...

网友评论

    本文标题:用栈来实现简易版中缀表达式的计算器

    本文链接:https://www.haomeiwen.com/subject/flkpfhtx.html