-
Notifications
You must be signed in to change notification settings - Fork 1
/
old.py
301 lines (229 loc) · 7.79 KB
/
old.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
current_input = "0"
root = tk.Tk()
root.title("Calculator")
label = ttk.Label(root, text=current_input, font=("Arial", 25))
label.grid(row=1, column=0, columnspan=100)
# parse_errors = {
# "1": "First value was an operator.",
# "2": "Double operators.",
# "3": "Was expecting an operator.",
# }
operators = ["+", "-", "/", "x"]
def parse_input(input) -> list[str]:
list = [""]
current_index = 0
for char in input:
if char in operators:
current_index = current_index + 1
if len(list) - 1 < current_index:
list.extend([""])
list[current_index] = list[current_index] + char
if char in operators:
current_index = current_index + 1
return list
def display_error(message: str):
messagebox.showerror("Calculator: ERROR!", message)
def better_float(var: str) -> float | None:
value = None
try:
value = float(var)
except:
value = None
return value
'''
def correct_order(list: list[str]) -> list[str]:
priority = {"x": 3, "/": 2, "-": 1, "+": 1}
new_list = []
new_list.extend(list)
list_of_operators = []
for index in range(0, len(list)):
is_operator = not (index % 2) == 0
value = list[index]
if is_operator:
list_of_operators.append(value)
def sort_function(operator):
return priority[operator]
list_of_operators.sort(key=sort_function, reverse=True)
operator_index = -1
for index in range(0, len(list)):
is_operator = not (index % 2) == 0
value = list[index]
if is_operator:
operator_index = operator_index + 1
new_list[index] = list_of_operators[operator_index]
right = new_list[index + 1]
left = new_list[-(index + 1)]
new_list[-(index + 1)] = right
new_list[index + 1] = left
return new_list
'''
'''
def parse():
global current_input
operations_priory = {"x": 2, "/": 2, "-": 1, "+": 1}
input = parse_input(current_input)
if not len(input) > 1:
return display_error("No operations provided!")
total = better_float(input[0])
if total == None:
return display_error(f"{input[0]} is not a valid number.")
if input[len(input) - 1] in operators:
return display_error(f"Last value cannot be an operation other then equal.")
last_operator = input[1]
for index in range(2, len(input)):
is_operator = not (index % 2) == 0
value = input[index]
if is_operator:
last_operator = value
else:
number = better_float(value)
if number == None:
return display_error(f"{value} is not a valid number.")
if last_operator == "+":
total = total + number
elif last_operator == "-":
total = total - number
elif last_operator == "/":
total = total / number
elif last_operator == "x":
total = total * number
if str(total).endswith(".0"):
total = str(total).replace(".0", "")
current_input = str(total)
label.configure(text=current_input)
return
'''
def parse():
global current_input
operations_priority = {"x": 2, "/": 2, "-": 1, "+": 1}
input = parse_input(current_input)
if not len(input) > 1:
return display_error("No operations provided!")
if input[len(input) - 1] in operators:
return display_error("Last value cannot be an operation other then equal.")
def do_math(list: list[str]) -> float:
solver_index = 0
last_priority = 0
for index in range(0, len(list)):
if not (index % 2) == 0:
priority = operations_priority[list[index]]
if priority > last_priority:
last_priority = priority
solver_index = index
if solver_index == 0: return list[0] # type: ignore
operator = list[solver_index]
result = 0
first_number = better_float(list[solver_index - 1])
second_number = better_float(list[solver_index + 1])
if first_number == None:
return display_error(f"{first_number} is not a valid number.") # type: ignore
if second_number == None:
return display_error(f"{second_number} is not a valid number.") # type: ignore
if operator == "+":
result = first_number + second_number
elif operator == "-":
result = first_number - second_number
elif operator == "/":
result = first_number / second_number
elif operator == "x":
result = first_number * second_number
list[solver_index - 1] = result # type: ignore
list.pop(solver_index)
list.pop(solver_index)
return do_math(list)
total = do_math(input)
if str(total).endswith(".0"):
total = str(total).replace(".0", "")
current_input = str(total)
label.configure(text=current_input)
return
def input_char(char: str):
if char == "=":
return parse()
global current_input
if char in operators:
last_char = current_input[-1]
if last_char in operators:
return
if current_input == "0":
current_input = ""
current_input = current_input + char
label.configure(text=current_input)
if current_input[0] in operators:
current_input = "0"
label.configure(text=current_input)
def _backspace():
global current_input
if current_input == "0":
return
current_input = current_input[:-1]
if current_input == "":
current_input = "0"
label.configure(text=current_input)
def _7():
input_char("7")
def _8():
input_char("8")
def _9():
input_char("9")
def _4():
input_char("4")
def _5():
input_char("5")
def _6():
input_char("6")
def _1():
input_char("1")
def _2():
input_char("2")
def _3():
input_char("3")
def _0():
input_char("0")
def _dot():
input_char(".")
def _equal():
input_char("=")
def _add():
input_char("+")
def _subtract():
input_char("-")
def _divide():
input_char("/")
def _multiplication():
input_char("x")
ttk.Button(root, text="7", command=_7).grid(row=2, column=0)
ttk.Button(root, text="8", command=_8).grid(row=2, column=1)
ttk.Button(root, text="9", command=_9).grid(row=2, column=2)
ttk.Button(root, text="4", command=_4).grid(row=3, column=0)
ttk.Button(root, text="5", command=_5).grid(row=3, column=1)
ttk.Button(root, text="6", command=_6).grid(row=3, column=2)
ttk.Button(root, text="1", command=_1).grid(row=4, column=0)
ttk.Button(root, text="2", command=_2).grid(row=4, column=1)
ttk.Button(root, text="3", command=_3).grid(row=4, column=2)
ttk.Button(root, text="0", command=_0).grid(row=5, column=1)
ttk.Button(root, text="/", command=_divide).grid(row=2, column=3)
ttk.Button(root, text="x", command=_multiplication).grid(row=3, column=3)
ttk.Button(root, text="-", command=_subtract).grid(row=4, column=3)
ttk.Button(root, text="+", command=_add).grid(row=5, column=3)
ttk.Button(root, text="=", command=_equal).grid(row=5, column=2)
ttk.Button(root, text=".", command=_dot).grid(row=5, column=0)
ttk.Button(root, text="Backspace", command=_backspace).grid(
row=6, column=0, columnspan=4
)
"""
ttk.Label(root, text="----------------------------------------").grid(
row=7, column=0, columnspan=4
)
ttk.Label(root, text="Errors", font=("Arial", 25)).grid(row=8, column=0, columnspan=4)
last_error_row = 8
for key in parse_errors:
last_error_row = last_error_row + 1
ttk.Label(root, text=f"{key} - {parse_errors[key]}", font=("Arial", 12)).grid(
row=last_error_row, column=0, columnspan=4
)
"""
root.mainloop()