Revers expression to Polish Notation in Java

175 views Asked by At

I want to make Reverse Polish Notation algorithm (from simple expression), but my code isn't working. Can anyone explain me why?

First of all I split a String expressionto array String[] like this:

String[] arraySymbols = expression.split(REGEX_SPLIT_EXPRESSION_BY_ONE_CHARACTER);

Next step:

private final ArrayDeque<Expression> stackExpression = new ArrayDeque<>();
private final List<String> polishNotation = new ArrayList<>();

@Override
    public List<String> interpret(String expression) {
        final String REGEX_SPLIT_EXPRESSION_BY_ONE_CHARACTER = "(?<=\\D)(?=\\d|\\D)|(?<=\\d|\\D)(?=\\D)";

        String[] arraySymbols = expression.split(REGEX_SPLIT_EXPRESSION_BY_ONE_CHARACTER);

        for(String oneSymbolFormatString: arraySymbols) {
            char oneSymbolFormatChar = oneSymbolFormatString.charAt(0);
            Expression currentExpression;

            switch (oneSymbolFormatChar) {
                case '(':
                    currentExpression = new TerminalExpressionParenthesisOpen();
                    comparePriority(currentExpression);
                    break;
                case ')':
                    currentExpression = new TerminalExpressionParenthesisClosing();
                    comparePriority(currentExpression);
                    break;
                case '+':
                    currentExpression = new TerminalExpressionPlus();
                    comparePriority(currentExpression);
                    break;
                case '-':
                    currentExpression = new TerminalExpressionMinus();
                    comparePriority(currentExpression);
                    break;
                case '*':
                    currentExpression = new TerminalExpressionMultiply();
                    comparePriority(currentExpression);
                    break;
                case '/':
                    currentExpression = new TerminalExpressionDivide();
                    comparePriority(currentExpression);
                    break;
                default:
                    polishNotation.add(oneSymbolFormatString);
            }
        }

        while (!stackExpression.isEmpty()) {
            polishNotation.add(stackExpression.pop().getName());
        }

        return polishNotation;
    }

private void comparePriority(Expression currentExpression) {
        if(stackExpression.isEmpty() | currentExpression.getName().equals(TypeExpression.PARENTHESIS_OPEN.getName())) {
            stackExpression.push(currentExpression);
        } else if(currentExpression.getName().equals(TypeExpression.PARENTHESIS_CLOSING.getName())) {
            while (true) {
                if(!stackExpression.getFirst().getName().equals(TypeExpression.PARENTHESIS_OPEN.getName())) {
                    polishNotation.add(stackExpression.pop().getName());
                } else {
                    stackExpression.pop();
                    break;
                }
            }
        } else if(stackExpression.getFirst().getPriority() >= currentExpression.getPriority()) {
            polishNotation.add(stackExpression.pop().getName());
            stackExpression.push(currentExpression);
        } else if(stackExpression.getFirst().getPriority() < currentExpression.getPriority()) {
            stackExpression.push(currentExpression);
        }
    }

It contains a list of operations, their names, designations and priority:

public enum TypeExpression {
    NON_TERMINAL('\0',0),
    PARENTHESIS_OPEN('(',1),
    PARENTHESIS_CLOSING(')', 4),
    MINUS('-', 2),
    PLUS('+', 2),
    DIVIDE('/', 3),
    MULTIPLY('*', 3);

    private final char name;
    private final int priority;

    public String getName() {
        return String.valueOf(this.name);
    }

    public int getPriority() {
        return this.priority;
    }

    TypeExpression(char name, int priority) {
        this.priority = priority;
        this.name = name;
    }
}

  • For example. For this expression:
(6+10-4)/(1+1*2)+1

my code return correct result:

[6, 10, +, 4, -, 1, 1, 2, *, +, /, 1, +]

  • But for this expression:
(8+2*5)/(1+3*2-4)

my code return incorrect result:

[8, 2, 5, *, +, 1, 3, 2, *, 4, -, +, /]

Should work (correct) is:

8, 2, 5, *, +, 1, 3, 2, *, +, 4, -/
0

There are 0 answers