Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. #####################################
  2. #   COMPSCI 105 S2 C, 2015          #
  3. #   Assignment 1 Question 2         #
  4. #                                   #
  5. #   @author  Gerald Lee, glee598   #
  6. #   @version    THE DATE            #
  7. #####################################
  8.  
  9. class Stack:
  10.     def __init__(self):
  11.         self.__stack = []
  12.        
  13.     def push(self, item):
  14.         self.__stack.append(item)
  15.        
  16.     def pop(self):
  17.         return self.__stack.pop()
  18.        
  19.     def peek(self):
  20.         return self.__stack[-1]
  21.        
  22.     def is_empty(self):
  23.         return self.__stack == []
  24.        
  25.     def checkstack(self):
  26.         return self.__stack
  27.        
  28. def q2(string):
  29.         s = Stack()
  30.         prec_dictionary = {"^":5, "*":4, "/":4, "+":3, "-":3, "<":2, ">":2, "(":1 }
  31.         allowed_operators = "+-/*^<>"
  32.         postfix_list = []
  33.         newlist = []
  34.  
  35.         for i in string:
  36.             if i in allowed_operators:
  37.                 while (not s.is_empty()) and (prec_dictionary[s.peek()] >= prec_dictionary[i]):
  38.                     postfix_list.append(s.pop())
  39.                 s.push(i)
  40.             elif i == "(":
  41.                 s.push(i)
  42.             elif i == ")":
  43.                 i = s.pop()
  44.                 while not i == "(":
  45.                     postfix_list.append(i)
  46.                     i = s.pop()
  47.             else:
  48.                 postfix_list.append(i)
  49.  
  50.         while not s.is_empty():
  51.             postfix_list.append(s.pop())
  52.            
  53.         for i in postfix_list:
  54.             if not i == ' ':
  55.                 newlist.append(i)
  56.         return calculate(newlist)
  57.        
  58. def calculate(postfix):
  59.     s = Stack()
  60.     postfix_newlist = postfix
  61.     allowed_operators = "+-/*<>**"
  62.     logicals = "<>"
  63.     for i in range(len(postfix)):
  64.         if postfix[i] == '^':
  65.             postfix_newlist[i] = '**'      
  66.  
  67.     for i in postfix_newlist:
  68.         s.push(i)
  69.         if i == '+':
  70.             s.pop()
  71.             num2 = s.pop()
  72.             num1 = s.pop()
  73.             s.push(str(float(num1) + float(num2)))
  74.         elif i == '-':
  75.             s.pop()
  76.             num2 = s.pop()
  77.             num1 = s.pop()
  78.             s.push(str(float(num1) - float(num2)))
  79.         elif i == '*':
  80.             s.pop()
  81.             num2 = s.pop()
  82.             num1 = s.pop()
  83.             s.push(str(float(num1) * float(num2)))
  84.         elif i == '/':
  85.             s.pop()
  86.             num2 = s.pop()
  87.             num1 = s.pop()
  88.             s.push(str(float(num1) / float(num2)))
  89.         elif i == '**':
  90.             s.pop()
  91.             num2 = s.pop()
  92.             num1 = s.pop()
  93.             s.push(str(float(num1) ** float(num2)))    
  94.         elif i == '>':
  95.             s.pop()
  96.             num2 = s.pop()
  97.             num1 = s.pop()
  98.             if num1 > num2:
  99.                 s.push(num1)
  100.             else:
  101.                 s.push(num2)
  102.         elif i == '<':
  103.             s.pop()
  104.             num2 = s.pop()
  105.             num1 = s.pop()
  106.             if num1 < num2:
  107.                 s.push(num1)
  108.             else:
  109.                 s.push(num2)
  110.    
  111.     result = s.peek()
  112.     print(type(result), result)
  113.    
  114.  
  115. print(q2('2 ^ ( 1 + 3 ^ 2 )'))  #1024
  116. print(q2('( 3 * 5 ) - ( 1 > 2 > 3 < 4 )'))  #12
  117. print(q2('2 * ( ( 4 < 2 + 3 ) + 3 * 4 )')) #32
  118. print(q2('( 40 > 15 < 35 ) + 10')) #45 #got 1.0 as output
  119. print(q2('40 > 15 < 35 + 10')) #40 #got 1.0 as output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement