GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Aug-2005, 09:20
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 230
Kacyndra will become famous soon enough
Unhappy

infix to postfix


Hi
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  
Old 15-Aug-2005, 09:46
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 230
Kacyndra will become famous soon enough
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:
The algorithm is: For each item in the postfix expression from the left: if the item is a number push it on the stack if the item is an operator (+,-,*, or /) then pop two numbers off the stack make a calculation: the second number popped-operator-first number push the result on the stack When the loop is done the answer is the only item remaining on the stack. Parsing an Infix Expression with No Parentheses (like 2 * 3 - 48/4 -4 * 5) Two stacks are required, one for numbers and the other for operators. The algorithm is: For each item in the infix expression (no parens) from the right to the left If the item is a number then push it on the number stack. If the item is an operator (+,-,*, or /) and: the operator stack is empty or the operator on the top of the stack is higher in priority (* and / are higher in priority than + or -), then Pop an operator from the operator stack. Pop two numbers off the number stack. Calculate using second number-operator-first number. Push the result on the number stack. Push the item on the operator stack. Else push the item on the operator stack. After the loop, while the operator stack is not empty Pop an operator from the operator stack. Pop two numbers off the number stack. Calculate using second number-operator-first number. Push the result on the number stack. The answer is the last item in the number stack. Parsing an Infix Expression with Parentheses (like ((2*4-6/3)*(3*5+8/4))-(2+3)) One stack is required. The algorithm is: Have a string available to store items For each item in the infix (with parens) expression If the item is a number then add it to the string with a space if the item is an operator then While the stack is not empty and an operator is at the top and the operator at the top is higher priority that the item then = Pop the operator on the top of the stack = Add the popped operator to the string with a space Push the item on the stack else if the item is a left paren Push the item on the stack else if the item is a right paren Pop a thing off of the stack. while that thing is not a left paren = Add the thing to the string with a space = Pop a thing off of the stack While the stack is not empty Pop a thing off the stack. Add it to the string with a space. Remove the last character (a space) from the string.
__________________
Xrum!
  #3  
Old 15-Aug-2005, 11:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Kacyndra
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
[

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:
a*-b

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:
# Left parentheses are always pushed onto the stack
# When a right parenthesis is encountered, the symbol at the top of the stack is popped off the stack and copied to the output. Repeat until the symbol at the top of the stack is a left parenthesis. When that occurs, both parentheses are discarded.

This also gives you a mechanism to detect unbalanced parentheses within the algorithm instead of a separate pass through the input.



Regards,

Dave
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
"cannot convert from 'class X *' to 'class X'" ?? hard2clone C++ Forum 11 04-Aug-2005 07:45
C calculator proximo C Programming Language 5 09-Apr-2005 10:27
Postfix and Fedora Core 1 JdS Computer Software Forum - Linux 8 07-Oct-2004 07:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:43.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.