【发布时间】:2018-04-04 15:20:33
【问题描述】:
我有一个任务,我需要编写一个遵循 BODMAS 法则的计算器。我已经为计算器编写了代码,但它没有正确通过 BODMAS 规则。这是我的代码: 公共类计算器{
import java.util.ArrayList;
import java.util.regex.Pattern;
public class Calculator {
String expression;
public Calculator(String expr) {
this.expression = expr.replaceAll("\\s+", "");
}
public double evalExpr() {
return eval(expression);
}
private double eval(String input) {
if (input.contains("-")) {
String[] operands = input.split("\\-", 2);
return eval(operands[0]) - eval(operands[1]);
} else if (input.contains("+")) {
String[] operands = input.split("\\+", 2);
return eval(operands[0]) + eval(operands[1]);
} else if (input.contains("*")) {
String[] operands = input.split("\\*", 2);
return eval(operands[0]) * eval(operands[1]);
} else if (input.contains("/")){
String[] operands = input.split("\\/", 2);
return eval(operands[0]) / eval(operands[1]);
} else if (input.matches("[0-9]*")) {
return Integer.parseInt(input);
} else {
throw new RuntimeException();
}
}
}
public class Main {
public static void main(String[] args) {
String input = "6*8/7+6-9+5/4*3-8+6";
Calculator calc = new Calculator(input);
double answer = calc.evalExpr();
System.out.println(answer);
}
}
但是这个版本由于某种原因没有通过 BODMAS。计算器应返回 ~ 5.61 作为答案,但返回 14.10。我不明白为什么我认为 Java 会自动为你做 BODMAS。谁能弄清楚这里出了什么问题?
谢谢。
【问题讨论】:
-
所以你希望java知道你碰巧想用字符串做一个计算器?可悲的是它无法读懂你的想法
-
@dave 你能提供任何实际的帮助还是只提供一些无聊的、不必要的 cmets?
-
看起来如果你在 eval 中重新排序 if-else-if,你会得到不同的结果,尽管我还不确定排序应该是什么。 @dave 你读过代码吗?它编译并运行。
-
我知道它编译他们在说为什么 java 没有得到正确的答案,而不是为什么它不起作用,我只是说原因是他们创建的不是 java
-
你不能用这么简单的代码制作一个兼容 BODMAS 的计算器。您将需要某种完整的解析器,例如LALR。