Input math functions like cosine tangential logarithm in Android

I am trying to make a small math application in Android, but I find a little problem. My problem is that I want my application to correctly interpret the contents of EditText, so that it can accept not only numbers, but also mathematical changes, such as the sine-cosine tangent logarithm, etc. . I am not an expert programmer, and I have never encountered this type of problem before.

+4
source share
3 answers

You can also try the java formel parser. There is a bunch for them. For example: http://www.speqmath.com/tutorials/expression_parser_java/index.html

It might look like this:

Parser prs = new Parser(); EditText et = (EditText)findViewById(R.id.edittext); String expr = et.getText().toString(); String result = prs.parse(expr); 

To install it, you need to download: http://www.speqmath.com/tutorials/expression_parser_java/html/files/expression_parser_java.zip

And then you have to copy the java files to the source folder in eclipse.

Did you do it like that? If you could post an exception.

+2
source

You can try the following:

 EditText et = (EditText)additem.findViewById(R.id.edittext); String item = et.getText().toString(); String splitter[] = item.split("\\d+"); if(splitter[0].equals("sin")) // perform sine //similarly for other functions 
+2
source

You need a parser.

You can either follow the frugi tips, or use the java library, or you can implement your own.

A simple parser to implement and suitable for this problem is the Shunting Yard algorithm.

Pros using your own analyzer:

  • Significantly more control over grammar and expressions.
  • Good experience.
  • Fun

Minuses:

  • It takes more time to prepare (obviously) than a third-party library.
  • May be a mistake.

I would recommend writing your own!

+2
source

Source: https://habr.com/ru/post/1416526/


All Articles