I recently appeared for the entrance of cdac.
There I found a lot of questions about postfix.(I know the postfix operator commonly used in c.But it wasn't that way.)
Sample question:- Wat will the expression ABC AB AC will become after postfix operation??
Please tell me how to deal with such questions???
And since I am a new user,if I have made any mistake in method of creating a new thread,pls forgive me for that.

Recommended Answers

All 9 Replies

> Sample question:- Wat will the expression ABC AB AC will become after postfix operation??
You need to post some actual C code.

are you talking about postfix operator as ++a or --a or you referring to postfix expression as in +ab will become a+b??

it is not postfix / prefix in 'c' language i.e. it is not ++a (pre increment 'a') or b-- (post decrement 'b')

it a postfix/prefix which referes to conversion of human readable mathematical expression into computer readable mathematical expression.
when we see "solve a+b" then we just do a plus b
but computer dont understand what 'a+b' means
to let computer understand what 'a+b' means we convert it to postfix expresstion
prefix meas we arrange our expression in such a way that all operators(+,/,-,*) will come first and then oprands (i.e. a,b, etc.)

in same way postfix means operatos will be at end and operands will come first


example : solve a+b , then
conversion of above example in prefix: +ab
conversion of above example in postfix: ab+

there are some algo to do this conversion. also there are many application of postfix/prefix string expressiotn like stack, string reverse etc.

Postfix expression is used to aid the order of evaluation of a mathematical expression.
When higher level programming languages came into existence one of the major hurdles faced by computer scientists was to generate machine language instructions that would properly evaluate any arithmetic expression.
for eg. A*B/C is AB*C/ in postfix notation.
Notice three features of postfix expression:

-The operands maintain the same order as in the equivalent infix expression.
-Parantheses are notneeded to designate the expression unambiguously
-While evaluating the postfix expression the priority of the operators is no longer relevant.

Sample question:- Wat will the expression ABC AB AC will become after postfix operation??

The expression contains only operands. Where are the operators?

If you have a string in infix, ie. in form A+B, and you wish to have it in the form AB+, you can use the Shunting Yard Algorithm

I am talking about postfix expression as in +ab will become a+b.
Can u pls explain it properly ???
And pls also tell me in which book i will get it so that I can have some more reference..

That is just the reverse of the aforementioned algorithm.

Explanation- Scan the input string from left to right. Whenever you see an operand, just push it onto a stack. When you encounter an operator, pop two elements and concatenate the operator between the two operands and push it back to the stack. Perform this till the input string is fully parsed. The only element left in the stack (if the postfix expression is valid) will be the required infix expression.

eg- AB+

1. push A to stack
2. push B to stack
3. Next element is '+'. pop two elements, B and A apply the operator (A+B) and push it back to stack
4. Since you have fully examined the input string, the element left in the stack is the required expression.

This is just a very basic explanation, google for a more precise technique

ps- the thread name says 'postfix' but in your post, you have given a prefix string (+AB). In such a case just parse the input dtring from R->L in the algorithm.

Hope this helps

commented: good answer: accurate, but without solving it for them. +12

Ok friends....thanks a lot......I got it......

postfix evaluation:
===================

question: AB+C-DE+*
steps:
1)group like this

(AB+) C-DE+*
-----
lets say AB+ (means A+B) is x then

2)(xC-)DE+*
-----
(k)

3)then

k(DE+)*
----
(z)

4)then
kz* means k*z is the answer.

postfix to infix convertion:
===========================
1) AB+C-DE+*

2)((AB+)C-)(DE+)*

put bracket like this where two operand and one operand should be there in this manner.

now change the operand in the last to middle.like

3)((A+B)C-)(DE+)*

4)((A+B)-C)(D+E)*

5)((A+B)-C)*(D+E) it will your final infix expression.

postfix to prefix convertion:
=============================
1) AB+C-DE+*

2)((AB+)C-)(DE+)*

put bracket like this where two operand and one operand should be there in this manner.
now bring the operation from the beginning to the end.

3)((+AB)C-)(DE+)*

4)*(-(+AB)C)(+DE)

5)now remove all the brackets.

6)*-+ABC+DE ====its the answer.

I have given your the fastest tips to solve these kind of problems.

In case of any problem you face let me know.

Thanks,
DP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.