使用两个堆栈评估中缀表达式

时间:2019-02-22 20:29:26

标签: java arrays math stack expression

我必须创建两个堆栈并评估像3 + 4 +(5 -2)这样的中缀表达式。我已经创建了一个代码,并使用postfix来创建基础,因为它更容易并且可以完美地工作!现在我遇到的问题是括号,我不知道如何检查括号是否平衡,然后再检查表达式是否正确。应该是这样的:

Type a balanced infix expression: 5 * (4 + 3)
The result is 35
Type a balanced infix expression: 10 / (2 + 1
Not balanced, try again :

这是测试员

import java.util.*;
public class InfixTester { //it's postfix at the moment
    public static void main(String[] args) {
        String expression, retry;
        int result;

        Scanner scan = new Scanner (System.in);

        do {
            InfixEvaluator evaluator = new InfixEvaluator(); //in reality is postfix rn
                System.out.println ("Type a balanced postfix expression one token at a time:");
                expression = scan.nextLine();

                result = evaluator.evaluate(expression);
                System.out.println();
                System.err.println("The result is " + result);

                System.out.print("Would you like to continue (y/n)? ");
                retry = scan.nextLine();
                System.out.println();
        }
        while (retry.equalsIgnoreCase("y"));
    }
}

这是一切的评估者

import java.util.*;
public class InfixEvaluator {  //it's actually postfix right now
    private final static char ADD = '+';
    private final static char SUBSTRACT =  '-';
    private final static char MULTIPLY = '*';
    private final static char DIVIDE = '/';

    private Stack <Integer> stack;
    public InfixEvaluator() {
        stack = new Stack <Integer>();
    }

    public int evaluate(String expr) {
        int op1, op2, result = 0;
        String token;
        Scanner parser = new Scanner (expr);

        while (parser.hasNext()) {
            token = parser.next();

            if (isOperator (token)) {
                op2 = (stack.pop()).intValue();
                op1 = (stack.pop()).intValue();
                result = evaluateSingleOperator(token.charAt(0), op1, op2);
                stack.push(new Integer (result));
            }
            else
                stack.push(new Integer (Integer.parseInt (token)));
        }
        return result;
    }

    private boolean isOperator (String token){
        return ( token.equals("+") || token.equals("-") ||
                token.equals("*") || token.equals("/"));
    }

    private int evaluateSingleOperator(char operation, int op1, int op2){
        int result = 0;

        switch  (operation){
            case ADD:
                result = op1 + op2;
                break;
            case SUBSTRACT:
                result = op1 - op2;
                break;
            case MULTIPLY:
                result = op1 * op2;
                break;
            case DIVIDE:
                result = op1 / op2;
                break;                                
        }
        return result;
    }
}

尽管所有这些对于后缀表达式都很好,但是我需要更改和添加什么才能正确评估后缀表达式?谢谢!

1 个答案:

答案 0 :(得分:1)

我假设这两个堆栈用于运算符和中间结果。您可以将开放式paren推入操作员堆栈。当遇到封闭的paren时,弹出堆栈并检查它是否为开放的paren。如果是这样,则无需执行任何操作。 “中间结果”堆栈的顶部是带括号的表达式的值。如果堆栈顶部不是开放式括号,请照常评估运算符。