Dans l'un de mes projets, je souhaite ajouter une fonctionnalité où l'utilisateur peut fournir une formule, par exemple
sin (x + pi)/2 + 1
que j'utilise dans mon application Java
/**
* The formula provided by the user
*/
private String formula; // = "sin (x + pi)/2 + 1"
/*
* Evaluates the formula and computes the result by using the
* given value for x
*/
public double calc(double x) {
Formula f = new Formula(formula);
f.setVar("x", x);
return f.calc();
// or something similar
}
Comment évaluer des expressions mathématiques ?