Convert the expression into postfix notation and then verify if it is correct or not,direct answers for both 5conversion are not allowed for verification take dummy values for eachvariable.
a+b*(c/d-e)%(f+g*h)-i
Note:
1)No code is required.
To compile postfix notation
Let n is the output string, st is the stack of operations
We read the expression a+b*(c/d-e)%(f+g*h)-i
read a add a to n= a
read + add + to st st= +
read b add b to n= ab
read * add * to st st= *+
read ( add ( to st st= (*+
read с add с to n= abс
read / add / to st st= /(*+
read d add d to n n= abсd
read - execution priority - less / top stack item
add top stack item to n n= abсd/
add - to st st= -(*+
read e add e to n n= abсd/e
read )
We attach the stack to the output line until we get an open parenthesis.
add top stack item to n n= abсd/e- st= (*+
top stack item ( delete ( st= *+
read % execution priority % less * top stack item
add top stack item to n n= abсd/e-*
add % to st st= %+
read ( add ( to st st= ( %+
read f add f to n= abсd/e-*f
read + add + to st st= +( %+
read g add g to n= abсd/e-*fg
read * add * to st st= *+( %+
read h addg to n= abсd/e-*fgh
read )
We attach the stack to the output line until we get an open parenthesis.
n = abсd/e-*fgh*+
st= %+
read - execution priority - less % top stack item
add top stack item to n n = abсd/e-*fgh*+%
st = +
top stack item + same priority with -
add top stack item to n n = abсd/e-*fgh*+%+
st empty
add - to st st = -
read i addg to n= abсd/e-*fgh*+%+i
All expression read
add top stack item to n n = abсd/e-*fgh*+%+i-
st empty
Answer:abсd/e-*fgh*+%+i-
For example a = 1 b =2 c= 64 d= 4 e = 5 f = 6 g= 7 h = 8 i = 9
a+b*(c/d-e)%(f+g*h)-i= 1+2*(64/4-5)%(6+7*8)-9= 14
abсd/e-*fgh*+%+i-
1 2 64 4/5 -*6 7 8 *+%+ 9-
stack
4 64 2 1
/
5 16 2 1
-
11 2 1
*
22 1
8 7 6 22 1
*
56 6 22 1
+
62 22 1
%
22 1
+
23
9 23
-
14
Comments
Leave a comment