Thread: Notation help!

  1. #1
    Divine
    Join Date
    Oct 2007
    Location
    Earth(duh!)
    Posts
    20

    Question Notation help!

    Hey guys, I need some help regarding prefix and postfix notations. I got myself confused like really bad. Correct me If i'm wrong...

    So here are the questions...

    infix > prefix notation

    a) A * (B+C) - D

    My answer is

    = * A (+BC) - D

    = *+A(BC) - D

    = *+-A B C D

    b) M / (E+Z) * N

    = / M (E+Z) * N

    = /+ (EZ) * m

    = /+* MEZ


    infix > postfix notation

    a) - A / * E B M

    = A / * E B M -

    = A / E B M - *

    = A E BM - * /


    b) - + J K * L M

    = + J K * L M -

    = J K * L M - +

    = J K L M - + *


    I don't think what i did is right... >_>

    Thanks....
    Last edited by Doink; 09-22-2008 at 02:18 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This does not really belong in C programming, so I have moved it to General Discussions.

    A simple method here is to work from expressions that bind tightest ("inner expressions", to put it in another way). So you have say, two terms and an operator, and you shuffle them around until you get the desired notation, then you consider the result a new term, and so find the next term and operator to work with. For example:
    A * (B+C) - D
    A * (+ B C) - D
    (* A + B C) - D
    - * A + B C D
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Divine
    Join Date
    Oct 2007
    Location
    Earth(duh!)
    Posts
    20
    Thanks for moving this topic.

    but why did you put "-" before "*"? didn't you say you have to work from the inner expression first? So it should be like this = * - A + B C D

    and also, what about the postfix? how do you begin to transform it?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    didn't you say you have to work from the inner expression first?
    Yes, and you insert at the front. So the later you handle an expression, the further to the front it appears.

    Postfix is pretty much the same, except that you append at the back.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    but why did you put "-" before "*"? didn't you say you have to work from the inner expression first?
    A * (B+C) - D is actually (A * (B + C)) - D
    So, once we have expressed (B + C) as (+ B C), we can express (A * (B + C)) in prefix notation as (* A + B C).

    So it should be like this = * - A + B C D
    The infix equivalent of that expression would be: (A - (B + C)) * D

    and also, what about the postfix? how do you begin to transform it?
    The idea is the same.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Divine
    Join Date
    Oct 2007
    Location
    Earth(duh!)
    Posts
    20
    Hey thanks again.

    Now I just need to know the process to convert from prefix to postfix.

    Do I need to convert it first to infix then to postfix?

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Oh, no. Infix is the messiest of notations. The others are much easier to work with.

    Prefix to postfix is quite easy with a recursive function. The function reads a token. If the token is a number, it writes it to the result. If it's an operator, it calls itself twice (for the two operands) and then writes the operator out.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Meanwhile, a visual trick to find the final expression is for you to first add all superfluous parenthesis following the usual order of evaluation rules for the expression at hand.

    1. A / B + C * D
    2. ( (A / B) + (C * D) )

    Then move the operator within each parenthesis either to the left or right of both its operands, depending on the notation you want, while keeping the operator still inside its set of parenthesis.

    3.prefix. ( + ( / A B) ( * C D) )
    3.postfix. ( (A B / ) ( C D * ) + )

    Then just remove the parenthesis.

    4.prefix. + / A B * C D
    4.postfix. A B / C D * +
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

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. CamelCase VS Hungarian notation, which is better?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2007, 09:31 PM
  3. I need help with RPN notation!!!
    By schnoor22 in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 05:12 PM
  4. polish notation calc problem
    By deedlit in forum C Programming
    Replies: 6
    Last Post: 06-14-2004, 10:17 PM
  5. hungarian notation
    By confuted in forum C++ Programming
    Replies: 2
    Last Post: 07-28-2003, 01:19 PM