【数据结构】栈实现计算器

1.用栈实现计算器:

在这里插入图片描述

public class test {
    public static void main(String[] args) {
        String e = "70+2*6-4";
        Stack num = new Stack(10);
        Stack operate = new Stack(5);
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' ';
        String keepNum = "";
        while (true){
            ch = e.substring(index,index+1).charAt(0);
            if (num.isOper(ch)){
                if (operate.isEmpty()){
                    operate.push(ch);
                }
                else{
                    if (num.caprty(ch)<=num.caprty(operate.getTop())){
                        num1 = num.pop();
                        num2 = num.pop();
                        oper = operate.pop();
                        res = num.cault(num2,num1,oper);
                        num.push(res);
                        operate.push(ch);
                    }
                    else{
                        operate.push(ch);

                    }

                }
            }else{
                keepNum+=ch;
                if (index==e.length()-1){
                    num.push(Integer.parseInt(keepNum));
                }else{
                if (num.isOper(e.substring(index+1,index+2).charAt(0))){
                    num.push(Integer.parseInt(keepNum));

                    keepNum = "";
                }

            }}
            index++;
            if (index>=e.length()){
                break;
            }
        }

        while (true){
            if (operate.isEmpty()){
                break;
            }
            num1 = num.pop();
            num2 = num.pop();
            oper = operate.pop();
            res = num.cault(num2,num1,oper);
            num.push(res);

        }

        System.out.println(e+"="+num.pop());




    }
}

class Stack{
    private int maxSize;
    private int[] stack;
    private int top = -1;

    public Stack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];

    }

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

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

    public int getTop(){
        return stack[top];
    }

    public void push(int e){
        if (isFull()){
            System.out.println("满了");
            return;
        }
        top++;
        stack[top] = e;
    }

    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.println(stack[i]);
       }
    }

    public int caprty(int oper){
        if (oper=='*'||oper=='/'){
            return 1;
        }
        if (oper=='+'||oper=='-'){
            return 0;
        }
        return -1;
    }

    public boolean isOper(char oper){
        return oper=='*'||oper=='/'||oper=='+'||oper=='-';

    }

    public int cault(int num,int num1,int oper){
        int res = 0;
        switch (oper){
            case '+':
                res = num + num1;
                break;
            case '-':
                res = num - num1;
                break;
            case '*':
                res = num * num1;
                break;
            case '/':
                res = num / num1;
                break;
            default:
                break;

        }
        return res;
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mr_树先森

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

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

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

打赏作者

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

抵扣说明:

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

余额充值