forked from nawelbay/DevopsSRE_DS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
202 lines (158 loc) · 5.26 KB
/
calculator.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import math
# Initialize variables used with global keyword in functions
display = "0"
operation = ""
prev_button = ""
numberA = float(0)
numberB = float(0)
# Define ERROR display string
ERROR = "ERROR "
# Define digit_set and operation_set
digit_set = set(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"])
operation_set = set(["plus", "minus", "multiply", "divide"])
# Define button_set which includes digit_set and operation_set
button_set = set(["sign", "point", "clear", "backspace", "equals"])
button_set.update(digit_set)
button_set.update(operation_set)
def reset(reset_display: bool):
"""
Function for resetting calculator
reset_display: True - Reset everything also the display
False - Reset everything except the display
"""
global display
global operation
global prev_button
global numberA
global numberB
if reset_display == True:
display = "0"
operation = ""
prev_button = ""
numberA = float(0)
numberB = float(0)
def press_button(button):
"""Function for pressing button on calculator"""
global prev_button
update_display(button)
prev_button = button
def update_display(button):
"""Function for updating calculator display according to button press"""
global display
global operation
global prev_button
global numberA
global numberB
# Do action for clear button
if button == "clear":
reset(True)
# Return immediately if display contains ERROR, because only clear button is allowed in this case
elif display == ERROR:
return
# Do action for digit buttons
elif button in digit_set:
if prev_button == "equals":
reset(True)
if display == "0" or prev_button in operation_set:
display = button
return
if display == "-0":
display = "-" + button
return
# Count number of digits already in display
digit_count = 0
for d in range(0, 10):
digit_count += display.count(str(d))
# If digit count is less than 10, insert new digit
if digit_count < 10:
display += button
# Do action for sign button
elif button == "sign":
if prev_button == "equals":
reset(False)
if prev_button in operation_set:
display = "-0"
return
# If first character is "-" remove it, otherwise add "-" as first character
if display[0] == "-":
display = display[1:]
else:
display = "-" + display
# Do action for point button
elif button == "point":
if prev_button == "equals":
reset(True)
if prev_button in operation_set:
display = "0."
return
if not "." in display:
display += "."
# Do action for operation buttons
elif button in operation_set:
if prev_button != "equals" and prev_button not in operation_set:
calculate()
numberA = float(display)
operation = button
# Do action for backspace button
elif button == "backspace":
if prev_button == "equals":
reset(False)
# Remove last character from display
display = display[:-1]
# Insert "0" if display is empty or only contains "-"
if display == "" or display == "-":
display = "0"
# Do action for equals button
elif button == "equals":
calculate()
def calculate():
"""Function for calculating result of numberA operation numberB"""
global display
global prev_button
global numberA
global numberB
# If previous button is not "equals" get new numberB from display, otherwise keep old numberB
if prev_button != "equals":
numberB = float(display)
# Perform operation to get new numberA
if operation == "":
numberA = float(display)
elif operation == "plus":
numberA = numberA + numberB
elif operation == "minus":
numberA = numberA - numberB
elif operation == "multiply":
numberA = numberA * numberB
elif operation == "divide":
if numberB == 0:
numberA = float("NAN")
else:
numberA = numberA / numberB
# Write new numberA to display
display = display_number(numberA)
def display_number(number: float) -> str:
"""Function for converting number to string to be written to display"""
# Return ERROR in case of NAN or overflow
if math.isnan(number) or number > 9999999999 or number < -9999999999:
return ERROR
# Number of digits before point
if abs(number) >= 1:
digits_before = int(math.log10(abs(number)))+1
else:
digits_before = 1
# Number of digits after point (10 digits allowed in total)
digits_after = 10 - digits_before
# Format number as string with fixed number of digits after point
string = ("{:." + str(digits_after) + "F}").format(number)
# Remove trailing zeros after point
if string.find(".") != -1:
while string[-1] == "0":
string = string[:-1]
# Remove trailing point
if string[-1] == ".":
string = string[:-1]
# Convert "-0" to 0
if string == "-0":
string = "0"
# Return the string
return string