我需要关于这个问题的额外帮助:如何计算以字符串形式给出的数学表达式。用户@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
: Expression exp = parse("x^2 - x + 2", variables);
,以便编译一次表达式,并使用for循环评估多个具有唯一x值的表达式。 parse
函数是什么意思。注:我已经评论了用户的问题但没有得到回复。
Expression parse() { nextChar(); Expression x = parseExpression(); if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch); return x; }
- Thibstarseval()
吧? - Thibstarseval()
而不是解析它,它会尝试解决方程,如果variables
不包含x
,则会出现错误。@Thibstars - HozeisExpression
之后放置的,这意味着表达式拥有某种记忆,但现在不再具备。因此,您只需要在循环内调用eval(String, Map)
即可,之前的parse
不再需要。 - Roland