This package can be used to improve our efficient mathematical expression calculations.
For example: This is a calculated formula for the game hit rate we've plotted.
Whether hit =a hit ratio – (b-level –a level) * (Hit factor (6) * b Dodge rate/100) +32
In order to look good, we turned him into: ahit-(BLV-ALV) * (6*beva/100) +32
Jeval is used in this case!!!!
Jeval:http://www.oschina.net/p/jeval is currently the latest version of 0.9.4 Beta
public class Evaltest {private static final double X_value = 1.0;private static final double y_value = 270;//testjeval/tes TJeval2 need to move the volume, TESTJEVAL3 increased the variable packaging method public static void main (string[] args) throws Exception {Testjeval (); TestJeval2 (); TestJeval3 ();} private static void Testjeval () throws Exception {String exp = "2 + (7-5) * 3.14159 * #{x} + sin (#{y})";//Compileevaluato R eva = new Evaluator (); Eva.putvariable ("X", Double.tostring (X_value)); Eva.putvariable ("Y", double.tostring (Y_value) );//Evaluatedouble result = double.parsedouble (Eva.evaluate (exp)); SYSTEM.OUT.PRINTLN (Result);//-> 2.0}public static void TestJeval2 () {//Our game Formula ahit-(BLV-ALV) * (6*beva/100) + 32String exp = "#{ahit}-(#{blv}-#{alv}) * (6*#{beva}/100) +32"; Evaluator eva = new Evaluator (); try {/** * Adds a variable to the Evaluator class instance. */eva.putvariable ("Ahit"), Eva.putvariable ("Blv", "ten"), Eva.putvariable ("ALV", "ten"), Eva.putvariable ("Beva", "5");/** * Simple output variable. *//*system.out.println (Eva.evaluate ("#{ahit}")); System.out.println (Eva.evaLuate ("#{blv}"); System.out.println (Eva.evaluate ("#{alv}")); System.out.println (Eva.evaluate ("#{beva}")) *///formula calculation System.out.println (EVA.EVALUATE (exp));} catch (Exception e) {e.printstacktrace ();}} public static void TestJeval3 () {//Our game Formula ahit-(BLV-ALV) * (6*beva/100) +32string exp = "ahit-(BLV-ALV) * (6*beva/100) +32" ; Evaluator eva = new Evaluator (); try {/** * Adds a variable to the Evaluator class instance. */eva.putvariable ("Ahit"), Eva.putvariable ("Blv", "ten"), Eva.putvariable ("ALV", "ten"), Eva.putvariable ("Beva", "5");//Formula Calculation System.out.println (eva.evaluate (Formatexpression (exp)));} catch (Exception e) {e.printstacktrace ();}} public static string Formatexpression (String exp) {//English letter variable is packaged with #{}: such as variable x,#{x}string re = "([a-za-z]+)"; Pattern pattern = pattern.compile (re); Matcher Matcher = Pattern.matcher (exp); StringBuffer sb = new StringBuffer (), while (Matcher.find ()) {matcher.appendreplacement (SB, "#{" + matcher.group (1) + "}") ;} Matcher.appendtail (SB); return sb.tostring ();}}
Java Mathematical Expression Parsing tool-Jeval