Thread: Just a postfix clarification.

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079

    Just a postfix clarification.

    With negative numbers ( using a unary '-' ), such as the examples:

    -6(5+3)/2
    (5+3)*(-2+1)

    How is this converted to postfix? I haven't really tackled the issue too deep but it seems like it may interfere with a minus in postfix. I'd wager a guess that these would be converted as:

    6-53+*2/
    53+2-1+*

    But in evaluating the second one, I could see myself ending up with a loose end at 7*. I hope you're following. If you know how these are handled, please tell me. Perhaps I should put parenthesis in the postfix such as:

    (-6)52+*2/
    53+(-2)1+*

    Any suggestion or answers on how this is handled? The way my program is set up, it's currently accepting a string, converting to a postfix string and evaluated to a long. Thanks.
    Last edited by SlyMaelstrom; 12-07-2005 at 05:59 PM.
    Sent from my iPadŽ

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Another possiblility I'm considering is that in the postfix string the instance unary '-' is replaced with another character. Perhaps a tilde or something similar.

    EDIT: Actually I think I got it. Since I'll have to add spaces to accomedate two digit numbers, I'll just have to do a check in converting to postfix to see if a '-' is unary and then just not add a space for that. Example:

    Code:
    -34(16+22)/2          /* infix */
    -34 16 22 + * 2 /     /* postfix */
    If you can think of something better or you think this is the best way, some input would be great.
    Last edited by SlyMaelstrom; 12-07-2005 at 06:31 PM.
    Sent from my iPadŽ

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    (5+3)*(-2+1)

    How is this converted to postfix? I haven't really tackled the issue too deep but it seems like it may interfere with a minus in postfix.
    Erm yes, I was having similar problemos. Postfix, doesn' t provide a fully working model of everything you'd like it to do. So a little bit of tinkering is needed.

    But before you look into that, these two sources are absolute gems, in regards to Postfix notation.

    http://www.spsu.edu/cs/faculty/bbrow...tures/postfix/
    http://www.qiksearch.com/articles/cs...ix-evaluation/

    And when you do actually get round to it. You'll be needing to use...

    1. The stack (Of course)
    2. The STL string class
    3. Vectors...much better than arrays
    4. Tokens.

    Don't look here if you want to think about this for yourself...

    http://cboard.cprogramming.com/showt...ulator+program

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    (5+3)*(-2+1)

    5 3 + 2 Uminus 1 + *

    edit: sry, didnt read your post carefully, so i guess your problem is with ambigous symbols. simply resolve them by defining a unique symbol for them:
    "-" (binary minus) becomes the symbol "-"
    "-" (unary minus) becomes the symbol "Uminus"
    Last edited by Raven Arkadon; 12-08-2005 at 09:49 AM.
    signature under construction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. Replies: 4
    Last Post: 03-12-2006, 02:17 PM
  3. calculating postfix problem
    By Axolotl in forum C Programming
    Replies: 2
    Last Post: 04-01-2004, 08:49 PM
  4. Infix to Postfix
    By dat in forum C Programming
    Replies: 6
    Last Post: 06-16-2003, 08:46 AM
  5. postfix
    By datainjector in forum C Programming
    Replies: 11
    Last Post: 11-20-2002, 08:33 PM