Advertisement
DMG

Infix To Postfix

DMG
Apr 6th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. class Stack:
  2.     def __init__(self):
  3.         self._stek = []
  4.  
  5.     def isEmpty(self):
  6.         if len(self._stek) != 0:
  7.             return False
  8.         return True
  9.            
  10.     def push(self, element):
  11.         self._stek.append(element)
  12.  
  13.     def pop(self):
  14.         return self._stek.pop()
  15.    
  16.     def top(self):
  17.         return self._stek[-1]
  18.        
  19.     def prikazi(self):
  20.         for i in self._stek:
  21.             print str(i) + " ",
  22.         print
  23.        
  24.     def __len__(self):
  25.         return len(self._stek)
  26.  
  27. niz = Stack()
  28. infix = raw_input()
  29.  
  30. postfix = ""
  31.  
  32. for i in infix:
  33.     if "0" <= i <= "9":
  34.         postfix += i
  35.     else:
  36.         if ((len(niz)>0) and (i=="+" or i=="-") and (niz.top()=="*" or niz.top()=="/")):
  37.             while len(niz) > 0:
  38.                 postfix += str(niz.pop())
  39.         elif (len(niz)>0 and ((i=="-" and (niz.top()=="+" or niz.top()=="-")) or (i=="+" and niz.top()=="-"))):
  40.                 postfix += str(niz.pop())
  41.         elif (len(niz)>0 and ((i=="/" and (niz.top()=="*" or niz.top()=="/")) or (i=="/" and niz.top()=="/") or (i=="*" and niz.top()=="/"))):
  42.                 postfix += str(niz.pop())
  43.         niz.push(i)
  44.        
  45. while len(niz) > 0:
  46.     postfix += str(niz.pop())
  47.    
  48. print postfix
  49.  
  50. input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement