用Java将表达式转换为波兰表示法

问题描述

我想制作反向波兰表示法(通过简单表达式),但是我的代码无法正常工作。 谁能解释我为什么?

首先,我将String expression拆分为array String[],如下所示:

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

下一步:

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);
        }
    }

它包含操作,其名称名称和优先级的列表:

public enum TypeExpression {
    NON_TERMINAL('\0',0),PARENTHESIS_OPEN('(',1),PARENTHESIS_CLOSING(')',4),MINUS('-',2),PLUS('+',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;
    }
}

  • 例如。对于此表达式:
(6+10-4)/(1+1*2)+1

我的代码返回正确结果:

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

  • 但是对于这种表达方式:
(8+2*5)/(1+3*2-4)

我的代码返回不正确结果:

[8,5,3,/]

应该工作(正确)是:

8,-/

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)