线性栈实现中缀表达式计算器

线性栈实现中缀表达式计算器

线性栈实现中缀表达式计算器
方法代码:
以(10+20/2*3)/2+8为例(计算结果为28):

private static int evaluateExpression(String expression){
        //创建两个栈,一个存放符号,一个存放数字
        ArrayStack<Character> operatorStack = new ArrayStack<>();
        ArrayStack<Integer> numberStack = new ArrayStack<>();
        expression = insertBlanks(expression);//空格插入方法
        String[] tokens = expression.split(" "); //按空格分割后用作遍历
        for (String token : tokens) { //token ==tokens[i]
            //过滤空串
            if (token.length() == 0) {
                continue;
            } else if (token.equals("+") || token.equals("-")) {
                while (!operatorStack.isEmpty() && (operatorStack.peek() == '+' || operatorStack.peek() == '-' || operatorStack.peek() == '*' || operatorStack.peek() == '/')) {
                    processAnOperator(numberStack, operatorStack);//弹栈计算方法
                }
                //如果符号栈为空或栈顶是(则直接将符号入栈
                operatorStack.push(token.charAt(0));
            } else if (token.equals("*") || token.equals("/")) {
                while (!operatorStack.isEmpty() && (operatorStack.peek() == '*' || operatorStack.peek() == '/')) {
                    processAnOperator(numberStack, operatorStack);
                }
                operatorStack.push(token.charAt(0));
            } else if (token.equals("(")) {
                operatorStack.push(token.charAt(0));
            } else if (token.equals(")")) {
                while (operatorStack.peek() != '(') {
                    processAnOperator(numberStack, operatorStack);
                }
                operatorStack.pop();
            } else { //遍历到数字
                numberStack.push(new Integer(token));
            }
        }
        while (!operatorStack.isEmpty()){
            processAnOperator(numberStack, operatorStack);
        }
        return numberStack.pop();
    }

空格插入和弹栈计算

总体思路:
线性栈实现中缀表达式计算器
空格插入方法

//遍历字符串,遇到非数字字符,在新字符串中加入空格c空格,遇到数字直接加入,
    //而后以空格为分隔符分割就不会把例如10变成1,0
    private static String insertBlanks(String expression){
        StringBuilder sb = new StringBuilder();
        for (int i = 0;i < expression.length();i++){
            char c = expression.charAt(i);
            if (c=='(' || c==')' || c=='+' || c=='-' || c=='*' || c=='/'){
                sb.append(' ');
                sb.append(c);
                sb.append(' ');
            }else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}

弹栈计算方法:

 //符号和数字弹栈计算,并将结果入栈
    private static void processAnOperator(ArrayStack<Integer> numberStack, ArrayStack<Character> operatorStack){
        char op = operatorStack.pop();
        int num1 = numberStack.pop();
        int num2 = numberStack.pop();
        if (op == '+'){
            numberStack.push(num2 + num1);
        }else if (op == '-'){
            numberStack.push(num2 - num1);
        }else if (op == '*'){
            numberStack.push(num2 * num1);
        }else {
            numberStack.push(num2 /num1);
        }
    }

总体源代码:

package p2.线性结构;
//中缀表达式计算器
public class InfixCalculator {
    public static void main(String[] args) {
        String experssion = "(10+20/2*3)/2+8";
        int result = evaluateExpression(experssion);
        System.out.println(result);
    }

    private static int evaluateExpression(String expression){
        //创建两个栈,一个存放符号,一个存放数字
        ArrayStack<Character> operatorStack = new ArrayStack<>();
        ArrayStack<Integer> numberStack = new ArrayStack<>();
        expression = insertBlanks(expression);
        String[] tokens = expression.split(" "); //按空格分割后用作遍历
        for (String token : tokens) { //token ==tokens[i]
            //过滤空串
            if (token.length() == 0) {
                continue;
            } else if (token.equals("+") || token.equals("-")) {
                while (!operatorStack.isEmpty() && (operatorStack.peek() == '+' || operatorStack.peek() == '-' || operatorStack.peek() == '*' || operatorStack.peek() == '/')) {
                    processAnOperator(numberStack, operatorStack);
                }
                //如果符号栈为空或栈顶是(则直接将符号入栈
                operatorStack.push(token.charAt(0));
            } else if (token.equals("*") || token.equals("/")) {
                while (!operatorStack.isEmpty() && (operatorStack.peek() == '*' || operatorStack.peek() == '/')) {
                    processAnOperator(numberStack, operatorStack);
                }
                operatorStack.push(token.charAt(0));
            } else if (token.equals("(")) {
                operatorStack.push(token.charAt(0));
            } else if (token.equals(")")) {
                while (operatorStack.peek() != '(') {
                    processAnOperator(numberStack, operatorStack);
                }
                operatorStack.pop();
            } else { //遍历到数字
                numberStack.push(new Integer(token));
            }
        }
        while (!operatorStack.isEmpty()){
            processAnOperator(numberStack, operatorStack);
        }
        return numberStack.pop();
    }

    //符号和数字弹栈计算,并将结果入栈
    private static void processAnOperator(ArrayStack<Integer> numberStack, ArrayStack<Character> operatorStack){
        char op = operatorStack.pop();
        int num1 = numberStack.pop();
        int num2 = numberStack.pop();
        if (op == '+'){
            numberStack.push(num2 + num1);
        }else if (op == '-'){
            numberStack.push(num2 - num1);
        }else if (op == '*'){
            numberStack.push(num2 * num1);
        }else {
            numberStack.push(num2 /num1);
        }
    }

    //遍历字符串,遇到非数字字符,在新字符串中加入空格c空格,遇到数字直接加入,
    //而后以空格为分隔符分割就不会把例如10变成1,0
    private static String insertBlanks(String expression){
        StringBuilder sb = new StringBuilder();
        for (int i = 0;i < expression.length();i++){
            char c = expression.charAt(i);
            if (c=='(' || c==')' || c=='+' || c=='-' || c=='*' || c=='/'){
                sb.append(' ');
                sb.append(c);
                sb.append(' ');
            }else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}

上一篇:1045 Favorite Color Stripe


下一篇:AcWing 848.有向图的拓扑序列