-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
178 lines (150 loc) · 4.95 KB
/
Copy pathcalculator.py
File metadata and controls
178 lines (150 loc) · 4.95 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
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
def calculate(calc, verbose = False):
if check(calc):
calc = calc_braces(calc, verbose)
calc = calc_mult_div(calc, verbose)
calc = calc_add_sub(calc, verbose)
return calc
else:
print("Invalid input")
return -1
def check(calc):
braces_open = 0
braces_close = 0
allowed_chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '*', '/', '(', ')', '.']
for char in calc:
if char not in allowed_chars:
return False
if char == '(':
braces_open += 1
if char == ')':
braces_close += 1
if braces_close > braces_open:
return False
return True
def calc_braces(calc, verbose):
while '(' in calc:
brace_depth = 0
end_left = 0
start_right = 0
found_left = False
start = True
end = True
for i in range(len(calc)):
if calc[i] == '(':
if i != 0 and brace_depth == 0:
start = False
brace_depth += 1
if not found_left:
found_left = True
end_left = i
if calc[i] == ')':
brace_depth -= 1
if brace_depth == 0:
if i != len(calc) - 1:
end = False
start_right = i + 1
break
if not start:
left = calc[:end_left]
if left[-1] not in ['+', '-', '*', '/', '(']:
left = left + '*'
else:
left = ''
if not end:
right = calc[start_right:]
if right[0] not in ['+', '-', '*', '/', ')']:
right = '*' + right
else:
right = ''
mid = calc[end_left + 1:start_right - 1]
mid = calculate(mid, verbose)
calc = left + mid + right
calc = replace_add_sub(calc)
if verbose:
print(f"Braces: {calc}")
return calc
def calc_mult_div(calc, verbose):
while '*' in calc or '/' in calc:
left_num = calc[0]
right_num = ''
operator = ''
end_left = 0
start_right = 0
found_left = False
start = True
end = True
negative = False
for i in range(1, len(calc)):
if negative:
negative = False
continue
if calc[i] not in ['+', '-', '*', '/']:
if not found_left:
left_num += calc[i]
else:
right_num += calc[i]
else:
if found_left:
start_right = i
end = False
break
elif calc[i] not in ['*', '/']:
left_num = ''
if calc[i + 1] == '-':
left_num += '-'
negative = True
end_left = i + 1
start = False
else:
found_left = True
operator = calc[i]
if calc[i + 1] == '-':
right_num += '-'
negative = True
left = calc[:end_left] if not start else ''
right = calc[start_right:] if not end else ''
mid = '{0:g}'.format(float(left_num) * float(right_num)) if operator == '*' else '{0:g}'.format(float(left_num) / float(right_num))
calc = left + mid + right
calc = replace_add_sub(calc)
if verbose:
print(f"Mult/Div: {calc}")
return calc
def calc_add_sub(calc, verbose):
while '+' in calc[1:] or '-' in calc[1:]:
left_num = calc[0]
right_num = ''
operator = ''
start_right = 0
found_left = False
end = True
for i in range(1, len(calc)):
if calc[i] not in ['+', '-']:
if not found_left:
left_num += calc[i]
else:
right_num += calc[i]
else:
if found_left:
start_right = i
end = False
break
else:
found_left = True
operator = calc[i]
right = calc[start_right:] if not end else ''
left = '{0:g}'.format(float(left_num) + float(right_num)) if operator == '+' else '{0:g}'.format(float(left_num) - float(right_num))
calc = left + right
if verbose:
print(f"Add/Sub: {calc}")
return calc
def replace_add_sub(calc):
while '--' in calc or '+-' in calc or '-+' in calc:
calc = calc.replace('--', '+')
calc = calc.replace('+-', '-')
calc = calc.replace('-+', '-')
if calc[0] == '+':
calc = calc[1:]
return calc
calc = input("> ")
calc = ''.join(calc.split())
calculate(calc, verbose = True)