Link to home
Start Free TrialLog in
Avatar of jpipitone
jpipitone

asked on

Evaluating a Postfix Expression

I am looking for code to compile in Visual C++ 6.0 or .NET that simply evaluates a Postfix expression using stacks (pop, push, etc).  I was working on a different program that converted infix to postfix expressions, but it's not what I needed.  Can anyone help?

Thanks
Avatar of jpipitone
jpipitone

ASKER

This is actually VERY Urgent.
Check this out,
http://programmersheaven.com/zone3/cat478/24213.htm

Note: This code has some Borland specific functions in it. It shouldn't be very hard to remove them, however, becuase they are used int the user interface and not the actual algorithm.

Exceter
This definitely helps, but I cannot have that function in there to convert, and the borland stuff I can't have either.  Basically I have no time on my hands to do this right now and it's needed ASAP!!

Thank you though, I can accept that for an answer, can you give me suggestions on how to get rid of unneeded code?
A postfix evaluator is REALLY easy:

See a string of digits?  evaluate them, push on stack.

See an operator?  Do it to the top two stack elements.

Can be done in under a page of code, no sweat.

Just attack it one step at a time, you'll eventually get it.

Look friend,
    When you are evaluating a postfix expression we assume that the operands are of sigle digit. Here you have to create two stacks. One for the operators and the other for the operands. Now read the input string one charecter at a time. If its a number push it into the operands stack and if its an operator(+, -, *, /) push it into the operators stack. Now at any point of time you see that a high priority operator sits on top of a low priority operator, pop the top 2 operands from the oprands stack and pop one item from the operators stack, evaluate them and push the result into the operands stack. Do this till you are at the end of the input string.
    When the string finishes, keep on pop 2 items from the operands stack and 1 from the operators stack, evaluate it and push the result into operands stack until both the stacks become empty. This will solve your problem if you code correctly. This is the basic operation for evaluating postfix expressions I have ever seen.
postfix assumes no order of operations other than the order they are entered.  grg99  is right.
>> Can be done in under a page of code, no sweat.

Quite true.

>> When you are evaluating a postfix expression we assume that the operands are of sigle digit.

Maybe you do, I don't. :-)

>> postfix assumes no order of operations other than the order they are entered.

Correct.

Exceter
A request for deletion has been made.  If no response or you feel this is in error, comment.  If no objection, I will delete in three days.

Computer101
E-E Admin
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial