栈实现综合计算

在这里插入图片描述
主函数测试代码:

 public static  void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        //完成表达式的运算
        String expression="70+2*6-4";
        //创建两个栈,一个数栈,一个符号栈
        ArrayStack numStack=new ArrayStack(10);
        ArrayStack operStack=new ArrayStack(10);
        int index=0;
        int num1=0;
        int num2=0;
        int oper=0;
        int res=0;
        char ch=' ';  //将每次扫描得到的char保存到ch
        String keepnum="";//用于拼接多位数
        //开始while循环扫描expression
        while (true){
            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);
                        numStack.push(res);
                        operStack.push(ch);
                    }else{
                        operStack.push(ch);
                    }
                }else {
                    operStack.push(ch);//接收参数是int   有可能出错
                }
            }else {
                //扫描到的是数字
                //numStack.push(ch-48);
                /**
                 * 分析思路:
                 * 1.当处理多位数时,不能发现是一个数就立即入栈,因为他可能是多位数
                 * 2.在处理数时,需要向expression的表达式的index后再看一位,如果是数就继续进行扫描,如果是符号才入栈
                 * 3.因此我们需要定义一个变量字符串,用于拼接
                 */
                //处理多位数
                keepnum+=ch;
                //如果ch已经是expression的最后一位,就直接入栈
                if (index==expression.length()-1){
                    numStack.push(Integer.parseInt(keepnum));
                }else{
                //判断下一个字符是不是数字,如果是数字,就继续扫描,如果是字符,则入栈
                //注意是看后一位,不是index++
                    if(operStack.isoper(expression.substring(index+1,index+2).charAt(0))){
                        //如果后一位是运算符
                        numStack.push(Integer.parseInt(keepnum));
                        //重要
                        keepnum="";
                    }
              }
            }
            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);
    }
    //将数栈的最后数pop出来,就是结果
        int res2=numStack.pop();
        System.out.printf("表达式%s=%d\n",expression,res2);
    }
}
class ArrayStack{
    private int[] stack;
    private int maxSize;
    private int top=-1;
    //构造器
    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack=new int[maxSize];
    }
    //增加一个方法,可以返回当前栈顶的值,但是不是真正的pop
    public int peek(){
        return stack[top];
    }
    //栈满
    public boolean isFull(){
        return top == maxSize - 1;
    }
    //栈空
    public boolean isempty(){
        return top==-1;
    }
    //添加数据
    public void push(int data){
        if(isFull()){
            System.out.println("栈已满不可添加数据~");
        }else{
            top+=1;
            stack[top]=data;
        }

    }
    //提取数据
    public int pop(){
        if(isempty()){
            throw new RuntimeException("栈为空,没有数据可取~");
        }
        return stack[top--];
    }
    //遍历栈
    public void list(){
        if(isempty()){
            System.out.println("栈为空,没有数据~");
        }else{
            for (int i = top; i >=0 ; i--) {
                System.out.printf("%d\t",stack[i]);
            }
            System.out.println();
        }
    }
    //返回运算符优先级,优先级是由程序员来确定优先级使用数字表示
    //数字越大,则优先级就越高
    public int priority(int oper){
        if(oper=='*'||oper=='/'){
            return 1;
        }else if(oper=='+'||oper=='-'){
            return 0;
        }else{
            return -1;
        }
    }
    //判断是不是一个运算符
    public boolean isoper(char val){
        return val=='+'||val=='-'||val=='*'||val=='/';
    }
    //计算方法
    public int cal(int num1,int num2,int oper){
        int res=0;
        switch (oper){
            case '+':
                res=num2+num1;
                break;
            case '-':
                res=num2-num1;
                break;
            case '*':
                res=num2*num1;
                break;
            case '/':
                res=num2/num1;
                break;
        }
        return res;
    }

}

结果图:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值