利用栈来实现简单的表达式计算

class Sta{
    public static void main(String[] args) {
        Sta sta1 = new Sta(10);
        Sta sta2 = new Sta(10);
        String expr = "3+2*6-2";
        char[] chars = expr.toCharArray();
        int n1 =0;
        int n2 = 0;
        int ope=0;
        int res =0;
        for (int i = 0; i < chars.length; i++) {
            char e = chars[i];
            if (sta1.isOper(e)) {//1.是不是符号
                if (sta2.isEmpty()) {//2-1).符号栈是空
                    sta2.push(e);
                }else{//2-2)不是空
                    if (sta2.priority(e) <= sta2.priority(sta2.peek())) {//3-1判断优先级
                        n1 = sta1.pop();
                        n2 = sta1.pop();
                        ope = sta2.pop();
                        res = sta1.cal(n1, n2, ope);
                        sta1.push(res);
                        sta2.push(e);
                   }else {
                        sta2.push(e);//因为这是字符的1,对应的十进制是49 ,字符转成十进制
                    }

                }
            }else {
                sta1.push(e-48);

            }

        }
        while (!sta2.isEmpty()) {
            n1 = sta1.pop();
            n2 = sta1.pop();
            ope = sta2.pop();
            res = sta1.cal(n1, n2, ope);
            sta1.push(res);

        }
        System.out.println("is over");
        System.out.println("sta1.pop() = " + sta1.pop());

    }
    int top;
    int max;
    int [] arr;

    public Sta(int max) {
        this.max = max;
        arr = new int[max];
        top = -1;
    }
    public int peek(){
        return arr[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 = num2/num1;
            default:break;
        }
        return res;
    }
//是不是一个运算f符
    public boolean isOper(char val) {
        return val=='*'||val=='/'||val=='-'||val=='+';
    }
//优先级
    public int priority(int mark) {
        if (mark == '*' || mark == '/') {
            return 1;
        } else if (mark == '+' || mark == '-') {
            return 0;
        }else{
            return -1;
        }
    }

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

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

    public void push(int val) {
        if (isFull()) {
            System.out.println("isFull");
            return;
        }
        top++;//这是先找位置,然后赋值
        arr[top]=val;
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("isEmpty");
        }
        int res = arr[top];
        top--;
        return res;
    }

    public void show() {
        if (isEmpty()) {
            System.out.println("isEmpty");
            return;
        }
        for (int i = top; i >=0 ; i--) {
            System.out.println(arr[i]);

        }
    }
}

主要就是新的符号优先级小,我们就先计算。
2.就是字符变成真正数字的转换。

发布了66 篇原创文章 · 获赞 0 · 访问量 773

猜你喜欢

转载自blog.csdn.net/Be_With_I/article/details/104190438