0% found this document useful (0 votes)
16 views5 pages

Conversion of Infix To Post Fix

The document outlines the algorithm for converting infix expressions to postfix notation and evaluating postfix expressions. It provides step-by-step examples for two infix expressions, demonstrating the conversion process and the evaluation of the resulting postfix expressions. Additionally, it includes a verification process for the evaluated results against the original infix expressions.

Uploaded by

Ramesh Tharu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Conversion of Infix To Post Fix

The document outlines the algorithm for converting infix expressions to postfix notation and evaluating postfix expressions. It provides step-by-step examples for two infix expressions, demonstrating the conversion process and the evaluation of the resulting postfix expressions. Additionally, it includes a verification process for the evaluated results against the original infix expressions.

Uploaded by

Ramesh Tharu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Conversion of Infix to post fix:-

Algorithm:-
1. Add a unique symbol # in to stack and at the end of array infix.
2. Scan symbol of array infix fro left to right.
3. If the symbol is left parenthesis ‘(‘then add in to the stack.
4. If symbol is operand then add it to array post fix.
5. (i) If symbol is operator then pop the operator which have same precedence
or higher precedence then the operator which occurred.
(ii) add the popped operator to array post fix.
(iii) Add the scanned symbol operator in to stack.
6. (i) If symbol is right parenthesis ‘)’ then pop all the operators from stack
until left parenthesis ‘(‘ in stack.
(ii) Remove left parenthesis ‘(‘ from stack.
7. If symbol is # then pop all the symbol form stack and add them to array
post fix except #.
8. Do the same process until # comes in scanning array infix.

Change Infix to post fix:-


Q. 1. A + B – C + D
1 A+ B – C + D #
Step Symbol Operator in stack post fix expression
1 A # A
2 + #- A
3 B #+ AB
4 - #- AB+
5 C #- AB+ C
6 + #+ AB +C -
7 D #+ AB + C -D
8 # # AB+C-D+
Q.2.A* (B+C $D) – E $F * (G/H)
A*(B+C$D) – E$ F*(G/H)
Step Symbol Operator in stack post fix expression
1 A
#
A
2 *+ #* A
3 ( #+ AB
4 B #- AB+
5 + #- AB+ C
6 C #+ AB +C -
7 $ #+ AB + C -D
8 D # AB+C-D+
9 ) #* ABCD $+
10 - #- ABCD$+*
11 E #- ABCD$+*E
12 $ #-$ ABCD $ +
*E
13 F #-$ ABCD$
+*EF
14 * #-* ABCD$ +*EF$
15 ( #-*( ABCD$+*EF$
16 G #-*( ABCD$+*EF$G
17 / #-*(/ ABCD$+*EF$G
18 # #-*(/ ABCD$+ *EF$GH/
19 ) #-* ABCD$ +*EF$GH/*-
20 # # ABCD$+*EF$GH/*-
Hence, the required post fix expression is ABCD$ +* EF $ GH/*-

Evaluation of post fix expression:-


In this case stack contents the operands. In stead of operators. Whenever
any operator occurs in scanning we evaluate with last two elements of stack.
Algorithm:-
(i) Add the unique symbol # at the end of array post fix.
(ii) Scan the symbol of array post fix one by one from left to right.
(iii) If symbol is operand, two push in to stack.
(iv) If symbol is operator, then pop last two element of stack and evaluate it as
[top- 1] operator [top] & push it to stack.
(v) Do the same process until ‘#’ comes in scanning.
(vi) Pop the element of stack which will be value of evaluation of post fix
arithmetic expression.

Postfix expression ABCD $+* EF$GHI * -


Evaluate postfix expression where A = 5, B = 5, C = 4, D = 2 E = 2, F= 2 ,
G = 9, H= 3 now, 4, 5, 2, $ , +, *, 2, 2, $, 9, 3, /, *, - , #
Step Symbol Operand in stack
1 4 4
2 5 4, 5
3 4 4,5,4
4 2 4,5,4,2
5 $ 4,5,16
6 + 4,21
7 * 84
8 2 84,2
9 2 84, 2,2
10 $ 84,4
11 9 84,4,9
12 3 84,4,9,3
13 / 84, 4, 3
14 * 84,12
15 - 72

The required value of postfix expression is 72


For checking:-
A*(B+C$D) – E$F*(G/H)
4*(5+42)-22*(9/3 )
4* (5+16)-4*3
84-12
72
Q. Convert the post fix:-
((A+B)*C – (D-E) 4F+G
Evaluate post fix where A=1, B=2, C= 3, D= 4, E= 3, F=2, G=1.
Verify it by evaluating it’s infix expression.
Step Symbol Operand in stack Post fix operation
1 ( #(
2 ( #(( A
3 + #((+ A
4 B #((+ AB
5 ) #() AB+
6 * #(* AB+
7 C #(* AB+C
8 - #(- AB+C*
9 ( #(-( AB+C*
10 D #(-(- AB+C*D
11 - #(-(- AB+C*D
12 E #(-(- AB+C*DE
13 ) #(- AB+C*DE-
14 ) # AB+C*DE--
15 $ #$ AB+C*DE--
16 ( #$( AB+C*DE--
17 F #$( AB+C*DE - -f
18 + #$(+ AB+C*DE--f
19 G #$(+ AB+C*DE - - FG
20 ) # AB+C*DE- - FG+ $
The required postfix expression is AB+C*DE - - FG +$
Now 1,2, +3, *4, 3, -, -, 2, 1, +, $, #
Step Symbol Operand in stack
1 1 1
2 2 1,2
3 + 3
4 3 3,3
5 * 9
6 4 9,4
7 3 9,4,3,1
8 - 3,1
9 - 8
10 1 8,2
11 1 8,2,1
12 $ 8,3
13 $ 24
For checking
The given infix expression is
((A+B)*C-(D-e))$(F+G)
= ((1+2)*3-(4-3))4(2+1)
= (3*3 -1) $3
= (9-1)$3
=8$9
= 24

You might also like