用栈实现中缀表达式

package ArrayStack;

public class ArrayStackDemo {
    public static void main(String[] args) {
        String exp = "70+20*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 = ' ';
        String keepNum = "";//用于拼接多位数
        while (true) {
            ch = exp.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);
                }
            } else {
               // numStack.push(ch-48);
                keepNum += ch;
                if(index == exp.length()-1){
                    numStack.push(Integer.parseInt(keepNum));//字符串转为int类型
                }else {
                    if (operstack.isOper(exp.substring(index + 1, index + 2).charAt(0))) {
                        numStack.push(Integer.parseInt(keepNum));//字符串转为int类型
                        keepNum = "";
                    }
                }
            }
            index++;
            if (index >= exp.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);
        }
        System.out.println("表达式" + exp + "的结果为:" + numStack.pop());
    }
}

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

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.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;
        }
        this.stack[++top] = value;
    }

    public int pop() {
        if (isEmpty()) {
            System.out.println("栈空");
        }
        return stack[top--];
    }

    //遍历
    public void list() {//
        if (isEmpty()) {
            System.out.println("栈空");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.println(stack[i]);
        }
    }

    //返回运算符的优先级
    public int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else {
            return 0;
        }
    }

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

    public int cal(int n1, int n2, int oper) {
        int res = 0;
        switch (oper) {
            case '+':
                res = n1 + n2;
                break;
            case '-':
                res = n2 - n1;
                break;
            case '*':
                res = n1 * n2;
                break;
            case '/':
                res = n2 / n1;
                break;
        }
        return res;
    }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值