![]() |
|
#1
|
||||
|
||||
infix to postfixHi
i was wondering you anyone could tell me where i can get the logic for the postfix.... i tried to do it myself, but it comes out wrong all the time... what i have so far is this: if digit, then put into postfix if operator, then check for higher value operator in stack if found, pop that and stack the new operator if not found, put the operator into the stack ............. is that right? what do i do wirht "(" and ")" thansk __________________
Xrum! |
||||
|
#2
|
||||
|
||||
|
i finally found it.
for those who might need it in the future here it is: (also, can someone who knows what htey'r doing look over it, and let me know if this is correct or not?) pleas Code:
__________________
Xrum! |
|
#3
|
|||
|
|||
|
Quote:
Is this a complete algorithm for converting infix to postfix and then evaluating the postfix? I usually separate them conceptually. And also in the implementation. For example parentheses never appear in a postfix expression. That's one of the reasons that postfix was invented: rewrite arbitrarily complicated arithmetic expressions in a way that that doesn't need parentheses to enforce operator precedence. It's easier for me to debug things if I keep them separate (but that's just me --- Your Mileage May Vary). For conversion from infix to postfix, there is an operator stack, an input string and an output string (not a "stack for numbers"). Of course you can evaluate the output stuff as you go, rather than have a separate pass for evaluation the postfix. Anyhow, for conversion from infix to postfix: Be sure to define operator precedence correctly. How about unary - and unary +? They are usually allowed, and sometimes really screw up the works if the algorithm is developed in an "ad hoc" manner. For example, does your program have to handle this? Quote:
Also, your first post asked about parentheses. Now I have seen examples where they simply treated '(' and ')' as operators at the highest precedence level, but left out a few details. Parentheses are something special, not just another operator, and not just another roadside attraction. Consider something like the following snippet (from http://www.spsu.edu/cs/faculty/bbrow...tures/postfix/) Quote:
This also gives you a mechanism to detect unbalanced parentheses within the algorithm instead of a separate pass through the input. Regards, Dave |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| "cannot convert from 'class X *' to 'class X'" ?? | hard2clone | C++ Forum | 11 | 04-Aug-2005 06:45 |
| C calculator | proximo | C Programming Language | 5 | 09-Apr-2005 09:27 |
| Postfix and Fedora Core 1 | JdS | Computer Software Forum - Linux | 8 | 07-Oct-2004 06:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The