个人学习:数据算法和结构之中缀表达式2021-06-03

中缀表达式实现多位数加减乘除

package dataStructure;

public class Calculator {
    public static void main(String[] args) {
        String express = "30+2*3-2+4/2";// 考虑多位数
        // 数字栈
        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 = express.substring(index, index + 1).charAt(0);
            //判断ch是数字还是运算符
            if (operStack.isOper(ch)) {// 运算符
                //判断符号栈是否为空
                if (!operStack.isEmpty()) {
                    // 不为空,需要判断优先级
                    //如果符号栈有操作符,就进行比较,如果当前的操作符的优先级小于或者等于栈中的操作符,就需要从数栈中pop出两个数,
                    //在从符号栈中pop出一个符号,进行运算,将得到结果,入数栈,然后将当前的操作符入符号栈
                    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 == express.length() - 1) {
                    numberStack.push(Integer.parseInt(keepNum));
                } else {
                    // 处理多位数,如果后面是数字,继续扫描,直到后面是符号
                    if (operStack.isOper(express.substring(index + 1, index + 2).charAt(0))) {
                        //是运算符,就将该多位数存进数栈
                        numberStack.push(Integer.parseInt(keepNum));
                        keepNum = "";// 恢复初始化
                    }
                }

            }

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

        //当表达式扫描完毕,就顺序的从 数栈和符号栈中pop出相应的数和符号,并运行.
        while (true) {
            // 如果符号栈为空,则表示已经计算了最后结果,数字栈中只有一个数字
            if (operStack.isEmpty()) {
                break;
            }
            num1 = numberStack.pop();
            num2 = numberStack.pop();
            oper = operStack.pop();
            res = numberStack.cal(num1, num2, oper);
            numberStack.push(res);
        }
        // 控制台打印最后结果
        System.out.printf("表达式 %s = %d", express, numberStack.pop());
    }
}

class ArrayStack2 {
    // 大小
    private int maxSize;
    // 数组
    private int[] myStack;
    // 默认栈顶
    private int top = -1;

    // 创建栈
    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        myStack = new int[this.maxSize];
    }

    // 判断栈空
    public boolean isEmpty() {
        return top == -1;
    }

    // 增加一个方法,查看栈顶的值,不是出账
    public int peek() {
        return myStack[top];
    }

    // 判断栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }

    // 栈遍历
    public void list() {
        // 先判断栈是否为空
        if (isEmpty()) {
            System.out.println("栈空,没有数据~~~~~");
            return;
        }
        // 遍历
        for (int i = top; i >= 0; i--) {
            System.out.printf("stack[%d]=%d\n", i, myStack[i]);
        }
    }

    // 添加栈数据 进栈
    public void push(int value) {
        //判断栈已满
        if (isFull()) {
            System.out.println("栈已满");
            return;
        }
        // 添加
        top++;
        myStack[top] = value;
    }

    // 出栈
    public int pop() {
        // 先判断栈空
        if (isEmpty()) {
            throw new RuntimeException("栈空,没有数据~~~~~~~~");
        }
        int result = myStack[top];
        top--;
        return result;
    }

    // 返回运算符优先级 程序员来定义。优先级使用数字表示
    // 数字越大,优先级越高
    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 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;
            default:
                break;
        }
        return res;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值