How to calculate mathematical expressions in a string?

17

I'm making an application handle an expression. How do I calculate the result of an expression? For example: 3(-9)+50/2 . This expression is typed in TextView .

    
asked by anonymous 19.02.2015 / 23:11

3 answers

11

To catch the value of TextView you should use the getText() method and convert to string using toString()

TextView myTextView;
...
String data = myTextView.getText().toString();

Parse and evaluate with expr

You can use the expr package ( link ), this package interprets and calculates math expressions about floating-point numbers, such as 2 + 2 or cos(x/(2*pi)) * cos(y/(2*pi))

  • Installing:

    To install, import the expr.jar package into your project (you will need to compile the .java first if you download it from GitHub.) If you do not have make , then run the following command in the folder you extracted the download of GitHub run javac -O expr/*.java and then jar cf expr.jar expr/*.class .)

  • Adding to Activity :

    To use the package you can include it in MainActivity.java (or other Activity ) using import exp; (or something similar)

  • Using:

    For use with TextView , it would look something like:

    String data = myTextView.getText().toString();
    
    try {
        expr = Parser.parse(data);
        myTextView.setText(expr.value());
    } catch (SyntaxException e) {
        myTextView.setText(e.explain());
    }
    
  • ScriptEngine

    As user @Walkin said (using JavaScript engine may be overkill for a "simple" task), but still you can use it.

    The ScriptEngine is an ECMAScript / JavaScript engine to perform the calculation / operation, as per the answer in the SOen , you will have to use javax.script.* , which is available from JDK1.6 .

    Example with TextView :

    import javax.script.ScriptEngineManager;
    import javax.script.ScriptEngine;
    ...
    
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("JavaScript");
    
    String data = myTextView.getText().toString();
    System.out.println(engine.eval(data));
    

    To set your own TextView or to another TextView , use:

    TextView myTextView;
    ...
    String data = myTextView.getText().toString();
    myTextView.setText(engine.eval(data));
    
        
    19.02.2015 / 23:22
    12

    You need to build an evaluator of mathematical expressions. I did one in C, but it was for reverse Polish notation (which I do not know if it suits you).

    The TextBox will return a string which is the expression that needs to be evaluated, so you basically read the line of text and as you find an operator or number, in a stack / tree to be able to perform operations in the proper order, for example: 1 + (2 * 5) .

    • You read the first item: 1 is a number, then put it on the stack.
    • You read the second item: + is an operator that will be executed with 1 , put in the operator stack.
    • You read the third item: ( - parentheses - then there is a new which I must evaluate before returning the result to operate with the previous one - opens a branch in the decision tree.
    • you read the fourth item: 2 is a number then put in the new stack.
    • You read the fifth item: * is an operator - put in the operator stack, it will be executed with 2 .
    • You read the last item: ) - parentheses, it is the last one and closes a branch of the tree, then pulls the stack and solves the expressions.

    I do not know if it was good to understand, but it's basically a data structure exercise (cells and trees) where you find expressions that will operate in sequence.

    The precedence of operators, is the math that says and trees are bifurcations of nested expressions. If an operator or parentheses is missing, the tree / stack is incomplete and the expression fails automatically.

    If I'm not mistaken, you can represent this as a Finite Deterministic Automaton , I do not remember for sure.

    A code I found on the net:

    link

        
    20.02.2015 / 03:09
    5

    Pragmatic response (do not reinvent the wheel). Use one of many libraries to evaluate expressions in Java.

    Links:

    There are so many that it's easier to search directly on Google. Your challenge is to select the best library for your particular case.

    Sources

    20.02.2015 / 12:58