-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsystem.py
More file actions
executable file
·41 lines (37 loc) · 1.46 KB
/
system.py
File metadata and controls
executable file
·41 lines (37 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class CalculatorSystem():
def __init__(self, display):
self.operation = ''
self.display = display
def set_operation(self, operation):
if self.display.get_text():
self.operation = operation
self.first = self.parse_display()
self.display.clear()
def reset(self):
self.operation = ''
self.display.clear()
def operate(self):
first = list(map(int, self.first))
second = list(map(int, self.parse_display()))
if self.operation:
try:
if self.operation == '+':
total = list(map(sum, zip(first, second)))
elif self.operation == '-':
total = first[0] - second[0], first[1] + second[1]
elif self.operation == '*':
total = first[0] * second[0], second[0]*first[1]+first[0]*second[1]
elif self.operation == '/':
total = first[0]/second[0], (second[0]**-1)*first[1]+(first[0]/(second[0])**2)*second[1]
except ZeroDivisionError:
total_str = 'err'
else:
total_str = '{}±{}'.format(total[0], total[1])
self.display.set_text(total_str)
self.operation = ''
def parse_display(self):
txt_display = self.display.get_text()
parsed = txt_display.split('±')
if len(parsed) < 2:
return parsed[0], '0'
return parsed