![]() |
| | #1 |
| Divine Join Date: Oct 2007 Location: Earth(duh!)
Posts: 20
| 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. |
| Doink is offline | |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,372
| 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
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #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? |
| Doink is offline | |
| | #4 | |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| Quote:
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 | |
| CornedBee is offline | |
| | #5 | |||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,372
| Quote:
So, once we have expressed (B + C) as (+ B C), we can express (A * (B + C)) in prefix notation as (* A + B C). Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |||
| laserlight is offline | |
| | #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? |
| Doink is offline | |
| | #7 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| 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 |
| CornedBee is offline | |
| | #8 |
| (?<!re)tired Join Date: May 2006 Location: Portugal
Posts: 5,661
| 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. |
| Mario F. is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Expression: Convert infix notation to postfix notation. | Nutshell | C Programming | 7 | 02-27-2010 07:44 AM |
| CamelCase VS Hungarian notation, which is better? | meili100 | C++ Programming | 4 | 04-22-2007 09:31 PM |
| I need help with RPN notation!!! | schnoor22 | C++ Programming | 2 | 04-09-2007 05:12 PM |
| polish notation calc problem | deedlit | C Programming | 6 | 06-14-2004 10:17 PM |
| hungarian notation | confuted | C++ Programming | 2 | 07-28-2003 01:19 PM |