m2skills

in2post python

Mar 29th, 2017
3,588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | None | 0 0
  1. # Program to convert infix expression to postfix expression
  2.  
  3. # method that returns the priority of the operator
  4. def priority(operator):
  5.     if operator == '+' or operator == '-':
  6.         return 1
  7.  
  8.     elif operator == '*' or operator == '/' or operator == '%':
  9.         return 2
  10.  
  11.     elif operator == '^':
  12.         return 3
  13.  
  14.     else:
  15.         return 0
  16.  
  17. # method that converts string in infix to postfix
  18. # all the strings are assumed to be valid expressions
  19. def in2postfix(infixString):
  20.     # taking a list variable to store operators
  21.     stack = []
  22.  
  23.     # result string variable
  24.     postfixString = ""
  25.     i = 0
  26.  
  27.     # loop till i is in the range of the length of string
  28.     while i in range(0, len(infixString)):
  29.  
  30.         # if an alphabet is found then copy it to the output string
  31.         if infixString[i].isalpha():
  32.             postfixString += infixString[i]
  33.  
  34.  
  35.         # if an opening bracket is found then put it in stack
  36.         elif infixString[i] == '(' or infixString[i] == '[' or infixString[i] == '{':
  37.             stack.append(infixString[i])
  38.  
  39.  
  40.         # if an closing bracket is found then
  41.         # keep removing the operators from the stack and add them to postfix string until you find the corresponding opening bracket
  42.         elif infixString[i] == ')' or infixString[i] == ']' or infixString[i] == '}':
  43.  
  44.             if infixString[i] == ')':
  45.                 while stack[-1] != '(':
  46.                     postfixString += stack.pop()
  47.                 stack.pop()
  48.  
  49.             if infixString[i] == ']':
  50.                 while stack[-1] != '[':
  51.                     postfixString += stack.pop()
  52.                 stack.pop()
  53.  
  54.             if infixString[i] == '}':
  55.                 while stack[-1] != '{':
  56.                     postfixString += stack.pop()
  57.                 stack.pop()
  58.  
  59.         # if none of the above cases are satisfied then we surely have an operator
  60.         else:
  61.  
  62.             # if the stack if empty then we simply put the operator in stack
  63.             if len(stack) == 0:
  64.                 stack.append(infixString[i])
  65.  
  66.             # if not then we compare the priority of the stack top and current operator
  67.             else:
  68.  
  69.                 # if the priority of current operator if high then push it onto the stack
  70.                 if priority(infixString[i]) > priority(stack[-1]):
  71.                     stack.extend(infixString[i])
  72.  
  73.  
  74.                 # if the priority of current operator is less than or equal to the stack top then
  75.                 # pop the stack top and add it to the postfix string
  76.                 elif priority(infixString[i]) <= priority(stack[-1]):
  77.                     postfixString += stack.pop()
  78.                     position = len(stack) - 1
  79.  
  80.                     # now if you have an operator that has equal priority as of current operator then pop
  81.                     while position >= 0 and priority(stack[position]) == priority(infixString[i]):
  82.                         postfixString += stack.pop()
  83.                         position -= 1
  84.                         if position < 0:
  85.                             break
  86.  
  87.                     stack.extend(infixString[i])
  88.  
  89.         # increment the value of i
  90.         i += 1
  91.  
  92.     # at the end remove all the operators from the stack
  93.     while len(stack) != 0:
  94.         postfixString += stack.pop()
  95.        
  96.     # return the result
  97.     return postfixString
  98.  
  99. # main function
  100. infix = input("Enter the Infix Expression : ")
  101. postfix = in2postfix(infix)
  102. print("The converted Expression in Postfix is : " + postfix)
Add Comment
Please, Sign In to add comment