用变量评估数学表达式. (java 8)
作者:互联网
我希望获得有关此问题的答案(Evaluating a math expression given in string form)的其他帮助.@ Boann用户使用非常有趣的算法回答了该问题,他还指出该算法可以更改为接受变量.我设法对其进行了修改,并使它正常工作,但不知道他如何区分编译和评估.这是我的代码:
import java.util.HashMap;
import java.util.Map;
public class EvaluateExpressionWithVariabels {
@FunctionalInterface
interface Expression {
double eval();
}
public static void main(String[] args){
Map<String,Double> variables = new HashMap<>();
for (double x = 100; x <= +120; x++) {
variables.put("x", x);
System.out.println(x + " => " + eval("x+(sqrt(x))",variables).eval());
}
}
public static Expression eval(final String str,Map<String,Double> variables) {
return new Object() {
int pos = -1, ch;
//if check pos+1 is smaller than string length ch is char at new pos
void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}
//skips 'spaces' and if current char is what was searched, if true move to next char return true
//else return false
boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
Expression parse() {
nextChar();
Expression x = parseExpression();
if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
return x;
}
// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)`
// | number | functionName factor | factor `^` factor
Expression parseExpression() {
Expression x = parseTerm();
for (;;) {
if (eat('+')) { // addition
Expression a = x, b = parseTerm();
x = (() -> a.eval() + b.eval());
} else if (eat('-')) { // subtraction
Expression a = x, b = parseTerm();
x = (() -> a.eval() - b.eval());
} else {
return x;
}
}
}
Expression parseTerm() {
Expression x = parseFactor();
for (;;) {
if (eat('*')){
Expression a = x, b = parseFactor(); // multiplication
x = (() -> a.eval() * b.eval());
}
else if(eat('/')){
Expression a = x, b = parseFactor(); // division
x = (() -> a.eval() / b.eval());
}
else return x;
}
}
Expression parseFactor() {
if (eat('+')) return parseFactor(); // unary plus
if (eat('-')){
Expression b = parseFactor(); // unary minus
return (() -> -1 * b.eval());
}
Expression x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.'){
nextChar();
}
double xx = Double.parseDouble(str.substring(startPos, this.pos));
x = () -> xx;
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z') nextChar();
String func = str.substring(startPos, this.pos);
if ( variables.containsKey(func)){
x = () -> variables.get(func);
}else{
double xx = parseFactor().eval();
if (func.equals("sqrt")) x = () -> Math.sqrt(xx);
else if (func.equals("sin")) x = () -> Math.sin(Math.toRadians(xx));
else if (func.equals("cos")) x = () -> Math.cos(Math.toRadians(xx));
else if (func.equals("tan")) x = () -> Math.tan(Math.toRadians(xx));
else throw new RuntimeException("Unknown function: " + func);
}
} else {
throw new RuntimeException("Unexpected: " + (char)ch);
}
if (eat('^')){
x = () -> {
double d = parseFactor().eval();
return Math.pow(d,d); // exponentiation
};
}
return x;
}
}.parse();
}
}
如果你看看他的回答,他的主要内容
public static void main(String[] args) {
Map<String,Double> variables = new HashMap<>();
Expression exp = parse("x^2 - x + 2", variables);
for (double x = -20; x <= +20; x++) {
variables.put("x", x);
System.out.println(x + " => " + exp.eval());
}
}
他在这行上调用函数parse表达式exp = parse(“ x ^ 2-x 2”,variables);一次编译表达式,并使用for使用唯一的x值对其多次求值.解析函数指的是什么.
ps:我对用户问题发表了评论,没有回复.
解决方法:
对困惑感到抱歉.我提到的“ parse”函数只是现有的eval函数,但由于它返回一个Expression对象而被重命名.
因此,您将拥有:
public static Expression parse(String str, Map<String,Double> variables) { ... }
并通过以下方式调用它:
Map<String,Double> variables = new HashMap<>();
Expression exp = parse("x+(sqrt(x))", variables);
for (double x = 100; x <= +120; x++) {
variables.put("x", x);
System.out.println(x + " => " + exp.eval());
}
另一件事:有必要在解析时知道名称是指变量还是函数,以便知道它是否带有参数,但是在解析期间您不能在变量映射上调用containsKey,因为在调用exp.eval()之前,变量可能不会出现在映射中!一种解决方案是将函数放在映射中,因此可以在其上调用containsKey:
} else if (ch >= 'a' && ch <= 'z') { // functions and variables
while (ch >= 'a' && ch <= 'z') nextChar();
String name = str.substring(startPos, this.pos);
if (functions.containsKey(name)) {
DoubleUnaryOperator func = functions.get(name);
Expression arg = parseFactor();
x = () -> func.applyAsDouble(arg.eval());
} else {
x = () -> variables.get(name);
}
} else {
然后在类级别的某个地方初始化函数映射:
private static final Map<String,DoubleUnaryOperator> functions = new HashMap<>();
static {
functions.put("sqrt", x -> Math.sqrt(x));
functions.put("sin", x -> Math.sin(Math.toRadians(x)));
functions.put("cos", x -> Math.cos(Math.toRadians(x)));
functions.put("tan", x -> Math.tan(Math.toRadians(x)));
}
(将函数映射定义为解析器内部的局部变量也是可以的,但这会在每次解析过程中增加更多的开销.)
标签:recursive-descent,abstract-syntax-tree,java 来源: https://codeday.me/bug/20191112/2023743.html