Java Mathematical Expression Parsing tool-Jeval

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.