I understand how to make an ordinary calculator with buttons + - * /. But I have difficulties to make a string, where there is one field and a button: enter image description here

I do not ask to give the finished code. Please clarify or give a hint how to approach this decision. Will it be correct: to break a mathematical expression into pieces / parts and put each into a variable, and then add it up? Or is there a different approach? I would be grateful for some external resource to read.

public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button buttonCalc; TextView result; EditText task; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonCalc =(Button) findViewById(R.id.btnCalc); } @Override public void onClick(View view) { } } 

3 answers 3

There is a huge number of approaches to solve this problem. The classic is as follows:

1) We split the source string into tokens (in your case they are identical to lexemes), for example, for the expression 3 + 4 * (5 + 2) it will be:

  String[] tokens={"3","+", "4", "*","(","5", "+", "2", ")"}; 

2) Further, according to the rules of the grammar of the conditional language, we perform the parsing of an array of tokens, the result of which will be a tree of instructions:

enter image description here

3) Now, when we have a tree of instructions, we begin to traverse the tree (usually recursively) - we actually perform the calculation:

  • Take the + sign on top of the tree
  • We find children (number 3 and * )
  • Since the * sign has more children , we are recurring
  • well, etc.
  • after returning from recursion instead of the sign * , we will have a value (in this case) - 28 - we substitute and add 28+3=31

You can of course plan it yourself, but kosher is considered an option when you take some kind of information in which according to the rules of this information you describe grammar and collects for you the sources that parse this grammar - this is usually a task for course students in Software Engineering or something like that.

Classic is ANTLR

  • So this can only be realized with recursion? even without using arrays? - Oleg Khegay
  • Any recursion task is reduced to a loop with arrays (in my opinion there is even such a theorem) - Barmaley

Here is the library https://github.com/mariuszgromada/MathParser.org-mXparser

Added to project

 dependencies { /*oter compiles*/ implementation group: 'org.mariuszgromada.math', name: 'MathParser.org-mXparser', version: '4.0.0' } 

Perform so

  String s = "(3+5)*2"; Expression e = new Expression(); e.setExpressionString(s); double res = e.calculate(); 

There are many more features there, pick it up - this is interesting.

    Once I was implementing such a thing, with which I could multiply and make double values. When I wrote this code, I was very young, so do not judge strictly) But you can take it for an idea: https://github.com/georrge1994/1.calculation

    • one
      In general, there is a Polish method of counting. You can convert the expression to the Polish version and then read it. Such a realization is much easier - George Chebotaryov