1

I have string math expression. I want to convert this expression to integer.

Example:

    char str[]="2*5";

So, I need to get 10 for this example in arduino. How can I do that ?

  • This is very close to your question and should help you to find the solution of refine the question https://stackoverflow.com/questions/9329406/evaluating-arithmetic-expressions-from-string-in-c – Julien Apr 16 '18 at 08:13

3 Answers3

1

a naive solution would be to split the expression (using regex or even sscanf [even though i do not recommend it] ) to numbers (cast the numbers to int\doubles of course) and operator(that could stay as a string ) and switch case the math operator action with the numbers (as in activating the relevant operator).

a better solution in my opinion is to use SY algorithem (wiki) to convert the expression to a Reverse Polish notation(wiki) which makes things much easier to parse and work with (especially if youll use a stack to parse it )

also found some useful answers here(evaluating from string), here (split string with math expression) and my personal favorite here (evaluate arithmetic from string)

Daniel
  • 106
  • 6
1

I did it with TinyExpr library. It worked like a charm !

https://github.com/codeplea/tinyexpr/blob/master/README.md

-1

You can't do this in quotation marks. Maybe you want just parse it to String:

int x = 2*5
String myString = String(x);
kacmak7
  • 131
  • 2
  • 9