# Question regarding RPN and error detection

• 10-01-2004
Roule
Question regarding RPN and error detection
My task is too write a tree-based calculator program using RPN, that reads input from a file with commands delimited by ";".

I have done my data-flow charts, and tree diagram

Two potential issues occured to me, and after a few hours reading havent been able to resolve:

1 - [Resolved]

The following was provided as an example line of input in the file:
Code:

`Sin(2pi*4.8)+exp(-pi)`
In order for me to use RPN on this task, doesn't the input have to be in RPN, something like:
Code:

` 2 pi 4.8 pi * * + exp`
*

2 - [Resolved]

When error checking, while I have implemented an input validation, that can check for the right operators, numbers, and correct number of parentheses. Should I check for "Undefined" mathematical functions? Like 0/1. Or should I let them pass, and return undefined as an evaluation of the answer.

Would appreciate any form of advice.

*Or similar, not totally familiar with the RPN notation

• 10-02-2004
Salem
> 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.
• 10-02-2004
Roule
Thanks for that comprehensive and fast response salem, excellent.

You mentioned that RPN evaluation is easily done with a stack, while not relevant to this thread, is that to say that using a tree is more difficult?
• 10-02-2004
Salem
> is that to say that using a tree is more difficult?
Just different.
You can store the entire RPN in a tree and then evaluate it.
A stack you store and evaluate as you go.
• 10-02-2004
Roule