> In order for me to use RPN on this task, doesn't the input have to be in RPN, something like:
Yes, but that's probably going to be an internal interface within your program
I can see 3 steps from the information you have provided.
1. Turn mathematical expressions into computational expressions
Things like 2pi do not make much sense, so you need to resolve these
For example
"Sin(2pi*4.8)+exp(-pi)" would become "Sin(2*pi*4.8)+exp(-pi)"
2. Conversion of an infix expression to an postfix (RPN) expression
Basically, you take the string and break it up (tokenise) it into its component parts - 'sin' '(' '2' etc etc
Then you have to implement something which follows operator precedence rules to rearrange the expression into the unambiguous RPN form.
There are examples on the board of how to do this.
For example
"Sin(2*pi*4.8)+exp(-pi)" would become "2 pi * 4.8 * sin pi -1 * exp +"
3. Evaluate the RPN expression.
Easy when done with a stack
These 3 steps are well isolated from one another, so you can work on each one separately without worrying what the other two functions will look like.
Your main could be something like this
Code:
int main ( ) {
string inputline, infixline, rpnline;
// read input
resolve( inputline, infixline );
convert( infixline, rpnline );
evaluate( rpnline );
}
It's easy to make sure each one works (by passing known correct input and examining outpur) without having to depend on other functions working correctly.
> Should I check for "Undefined" mathematical functions?
Yes, you wouldn't want to just print out any old answer.
Even if the function is defined, say sqrt(), you don't want to be passing negative numbers to it.