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 ?
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 ?
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)
You can't do this in quotation marks. Maybe you want just parse it to String:
int x = 2*5
String myString = String(x);