Lecture 8: Simple Calculator Application Postfix Calculator Dr Kieran T. Herley Department of Computer Science University College Cork 2016/17 KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 1 / 15
Summary Postfix Calculator KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 2 / 15
Postfix notation Postfix is a notation of writing arithmetic expressions in which operands appear after the operands to which they apply. Examples: 1 2 + 3 3 4 12 1 2 + 3 + 6 1 2 3 + + 6 1 2 3 + 7 Advantages: As expressive as traditional infix notation Simple to evaluate using simple stack-based algorithm KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 3 / 15
Stack Behaviour A stack is a container abstraction that holds a collection of items in a linear bottom to top order. Operations push(x) Add a new item x to the top of the stack pop() Remove and return the item at the top of the stack. Illegal if stack is empty. is empty() Return True if stack is empty, False otherwise KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 4 / 15
Stack Behaviour A stack is a container abstraction that holds a collection of items in a linear bottom to top order. Operations push(x) Add a new item x to the top of the stack pop() Remove and return the item at the top of the stack. Illegal if stack is empty. is empty() Return True if stack is empty, False otherwise Illustration op. push(17) 17 stack (top to right) empty KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 4 / 15
Stack Behaviour A stack is a container abstraction that holds a collection of items in a linear bottom to top order. Operations push(x) Add a new item x to the top of the stack pop() Remove and return the item at the top of the stack. Illegal if stack is empty. is empty() Return True if stack is empty, False otherwise Illustration op. push(17) 17 push(23) 17 23 stack (top to right) empty KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 4 / 15
Stack Behaviour A stack is a container abstraction that holds a collection of items in a linear bottom to top order. Operations push(x) Add a new item x to the top of the stack pop() Remove and return the item at the top of the stack. Illegal if stack is empty. is empty() Return True if stack is empty, False otherwise Illustration op. push(17) 17 push(23) 17 23 stack (top to right) empty pop() 17 returns 23 KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 4 / 15
Stack Behaviour A stack is a container abstraction that holds a collection of items in a linear bottom to top order. Operations push(x) Add a new item x to the top of the stack pop() Remove and return the item at the top of the stack. Illegal if stack is empty. is empty() Return True if stack is empty, False otherwise Illustration op. push(17) 17 push(23) 17 23 stack (top to right) empty pop() 17 returns 23 push(29) 17 29 KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 4 / 15
Stack Behaviour A stack is a container abstraction that holds a collection of items in a linear bottom to top order. Operations push(x) Add a new item x to the top of the stack pop() Remove and return the item at the top of the stack. Illegal if stack is empty. is empty() Return True if stack is empty, False otherwise Illustration op. push(17) 17 push(23) 17 23 stack (top to right) empty pop() 17 returns 23 push(29) 17 29 push(31) 17 29 31 KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 4 / 15
Stack Behaviour A stack is a container abstraction that holds a collection of items in a linear bottom to top order. Operations push(x) Add a new item x to the top of the stack pop() Remove and return the item at the top of the stack. Illegal if stack is empty. is empty() Return True if stack is empty, False otherwise Illustration op. push(17) 17 push(23) 17 23 stack (top to right) empty pop() 17 returns 23 push(29) 17 29 push(31) 17 29 31 pop() 17 29 returns 31 KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 4 / 15
Stack Behaviour A stack is a container abstraction that holds a collection of items in a linear bottom to top order. Operations push(x) Add a new item x to the top of the stack pop() Remove and return the item at the top of the stack. Illegal if stack is empty. is empty() Return True if stack is empty, False otherwise Illustration op. push(17) 17 push(23) 17 23 stack (top to right) empty pop() 17 returns 23 push(29) 17 29 push(31) 17 29 31 pop() 17 29 returns 31 pop() 17 returns 29 KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 4 / 15
Stack Behaviour A stack is a container abstraction that holds a collection of items in a linear bottom to top order. Operations push(x) Add a new item x to the top of the stack pop() Remove and return the item at the top of the stack. Illegal if stack is empty. is empty() Return True if stack is empty, False otherwise Illustration op. push(17) 17 push(23) 17 23 stack (top to right) empty pop() 17 returns 23 push(29) 17 29 push(31) 17 29 31 pop() 17 29 returns 31 pop() 17 returns 29 pop() empty returns 17 KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 4 / 15
Stack Behaviour A stack is a container abstraction that holds a collection of items in a linear bottom to top order. Operations push(x) Add a new item x to the top of the stack pop() Remove and return the item at the top of the stack. Illegal if stack is empty. is empty() Return True if stack is empty, False otherwise Illustration op. push(17) 17 push(23) 17 23 stack (top to right) empty pop() 17 returns 23 push(29) 17 29 push(31) 17 29 31 pop() 17 29 returns 31 pop() 17 returns 29 pop() empty returns 17 pop() ERROR KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 4 / 15
Postfix evaluation algorithm Split expression into constituent elements: Operators: +,,, / Operands: unsigned integers (or floats) Use stack to hold intermediate results, initially empty, holds result at alg s conclusion. For each element: if operand: push it onto stack if operator: pop top two items, apply operator to these, push result back onto stack KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 5 / 15
Illustration 1 2 3 + elt. stack Remarks empty 1 1 1 pushed 2 1 2 2 pushed 3 1 2 3 3 pushed 1 6 3, 2 popped, 6 = 2 3 pushed + 7 6, 1 popped, 7 = 1 + 6 pushed Result 7 on top of stack KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 6 / 15
Illustration 3 2 1 elt. stack Remarks empty 3 3 3 pushed 2 3 2 2 pushed 1 3 2 1 1 pushed 3 1 1, 2 popped, 1 = 2 1 pushed 7 1, 3 popped, 2 = 3 1 pushed Result 2 on top of stack KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 7 / 15
Elements of a Python implementation Implement Stack idea using class definition. Parse input expression into constituent elements using regular expressions Evaluation algorithm a doddle KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 8 / 15
Implementing Stack class Stack(): def init ( self ): self. items = [] def push( self, new item): self. items. append(new item) def pop( self ): if self.is empty (): raise Exception( Stack error :.... ) else : return self. items. pop() Function is empty omitted for brevity. KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 9 / 15
Splitting expression into constituent elements tokens = re.compile(r [ +/ ] (\d+(\.\d+)?) ) for t in tokens. finditer ( expression ): # Code to handle element t as per algorithm KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 10 / 15
Splitting expression into constituent elements tokens = re.compile(r [ +/ ] (\d+(\.\d+)?) ) for t in tokens. finditer ( expression ): # Code to handle element t as per algorithm The [-+/*] bit captures the four operators The \d+(\.\d+)? bit captures operands (ints or decimals) KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 10 / 15
Teeing things up values = Stack() operators = { + : lambda x, y: x + y, : lambda x, y: x y, : lambda x, y: x y, / : lambda x, y: x // y if type(x) == type(y) == int else x / y } expression = input( Please enter postfix expression : ) KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 11 / 15
Evaluation algorithm for t in tokens. finditer ( expression ): tkn = t.group(). strip () if tkn in operators : # Handle operator case else : # Handle operand case print( Result is %s. % str(values.pop())) KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 12 / 15
Evaluation algorithm cont d for t in tokens. finditer ( expression ): tkn = t.group(). strip () if tkn in operators : # Handle operator case else : if tkn. isdigit (): values. push(int(tkn)) else : values. push(float(tkn)) KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 13 / 15
Evaluation algorithm cont d for t in tokens. finditer ( expression ): tkn = t.group(). strip () if tkn in operators : val2 = values.pop() val1 = values.pop() action = operators[tkn] new val = action(val1, val2) values. push(new val) else : # Handle operand case KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 14 / 15
Evaluation algorithm cont d for t in tokens. finditer ( expression ): tkn = t.group(). strip () if tkn in operators : val2 = values.pop() val1 = values.pop() action = operators[tkn] new val = action(val1, val2) values. push(new val) else : if tkn. isdigit (): values. push(int(tkn)) else : values. push(float(tkn)) KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 15 / 15
Infix evaluator Can develop alg. based on similar principles for infix notation. Could always use Python s eval function: result = eval(expression) print( result ) Warning: eval and its sister exec can represent security risk if used on unvetted strings. KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17 16 / 15