Question regarding RPN and error detection

This is a discussion on Question regarding RPN and error detection within the C++ Programming forums, part of the General Programming Boards category; My task is too write a tree-based calculator program using RPN, that reads input from a file with commands delimited ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    19

    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

    Edit: Question answered by Salem
    Last edited by Roule; 10-02-2004 at 12:53 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    19
    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?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    19
    Thanks again for your help.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21