Advertisement
6677

STSPBWBBIWHTD

Apr 11th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. #scan the infix string from left to right
  2. #initialise an empty stack
  3. #if the scanned character is an operand then add it to the postix string
  4. #if the scanned character is an operator and the stack is empty, push the character to the stack
  5. #if the scanned character is operator and stack not empty, compare the precedence of the character with the elemont on top of the stack, if the top of stack has higher precedence over the scanned character then pop the stack else push the scanned character to stock, repeat this step as long as stack is not empty and top stack has precedence over the character.
  6. #repeat until all characters are scanned.
  7. #pop contents of the stack 1 by 1 onto the end of the postfix string until the stack is empty
  8. #return the postfix string
  9. def inToPost(infix):
  10.     #initialise empty stack and output
  11.     postfix = ""
  12.     stack = []
  13.  
  14.     #scan each character left to right
  15.     for count in range(0, len(infix)):
  16.         token = infix[count]
  17.         #print token
  18.         #print postfix
  19.         #print stack
  20.         if isNumber(token):
  21.             #if scanned character is an operand then add to postfix string
  22.             postfix += token
  23.         elif isOperator(token):
  24.             #scanned character is an operator
  25.  
  26.             comparing = True
  27.             while comparing == True:
  28.                 if len(stack) == 0:
  29.                     #if stack is empty, push token onto the stack
  30.                     stack.append(token)
  31.                     comparing = False
  32.                 elif precedence(token) >= precedence(stack[-1]):
  33.                     stack.append(token)
  34.                     comparing = False
  35.                 else:
  36.                     postfix += stack.pop()
  37.                    
  38.            
  39.             '''
  40.            else:
  41.                #compare precedence
  42.                comparing = True
  43.                while comparing == True:
  44.                    if precedence(stack[-1]) <= precedence(token):
  45.                        #top of stack has lower precedence, add token to output
  46.                        comparing = False
  47.                        postfix += token
  48.                    else:
  49.                        #top of stack has higher precedence, add top of stack to output
  50.                        postfix += stack.pop()
  51.                        if len(stack) == 0:
  52.                            stack.append(token)
  53.                            comparing = False
  54.                    print postfix
  55.                #end of while
  56.            '''
  57.             #end of isOperator
  58.         else:
  59.             print "invalid token: " + token + "discarded"
  60.         #print "\n"
  61.         #print postfix
  62.         #print stack
  63.         #print "\n###~###\n"
  64.     #pop contents of stack onto postfix string
  65.     for count in range(0, len(stack)):
  66.         postfix += stack.pop()
  67.     return postfix
  68.  
  69. def isNumber(token):
  70.     try:
  71.         token = int(token)
  72.         return True
  73.     except:
  74.         return False
  75.  
  76. def isOperator(token):
  77.     if token in ["+", "-", "*", "/", "^"]:
  78.         return True
  79.     else:
  80.         return False
  81.  
  82. def precedence(token):
  83.     #bidmas
  84.     if token == "-":
  85.         return 1
  86.     elif token == "+":
  87.         return 1
  88.     elif token == "*":
  89.         return 2
  90.     elif token == "/":
  91.         return 2
  92.     elif token == "^":
  93.         return 3
  94.     return 0
  95.  
  96. expression = "1+2*3-4"
  97. #expression = "3+4*5/6"
  98. print inToPost(expression)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement