m2skills

in2pre python

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