极简计算器代码

计算器

//这里不知为何,有错误,没找到
下面展示一些 内联代码片

public class CalculatorExer {
    public static void main(String[] args) {

        //根据前面老师的思路,完成表达式的运算
        String expression = "37+6*2+2";
        ArrayStack3 numStack = new ArrayStack3(10);
        ArrayStack3 operStack = new ArrayStack3(10);

        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int res = 0;
        int oper = 0;
        char ch = ' ';
        String Keepnum = " ";

        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 = operStack.cal(num1,num2,oper);

                        numStack.push(res);
                        operStack.push(ch);

                    }else{
                        operStack.push(ch);
                    }
                }else{
                    operStack.push(ch);
                }
            }else{
                Keepnum += ch;
                if(index == expression.length() - 1){
                    numStack.push(Integer.parseInt(Keepnum));
                }else{
                    if(operStack.isOper(expression.substring(index + 1,index + 2).charAt(0))){
                        numStack.push(Integer.parseInt(Keepnum));
                        Keepnum = " ";
                    }
                }

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

        }

        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处理
        System.out.printf("表达式%s = %d",expression,numStack.pop());

    }


}

class ArrayStack3{
    private int maxSize;//栈的大小
    private int[] stack;//数组模拟栈,数据就放在该数组
    private int top = -1;//表示栈顶,初始化为-1

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

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

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

    public void push(int value){
        if(isFull()){
            System.out.println("栈已满");
            return;
        }
        top++;
        stack[top] = value;
    }
    public int pop(){
        if(isFull()){
            System.out.println("栈为空");
        }
        int value = stack[top];
        top--;
        return value;
    }

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

    public void list(){
        if(isEmpty()){
            System.out.println("栈空");
        }
        for(int i = top;i >= 0;i--){
            System.out.printf("stack[%d] = %d\n",i,stack[i]);
        }
    }

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

    //返回运行符的优先级,由程序员确定,使用数字表示
    //数字越大,优先级越高
    public int priority(int oper){
        if(oper == '*' || oper == '/'){
            return 1;
        }else if(oper == '+' || oper == '-'){
            return 0;
        }else{
            return -1;//假定目前表达式只有+,—,*,/
        }
    }
    //计算方法
    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 = num1 * num2;
                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、付费专栏及课程。

余额充值