Is there a math expression analysis library on Android?

I currently have an iOS app that I am looking for for a port for Android. This application uses the Objective-C GCMathParser Infrastructure / Class.

I'm looking for a replacement on Android (or a way to get GCMathParser to work on Android) that can figure things out like:

2*2 (2+3)+2 ((2+3)+2)/5 (-2 + sqrt((-5^2)-4*2*3))/(2 * 2) 

I would like to be able to pass String to this (e.g. above) and return a result, preferably relatively accurate (e.g. double).

What options do I have for parsing mathematical expressions on Android?

+2
source share
4 answers

What about MVEL ?

 Map vars = new HashMap(); vars.put("x", new Integer(5)); vars.put("y", new Integer(10)); Integer result = (Integer) MVEL.eval("x * y", vars); 

You can also use simple Java calls like Math.sqrt(...)

+3
source

Take a look at one of these libraries:

+1
source

Evaluating expressions in order is fairly straightforward. If you need a link, check out the GNU expr program for the C source code. I have ported it to Java for my own projects. It is easy and simple: www.codeforge.com/read/126659/expr.c__html

+1
source

If you do not want to use the library or cannot find it, you can always use webView to solve this function using javascript.

here is an example of how you can do this (1 + 2 calculation):

 public class MainActivity extends Activity { private boolean _hasAnsweredQuestion =false; /** Called when the activity is first created. */ @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final WebView webView=(WebView)findViewById(R.id.webView1); webView.setWebChromeClient(new WebChromeClient()); webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(final WebView view,final String url) { super.onPageFinished(view,url); if(_hasAnsweredQuestion) return; _hasAnsweredQuestion=true; final String mathProblem="1+2"; final String newUrl="javascript:AndroidFunction.showToast("+mathProblem+");"; webView.loadUrl(newUrl); } }); webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new Object() { @JavascriptInterface public void showToast(final String webMessage) { Toast.makeText(MainActivity.this,webMessage,Toast.LENGTH_SHORT).show(); } },"AndroidFunction"); webView.loadUrl(" "); } } 
0
source

All Articles