栈实现综合计算器 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;
    }

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

舒克日记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值