murat
murat

Reputation: 13

Flutter calculator app -How can I calculate without closed bracket?

I am developing a new calculator app with using flutter. Also I'm using math_expression package. But somehow, when I opened a new bracket and write numbers, it is not calculating until I closing the bracket (calculator giving results as an Error. I just want the calculator to calculate the equation that I wrote before closing the bracket. How I can do that. Which code should I use for It can you explain shortly? There it is what kind of calculate I want]

Upvotes: 1

Views: 955

Answers (2)

ChilliPenguin
ChilliPenguin

Reputation: 715

Before parsing the string into the expression you can add the brackets into the string like so:

String currentExpression = "(2+(3*(5)";
  
currentExpression += ")" * ("\(".allMatches(currentExpression).length
                            -"\)".allMatches(currentExpression).length);

The code finds the remain amount of right brackets left in the string and adds it to the end. The above code returns: (2+(3*(5))) which should be parsable.

Upvotes: 1

Corvus Albus
Corvus Albus

Reputation: 185

I think you should assume in your code that all the bracket ends. This will be works in case your printscreen. But will be problem in moment like (3+(5* when you don't know by what digit or expression user want to multiply and close the bracket after all equations give error again. In this scenario is better to give exact information, to your user,what he/she don't write yet than assume something.

Upvotes: 1

Related Questions