| | |
Postfix operation
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
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.
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.
Last edited by abhi.navale; Jul 12th, 2009 at 6:18 am.
Abhi
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.
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.
Intel inside, mental outside
•
•
•
•
Sample question:- Wat will the expression ABC AB AC will become after postfix operation??
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
My blog on .NET- http://dotnet.tekyt.info
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
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
My blog on .NET- http://dotnet.tekyt.info
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
===================
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
Last edited by Dream2code; Jul 14th, 2009 at 7:51 am.
![]() |
Similar Threads
- Need help with a recursive postfix expression evaluator (C++)
- custom made queue/stack - having problems (C++)
- stack/queue program for long math operation - need help (C++)
- infix to postfix (Java)
- Controlling wheter postfix expression is valid (C)
- Illegal operation @ startup (Windows NT / 2000 / XP)
- Postfix configuration (*nix Software)
Other Threads in the C Forum
- Previous Thread: String Swap Not Working
- Next Thread: How cn i change a windows folder
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






