栈实现综合计算器 java 详细讲解

栈实现综合计算器 java 详细讲解

案例

在这里插入图片描述

解题思路

在这里插入图片描述

代码实现

package com.qf.stack;

public class Caculator {
    public static void main(String[] args) {
        StackArray numArray=new StackArray(20);
        StackArray operateArray=new StackArray(20);
        int index=0;
        int num1=0;
        int num2=0;
        int res=0;
        String funNum="";
        String expression="7*2*2-5+1-5+3-4";
        for (int i = 0; i < expression.length(); i++) {
            char ch=expression.subSequence(i,i+1).charAt(0);
            if (StackArray.isOperate(ch)){
                //3.1 如果发现当前的符号栈为 空,就直接入栈
                //3.2 如果符号栈有操作符,就进行比较,如果当前的操作符的优先级小于或者等于栈中的操作符, 就需要从数栈中pop出两个数,在从符号栈中pop出一个符号,进行运算,将得到结果,入数栈,然后将当前的操作符入符号栈,
                // 如果当前的操作符的优先级大于栈中的操作符, 就直接入符号栈.
                if (operateArray.isEmpty()){
                    operateArray.push(ch);
                }else{
                    int peek = operateArray.peek();
                    int local = operateArray.priority((char) peek);
                    int chars = operateArray.priority(ch);
                    if (local>=chars){
                        num1 = numArray.pop();
                        num2 =numArray.pop();
                        int pop = operateArray.pop();
                        int cal = StackArray.cal(num1, num2, (char) pop);
                        numArray.push(cal);
                        operateArray.push(ch);
                    }else{
                        operateArray.push(ch);
                    }
                }
            }else{
                funNum+=ch;
                if (i==expression.length()-1){
                    numArray.push(Integer.parseInt(funNum));
                }else {
                    if (StackArray.isOperate(expression.subSequence(i + 1, i + 2).charAt(0))) {
                        numArray.push(Integer.parseInt(funNum));
                        funNum="";
                    }
                }
            }
        }
        while (true){
            if (operateArray.isEmpty()){
                break;
            }
            num1 = numArray.pop();
            num2 =numArray.pop();
            int pop = operateArray.pop();
            int cal = StackArray.cal(num1, num2,  pop);
            numArray.push(cal);
        }
        if (operateArray.isEmpty()){
            res=numArray.pop();

            System.out.println("结算得到的结果:"+expression+":"+res);
        }
    }
}

class StackArray {
    public int maxSize;
    public int top;
    public int [] stack;

    //初始化
    public StackArray(int maxSize){
        this.maxSize=maxSize;
        this.top=-1;
        this.stack=new int[maxSize];
    }

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

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

    //入栈
    public void push(int num){
        if (isFull()){
            System.out.println("栈已满,请出栈~~~");
        }else{
            top++;
            stack[top]=num;
        }
    }

    //获取栈顶元素
    public int peek(){return stack[top];}
    //出栈
    public int pop(){
        if (isEmpty()){
            throw new RuntimeException("栈为空,请入栈~~~");
        }
        int value=stack[top];
        top--;
        System.out.println("出栈的值为:"+value);
        return value;
    }

    //循环栈元素
    public void show(){
        if (isEmpty()){
            throw new RuntimeException("栈为空,请入栈~~~");
        }

        for (int i = top; i >=0 ; i--) {
            System.out.println("a["+i+"]"+"="+stack[i]);
        }
    }

    //判断是否为算数符
    public static boolean isOperate(char num){
        return num=='+'||num=='-'||num=='*'||num=='/';
    }

    //判断字符串等级,*或/为高等级,+ -为低等级,字符为0
    public  int priority(char num){
        int result=0;
        if (num=='+'||num=='-'){
            result=-1;
        }else if (num=='*'||num=='/'){
            result=1;
        }else{
            result=0;
        }
        return result;
    }

    //计算数值
    public static int cal(int num1,int num2,int character){
        int res=0;
        switch (character){
            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 @ 2022-08-30 22:41  雾托邦  阅读(22)  评论(0编辑  收藏  举报