四则运算混合计算机java实现

不带括号。

package.数组模拟栈.中缀计算器;

public class Calculator {
    public static void main(String[] args) {
        String s = "22+4*2-1"; // 9
        ArrayStack numStack = new ArrayStack(10);
        ArrayStack operStack = new ArrayStack(10);
        int num1 = 0, num2 = 0, oper = 0, res = 0, index = 0;
        char ch = ' ';
        String plus = "";

        while (true) {
            ch = s.substring(index, index + 1).charAt(0);
            // 如果ch是数,则直接入栈
            if (numStack.isNum(ch)) {
                plus += ch;
                if (index == s.length() - 1) { // 临界条件,表达式末尾减1情况
                    numStack.push(Integer.parseInt(plus));
                }
                if (index != s.length() - 1) {
                    if (operStack.isOper(s.substring(index + 1, index + 2).charAt(0))) { // 如果后一位是运算符,则不拼接,直接将当前数压栈
                        numStack.push(Integer.parseInt(plus));
                        plus = "";
                    }
                }
            }
            index++;
            if (index >= s.length()) {
                break;
            }

            // 如果ch是符号
            if (operStack.isOper(ch)) {
                // 如果符号栈为空,则直接入栈
                if (operStack.isEmpty()) {
                    operStack.push(ch);
                    continue;
                }
                // 如果符号栈不为空,则判断符号优先级
                if (!operStack.isEmpty()) {
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())) { // 栈顶运算符优先级高,先取出运算,勿忘最后将当前运算符压栈
                        oper = operStack.pop();
                        num2 = numStack.pop();
                        num1 = numStack.pop();
                        res = numStack.cal(num1, num2, oper);
                        numStack.push(res);
                        operStack.push(ch);
                    }
                    if (operStack.priority(ch) > operStack.priority(operStack.peek())) {
                        operStack.push(ch);
                    }
                }
            }
        }
        while (true) {
            if (operStack.isEmpty()) { // 数栈栈底为结果
                break;
            }
            num2 = numStack.pop();
            num1 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1, num2, oper);
            numStack.push(res);
        }
        int finalRes = numStack.pop();
        System.out.println("表达式s=" + s + "计算结果为:" + finalRes);

    }

}

class ArrayStack {
    int top = -1; // 栈顶默认为-1
    private int[] stack = null;
    private int maxSize; // 栈的最大容量

    public ArrayStack(int size) {
        maxSize = size;
        stack = new int[size];
    }

    public void push(int value) {
        if (top == maxSize - 1) {
            throw new RuntimeException("栈满,元素不能入栈");
        }
        stack[++top] = value;
    }

    public int pop() {
        if (top == -1) {
            throw new RuntimeException("栈空,没有元素可以出栈");
        }
        int value = stack[top--];
        return value;
    }

    public void list() { // 遍历栈的时候不能移动top,因为出栈方法才可去移动top
        int temp = top;
        for (int i = temp; i >= 0; i--) {
            System.out.println(stack[temp--]);
        }
    }

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

    public boolean isNum(char ch) {
        return ch != '+' && ch != '-' && ch != '*' && ch != '/';
    }


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

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

    public int priority(int ch) {
        if (ch == '*' || ch == '/') {
            return 1;
        }
        if (ch == '+' || ch == '-') {
            return 0;
        }
        return -1;
    }

    public int peek() {
        return stack[top];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值