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

计算1+5*13等表达式

package com.hhit.stack;

public class Calculator {
    public static void main(String[] args) {
        String expression = "70*2*2-5+1+1"; // 15//如何处理多位数的问题?
        //创建两个栈,数栈,一个符号栈

        ArrayStack2 numStack = new ArrayStack2(10);
        ArrayStack2 operStack = new ArrayStack2(10);
        //定义需要的相关变量
        int index = 0;//用于扫描
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' '; //将每次扫描得到char保存到ch
        String keepNum="";

        while (true){
            if (index==expression.length()) break;
            ch=expression.substring(index,index+1).charAt(0);
            if (operStack.isOper(ch)){
                if (!operStack.isEmpty()){
                    if (operStack.priority(ch)<=operStack.priority(operStack.peek())){
                        num1=numStack.pop();
                        num2=numStack.pop();
                        oper=operStack.pop();
                        res=numStack.cal(num1,num2,oper);
                        operStack.push(ch);
                        numStack.push(res);
                    }else {
                        operStack.push(ch);
                    }
                }else {
                    operStack.push(ch);
                }
            }else {
                //numStack.push(ch-48);
                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++;
        }
        while (true){
            if (operStack.isEmpty()) break;
            num1=numStack.pop();
            num2=numStack.pop();
            oper=operStack.pop();
            res=numStack.cal(num1,num2,oper);
            numStack.push(res);
        }
        res=numStack.pop();
        System.out.println(res);
    }
}
class ArrayStack2{
    private int maxSize; // 栈的大小
    private int[] stack; // 数组,数组模拟栈,数据就放在该数组
    private int top = -1;// top表示栈顶,初始化为-1

    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }
    //返回栈顶结果
    public int peek(){
        return stack[top];
    }

    //判断是否空
    public boolean isEmpty() {
        return top == -1;
    }


    //栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }

    //入栈-push
    public void push(int value) {
        if (isFull()) {
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top] = value;
    }

    //出栈-pop, 将栈顶的数据返回
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空");
        }
        int value = stack[top];
        top--;
        return value;
    }

    //显示栈的情况[遍历栈], 遍历时,需要从栈顶开始显示数据
    public void list() {
        if (isEmpty()) {
            System.out.println("栈中没有数据");
            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;
        }
        if (oper=='+'||oper=='-'){
            return 0;
        }
        return -1;
    }

    //设置符号判断
    public boolean isOper(int cha){
        if (cha=='*'||cha=='/'||cha=='+'||cha=='-'){
            return true;
        }
        return false;
    }

    //计算方法
    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;
    }

}


你可能感兴趣的:(数据结构与算法(java))