20-12-17 基于栈做一个计算器

package Stack;

public class Calculator {
    public static void main(String[] args) {
        String expression = "7*3+3*6-3+1";
        ArrayStack2 numberStack = 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) {
            ch = expression.substring(index, index + 1).charAt(0);
            if (operStack.isOper(ch)) {
                if (!operStack.isEmpty()) {
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
                        num1 = numberStack.pop();
                        num2 = numberStack.pop();
                        oper = operStack.pop();
                        res = numberStack.cal(num1, num2, oper);
                        numberStack.push(res);
                        operStack.push(ch);
                    } else {
                        operStack.push(ch);
                    }
                } else {
                    operStack.push(ch);
                }
            } else {
                keepNum += ch;// 这个是字符串的拼接
                if (index == expression.length() - 1) {
                    numberStack.push(Integer.parseInt(keepNum)); //直接调用包装类的方法,转变成Int型
                } else {
                    if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
                        numberStack.push(Integer.parseInt(keepNum));
                        keepNum = "";
                    }
                }
            }
            index++;
            if (index >= expression.length()) {
                break;
            }
        }
        while (true) {
            //如果符号栈为空,则计算到最后的结果, 数栈中只有一个数字【结果】
            if (operStack.isEmpty()) {
                break;
            }
            num1 = numberStack.pop();
            num2 = numberStack.pop();
            oper = operStack.pop();
            res = numberStack.cal(num1, num2, oper);
            numberStack.push(res);//入栈
        }
        //将数栈的最后数,pop出,就是结果
        int res2 = numberStack.pop();
        System.out.printf("表达式 %s = %d", expression, res2);
    }
}


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

    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize]; //这里其实是覆盖了之前的数组长度
    }

    //判断栈满   是在ArrayStack内部的方法,所以可以直接调用内部的参数    只要有返回数就是有返回值的,这里是布尔数
    public boolean isFull() {
        return top == maxSize - 1;
    }

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

    //向栈中添加数  push
    public void push(int merber) {
        if (isFull()) {
            System.out.println("栈满,添加失败");
        } else {
            stack[++top] = merber;
        }
    }

    //栈中弹出数     当需要有返回值的时候,需要返回异常
    public int pop() {
        int temp;
        if (isEmpty()) {
            throw new RuntimeException("栈满,弹出失败");
        } else {
            temp = stack[top--];
            return temp;
        }
    }

    public void show() {
        if (isEmpty()) {
            System.out.println("栈空");
            return;
        } else {
            for (int i = 0; i < top; i++) {
                System.out.printf("  %d  ", 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 peek() {
        return stack[top];
    }

    //计算的方法
    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 = num1 / num2;
                break;
            default:
                break;
        }
        return res;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值