java - 将表达式反转为 Java 中的波兰表示法
问题描述
我想制作反向波兰表示法算法(从简单的表达式),但我的代码不起作用。谁能解释我为什么?
首先,我将 a 拆分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('+', 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;
}
}
- 例如。对于这个表达式:
(6+10-4)/(1+1*2)+1
我的代码返回正确的结果:
[6, 10, +, 4, -, 1, 1, 2, *, +, /, 1, +]
- 但是对于这个表达式:
(8+2*5)/(1+3*2-4)
我的代码返回不正确的结果:
[8, 2, 5, *, +, 1, 3, 2, *, 4, -, +, /]
应该工作(正确)是:
8, 2, 5, *, +, 1, 3, 2, *, +, 4, -/
解决方案
推荐阅读
- python - 防止进程从 ProcessPoolExecutor BrokenProcessPool 添加
- java - 谷歌地图活动某种错误
- scala - scalacheck:提升自定义随机生成器
- python - 如何从这个 BloomBerg 网站刮掉五张桌子?
- reactjs - 如何阻止随机链接使用 react-router 打开?
- elasticsearch - 设置 Logstash 时遇到问题
- python - 从另一个 Lambda 函数中终止一个 Lambda 函数
- php - 列的总和在连接表 PHP 中不起作用
- java - 如果条件失败,则打破最里面的 for 循环
- c - 循环不变时的段错误