Question

By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert...

By using PYTHON language

Postfix to Infix using Stack Develop a stack application that can convert Postfix notation to Infix notation using the following algorithm. In your stack application, you can use only two stacks, one for a stack that can store Postfix notation, and the other is a stack to store infix notation. Also, it would help if you had a function to distinguish between an operation or an operand.

Input

A B C * + D E / F * -

Output

(A+(B*C))+((D/E)*F))

Algorithm of Postfix to infix

1. Push postfix notation to the PostfixStack reverse order

( eg. push - * F / E D + * C B A)

2. while there are no data at PostfixStack

3. read one symbol from the PostfixStack

4. if the symbol is an operand

5. push it into the InfixStack

6. else

7. pop the top 2 values from the InfixStack

8. put the operator between two operand

9. Encapsulate the resulted string with parenthesis

10. if there is only one value in the stack That value in the stack is the desired Infix notation.

NOTE : By using Python language

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def isOperand(x):
return ((x >= 'a' and x <= 'z') or
(x >= 'A' and x <= 'Z'))
  
# Get Infix for a given postfix
# expression
def getInfix(exp) :
  
s = []
  
for i in exp:
  
# Push operands
if (isOperand(i)) :
s.insert(0, i)
  
# We assume that input is a
# valid postfix and expect
# an operator.
else:
  
op1 = s[0]
s.pop(0)
op2 = s[0]
s.pop(0)
s.insert(0, "(" + op2 + i +
op1 + ")")
  
# There must be a single element in
# stack now which is the required
# infix.
return s[0]
  
# Driver Code
if __name__ == '__main__':
  
exp = input("Enter the Prefix expression : ")
print(getInfix(exp.strip()))

Add a comment
Know the answer?
Add Answer to:
By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT