JAVA数据结构与算法-栈(5)

/**
 * 1、如果是数值直接入数栈
 * 2、如果是符号
 *  2.1、操作栈为空,直接压栈
 *  2.2、不为空
 *      2.2.1、优先级大于栈顶操作符,直接压入
 *      2.2.2、优先级小于等于栈顶操作符,弹出栈顶操作符,再弹出两个数字计算
 * 
 * @author Clifton
 * @create 2019/12/2 - 14:21
 */
public class Calculator
{

    Stack numStack;
    java.util.Stack opeStack;

    public Calculator() {
        // 初始化栈
        numStack = new Stack(6);
        opeStack = new java.util.Stack();
    }

    public int calculate(String expression) throws Exception {
        int res = 0;
        // 用于记录strings的当前索引
        int size = 0;
        // 存放解析表达式后的数字与符号
        String[] strings = new String[11];

        StringBuffer sb = new StringBuffer();
        // 遍历表达式
        for (int i = 0; i < expression.length(); i++) {
            // 当前字符
            char cur = expression.charAt(i);
            if (isNum(cur)) {// 如果是数字加入sb
                sb.append(cur);
            }
            else {// 不是数字
                  // 将sb放入字符数组,并清空sb
                strings[size++] = sb.toString();
                sb.setLength(0);

                // 如果遇到“=”退出遍历
                if ("=".equals(String.valueOf(cur))) {
                    break;
                }
                // 将当前操作符放入字符数组
                strings[size++] = expression.substring(i, i + 1);
            }
        }
        // 遍历字符数组
        for (int i = 0; i < size; i++) {
            if (isNum(strings[i])) {// 如果是数字,压入数栈
                numStack.push(Integer.parseInt(strings[i]));
            }
            else {
                // 不是数字,循环执行,直到操作符栈为空,或 当前操作符大于操作栈栈顶操作符
                while (true) {
                    if (opeStack.isEmpty()) {
                        opeStack.push(strings[i]);
                        break;
                    }
                    else {
                        int topPriority = getPriority((String) opeStack.peek());
                        int curPriority = getPriority(strings[i]);
                        if (curPriority > topPriority) {
                            opeStack.push(strings[i]);
                            break;
                        }
                        else {
                            // 当前操作符小于操作栈栈顶操作符,弹出计算
                            int pres = doOpe();
                            // 将计算结果压入数栈
                            numStack.push(pres);
                        }

                    }
                }
            }
        }

        System.out.println("结果为:");
        while (true) {
            try {
                res = doOpe();
                numStack.push(res);
            }
            catch (Exception e) {
                break;
            }
        }

        return res;
    }

    /**
     * 弹出2个数栈和1个操作栈计算
     * 
     * @return
     * @throws Exception
     */
    private int doOpe() throws Exception {
        int res = 0;
        String topOpe = (String) opeStack.pop();
        int postNum = numStack.pop();
        if ("*".equals(topOpe)) {
            res = numStack.pop() * postNum;
        }
        else if ("/".equals(topOpe)) {
            res = numStack.pop() / postNum;
        }
        else if ("+".equals(topOpe)) {
            res = numStack.pop() + postNum;
        }
        else if ("-".equals(topOpe)) {
            res = numStack.pop() - postNum;
        }
        return res;
    }

    private int getPriority(String ope) {
        if ("*".equals(ope) || "/".equals(ope)) {
            return 2;
        }
        else {
            return 1;
        }
    }

    public static boolean isNum(char c) {
        String regex = "[0-9]$";
        String s = String.valueOf(c);
        return s.matches(regex);
    }

    public static boolean isNum(String s) {
        String regex = "[0-9]*$";
        return s.matches(regex);
    }

}

你可能感兴趣的:(数据结构与算法,数据结构,算法,java)