-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCalc.py
265 lines (239 loc) · 8.92 KB
/
Calc.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
from decimal import *
import math
import sys
from typing import List
OPERATORS = ('+', '-', '*', '/', '^', '(', ')', '.', 'l', 'o', 'g', 'e', 'x', 'p', 'L', 'O', 'G', 'E', 'X', 'P')
DIGITS = ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0')
WELCOME = "Please enter a sequence that you want to compute: (q to quit)"
INVALID_INPUT = "Invalid input: please type like log3+-5*exp(4.2)/(5+7) or log(3)+(-5)*exp(4.2)/(5+7)"
ERROR_INPUT = "Error: The sequence must only include operators and digits"
ZERO_DIVISION = "Error: division by zero"
MATHEMATICAL_ERROR = "Mathematical Error: the domain of log must be positive"
stack = []
# check if input is valid
def is_valid(lst):
last_item = ''
for i in lst:
if i not in OPERATORS and i not in DIGITS:
return False
if lst.__len__() > lst.index(i) and i in OPERATORS and i != '-' and last_item == i:
return False
last_item = i
return True
# main calc part calls bracket to store and load stack
def calc(lst):
bracket(lst)
while stack.__len__() > 1 or stack.__len__() == 0:
new_list = stack.copy()
stack.clear()
bracket(new_list)
final_result = stack[0]
stack.clear()
return str(round(float(final_result), 3))
# classify list into two lists of numbers and operators
def classify(lst):
number_list = []
operator_list = []
number = ''
for i in lst:
if not is_operator(i) or i == '.':
number = number + i
else:
number_list.append(number)
operator_list.append(i)
number = ''
number_list.append(number)
return number_list, operator_list
# do operation in correct order
def operation(number_list, operator_list):
try:
log_exp(number_list, operator_list)
power(number_list, operator_list)
mul_div(number_list, operator_list)
add_sub(number_list, operator_list)
except ZeroDivisionError:
raise Exception(ZERO_DIVISION)
except InvalidOperation:
raise Exception(INVALID_INPUT)
except ValueError as e:
raise ValueError(MATHEMATICAL_ERROR)
except Exception as e:
raise e
return number_list, operator_list
# store the content outside brackets and compute content inside brackets and store the result in stack
def bracket(lst):
count = 0
if '(' not in lst:
classified = classify(lst)
number_list = classified[0]
operator_list = classified[1]
if stack:
stack.pop()
stack.append(str(operation(number_list, operator_list)[0][0]))
if '(' in stack:
bracket_number = stack.count('(')
for j in range(0, bracket_number):
stack.append(')')
return
else:
i = 0
while i < lst.__len__():
stack.append(lst[i])
if str(lst[i]) == '(':
count = count + 1
new_list = []
i += 1
while count != 0:
if str(lst[i]) == '(':
count += 1
if str(lst[i]) == ')':
count -= 1
if count == 0:
break
new_list.append(lst[i])
i += 1
bracket(new_list)
i += 1
# do log and exp calculation
def log_exp(number_list: List, operator_list: List):
i = 0
while i < operator_list.__len__():
if operator_list[i] == 'l' or operator_list[i] == 'L':
if operator_list.__len__() > i + 2 and (operator_list[i + 1] == 'o' or operator_list[i + 1] == 'O') and (
operator_list[i + 2] == 'g' or operator_list[i + 2] == 'G'):
operator_list.pop(i)
operator_list.pop(i)
operator_list.pop(i)
number_list.pop(i)
number_list.pop(i)
number_list.pop(i)
if number_list[i] is None or number_list[i] == '':
raise ValueError("Invalid Input: log has no arguments given")
if float(number_list[i]) <= 0.0:
raise ValueError(MATHEMATICAL_ERROR)
number_list[i] = math.log(float(number_list[i]))
else:
raise Exception(INVALID_INPUT)
i -= 1
elif operator_list[i] == 'e' or operator_list[i] == 'E':
if operator_list.__len__() > i + 2 and (operator_list[i + 1] == 'x' or operator_list[i + 1] == 'X') and (
operator_list[i + 2] == 'p' or operator_list[i + 2] == 'P'):
operator_list.pop(i)
operator_list.pop(i)
operator_list.pop(i)
number_list.pop(i)
number_list.pop(i)
number_list.pop(i)
number = number_list[i]
if operator_list.__len__() > i and operator_list[i] == '-' and number == '':
operator_list.pop(i)
number = '-' + number_list[i + 1]
number_list.pop(i)
number_list[i] = Decimal(str(math.exp(float(number))))
else:
raise Exception(INVALID_INPUT)
i -= 1
elif operator_list[i] == 'o' or operator_list[i] == 'g' or operator_list[i] == 'x' or operator_list[i] == 'p':
raise Exception(INVALID_INPUT)
i += 1
# do power calculation
def power(number_list: List, operator_list: List):
i = 0
while i < operator_list.__len__():
if operator_list[i] == '^':
number1 = number_list[i]
number2 = number_list[i + 1]
if operator_list.__len__() > i + 1 and operator_list[i + 1] == '-' and number2 == '':
operator_list.pop(i + 1)
number2 = '-' + number_list[i + 2]
number_list.pop(i + 1)
result = Decimal(number1) ** Decimal(number2)
operator_list.pop(i)
number_list.pop(i)
number_list.pop(i)
number_list.insert(i, result)
i -= 1
i += 1
# do multiply and division calculation
def mul_div(number_list: List, operator_list: List):
i = 0
while i < operator_list.__len__():
if operator_list[i] == '*' or operator_list[i] == '/':
number1 = number_list[i]
number2 = number_list[i + 1]
if operator_list.__len__() > i + 1 and operator_list[i + 1] == '-' and number2 == '':
operator_list.pop(i + 1)
number2 = '-' + number_list[i + 2]
number_list.pop(i + 1)
if operator_list[i] == '*':
result = Decimal(number1) * Decimal(number2)
elif operator_list[i] == '/':
result = Decimal(number1) / Decimal(number2)
operator_list.pop(i)
number_list.pop(i)
number_list.pop(i)
number_list.insert(i, result)
i -= 1
i += 1
# do addition and subtraction calculation
def add_sub(number_list: List, operator_list: List):
i = 0
while i < operator_list.__len__():
if operator_list[i] == '+' or operator_list[i] == '-':
number1 = number_list[i]
number2 = number_list[i + 1]
if operator_list.__len__() > i + 1 and operator_list[i + 1] == '-' and number2 == '':
operator_list.pop(i + 1)
number2 = '-' + number_list[i + 2]
number_list.pop(i + 1)
if operator_list[i] == '+':
result = Decimal(number1) + Decimal(number2)
elif operator_list[i] == '-':
if number1 == "":
number1 = '0'
result = Decimal(number1) - Decimal(number2)
operator_list.pop(i)
number_list.pop(i)
number_list.pop(i)
number_list.insert(i, result)
i -= 1
i += 1
# check if is operator
def is_operator(op):
last_op = ''
for i in op:
if i not in OPERATORS:
return False
if op.__len__() > op.index(i) and i != '-' and last_op == i:
return False
last_op = i
return True
# remove whitespace in a string
def remove_whitespace(sequence: str):
return sequence.replace(" ", "")
def calculate(sequence):
try:
sequence = remove_whitespace((sequence))
lst = list(sequence)
if is_valid(lst):
result = calc(lst)
return round(float(result), 5)
return ERROR_INPUT
except Exception as e:
return e
"""
# user input and result output
if __name__ == '__main__':
while True:
sequence = input(WELCOME)
sequence = remove_whitespace((sequence))
print(sequence)
lst = list(sequence)
if sequence == 'q':
break
elif is_valid(lst):
result = calc(lst)
stack.clear()
print("The result for", sequence, "is", "%.3f" % float(result))
else:
sys.exit(ERROR_INPUT)"""