Java数据结构与算法——计算器代码实现2(栈)(中缀)(多位数)

计算器代码实现2

承接上一篇
实现多位数计算器的思路
1.如果出现多位数,不能就一个数直接入栈。
2.需要定义一个变量字符串keepNum,用于拼接,来保存多位数。
3.在处理多位数时,需要expression的表达式的index位再后再看一位,如果是数就再进行循环扫描,如果是符号,数keepNum(这里的keepNum是字符型的,需转换成数字)才入栈。
关键代码如下:

				//处理多位数
                keepNum += ch;
                //如果ch是expression是最后一位,直接入栈
                if (index == expression.length()-1){
                    numstack.push(Integer.parseInt(keepNum));
                }else {
                    //下一字符是数字就继续扫描,遇字符才入栈。
                    //注意是扫描到最后数字
                    if (operstack.isOper(expression.substring(index+1,index+2).charAt(0))){
                        //扫描到字符串,则入栈keepNum。keepNum是字符型用Integer转换成数字型
                        numstack.push(Integer.parseInt(keepNum));
                        //清空KeepNum
                        keepNum = "";
                    }
                }

完整代码如下:

package stack;

public class Calculator {
    public static void main(String[] args) {
        //计算器的测试
        String expression = "70+20*60-4";
        //首先创建两个栈,一个数据栈,一个符号栈
        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 = ' ';//用于存放算式的字符形式
        String keepNum = "";
        while(true){
            //依次得到expression的每一个字符
            ch = expression.substring(index,index+1).charAt(0);
            //判断ch是什么,然后做相应的处理
            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);
                }
            }else{//如果是数,直接入数栈。
                //多位数计算思路分析
                //1.如果时多位数,不能就一个数直接入栈
                //2.在处理数,需要expression的表达式的index后再看一位,如果是数就进行扫描,如果是符号,数keepNum才入栈。
                //3.需要定义一个变量字符串,用于拼接

                //处理多位数
                keepNum += ch;
                //如果ch是expression是最后一位,直接入栈
                if (index == expression.length()-1){
                    numstack.push(Integer.parseInt(keepNum));
                }else {
                    //下一字符是数字就继续扫描,遇字符才入栈。
                    //注意是扫描到最后数字
                    if (operstack.isOper(expression.substring(index+1,index+2).charAt(0))){
                        //扫描到字符串,则入栈keepNum。keepNum是字符型用Integer转换成数字型
                        numstack.push(Integer.parseInt(keepNum));
                        //清空KeepNum。重要!!!
                        keepNum = "";
                    }
                }


            }
            //让index+1,并判断是否扫描到expression最后,完成后退出while循环。
            index++;
            if (index >= expression.length()){
                break;
            }
        }
        //扫描完expression,
        while(true){
            //判符号栈是否空,空则计算完成,退出
          if (operstack.isEmpty()){
              break;
          }
            num1 = numstack.pop();
            num2 = numstack.pop();
            oper = operstack.pop();
            res = numstack.cal(num1,num2,oper);
            numstack.push(res);//
        }
        int res2 = numstack.pop();
        System.out.printf("表达式%s = %d",expression,res2);//数据栈最后一个为计算结果,输出结果
    }
}
//
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 isFull(){
        return top == maxSize-1;
    }
    //判空
    public boolean isEmpty(){
        return top == -1;
    }
    //入栈 push
    public void push(int value){
        //判满
        if (isFull()){
            System.out.printf("栈满。\n");
            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;
        }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 = num1 + num2;
                break;
            case '-':
                res = num2 - num1;//注意顺序
                break;
            case '/':
                res = num2 / num1;//注意顺序
                break;
            case '*':
                res = num1 * num2;
                break;
            default:
                break;
        }
        return res;
    }

}

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值