0% found this document useful (0 votes)
81 views

Infix To Prefix PDF

The document summarizes the steps to convert an infix expression to prefix notation. It shows working for the expression ((A-B)+C*(D+E))-(F+G). First, it parses the infix expression from right to left, pushing operators onto a stack and variables to an output stack. Then, it reverses the output stack to get the prefix form: -+-AB*C+DE+FG. Finally, it evaluates the prefix expression by assigning values to variables and evaluating from right to left, getting a result of 33.

Uploaded by

Bangla Tweaks
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Infix To Prefix PDF

The document summarizes the steps to convert an infix expression to prefix notation. It shows working for the expression ((A-B)+C*(D+E))-(F+G). First, it parses the infix expression from right to left, pushing operators onto a stack and variables to an output stack. Then, it reverses the output stack to get the prefix form: -+-AB*C+DE+FG. Finally, it evaluates the prefix expression by assigning values to variables and evaluating from right to left, getting a result of 33.

Uploaded by

Bangla Tweaks
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

We have infix expression in the form of:

((A-B)+C*(D+E))-(F+G)
Now reading expression from right to left and pushing operators into stack and variables to
output stack

Input Output_stack Stack


) EMPTY )
G G )
+ G )+
F GF )+
( GF+ EMPTY
- GF+ -
) GF+ -)
) GF+ -))
E GF+E -))
+ GF+E -))+
D GF+ED -))+
( GF+ED+ -)
* GF+ED+ -)*
C GF+ED+C -)*
+ GF+ED+C* -)+
) GF+ED+C* -)+)
B GF+ED+C*B -)+)
- GF+ED+C*B -)+)-
A GF+ED+C*BA -)+)-
( GF+ED+C*BA- -)+
( GF+ED+C*BA-+ -
EMPTY GF+ED+C*BA-+- EMPTY
Out put_stack = GF+ED+C*BA-+-

Reversing the output_stack we get prefix expression: -+-AB*C+DE+FG


Task b:
Evaluating the prefix expression:
Assigning the values to variables

A=7 B=6 C=5 D=4 E=3 F=2 G=1


Reading expression from right to left:

reading operation Stack_output


1 1 Empty
2 12
+ 12+ 3
3 33 3
4 334 3
+ 3 3 4+ 37
5 375 37
* 3 7 5* 3 7 35
6 3 35 6 3 35
7 3 35 6 7 3 35
- 3 35 6 7- 3 35 1
+ 3 35 1+ 3 36
- 3 36 - 33
Pop stack_output : 33

Result=33
Comparing both expressions evaluations:

((A-B)+C*(D+E))-(F+G)

((7-6)+5(4+3))-(2+1)

(1+5*7)-3

1+35-3

36-3

33

You might also like