Let's say I have a string with 1+5*9+4 etc.. whatever it may be. How do I use C++ do calculate this? I know recursive functions work here, but how? I figured I could loop ..searching for the * first ( higher precedence ) and then the +. No idea. Would not be a recursive function, would it? What does this function look like for this example?

7 Years
Discussion Span
Last Post by Taywin

It sounds like you want to build an LR recursive descent parser. You build a binary tree to represent the equation. Evaluation is a recursive, Left-to-Right operation. There are a large number of texts that describe how to do this. It is your basic applied programming class contents.


You could do as SnowFall if and only if you are allowed to use other libraries to build the function. Otherwise, you better build your own parser first. Also, does the incoming string could contain parenthesis as well? Watch out for that as well...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.