After doing some research, I learned about the postfix notation, and how to parse an expression.
My code converts to postfix notation, and then it evaluates it using the stack based method. For example:
Initial expression: 5 + 2^(4 - 1)
Postfix notation: 5 2 4 1 - ^ +
Result: 13
Now I am trying to expand this program to be able to evaluate expressions with functions, like sin, cos, log, for example: 5 + sin (2 + log (2)).
My initial idea was, before parsing it, to find these functions, evaluate their results, and replace it in the string. But I don’t think it’s a very good idea…
So how can I do this in an efficient way?
Here is some code written in c#, if it is of any use: http://pastebin.com/7wB81fyQ, but I would prefer some pseudo-code so that I understand what I am writing better… I’m not one of those guys who just copy and paste.
Treat these functions like any other operator plus braces. First calculate what’s in the braces, then execute the function.
Other than
+, which consumes two numbers,Sinconsumes only one number.