-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
337 lines (301 loc) · 11.7 KB
/
main.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# This project created by Enadream, enadream.com
class BasicOperations:
def _addition(self, num1, num2): # protected
result = "error"
try:
result = str(float(num1) + float(num2))
except TypeError:
Console.PrintError(2)
return result
def _subtraction(self, num1, num2):
result = "error"
try:
result = str(float(num1) - float(num2))
except TypeError:
Console.PrintError(2)
return result
def _multiplication(self, num1, num2):
result = "error"
try:
result = str(float(num1) * float(num2))
except TypeError:
Console.PrintError(2)
return result
def _division(self, num1, num2):
result = "error"
try:
result = str(float(num1) / float(num2))
except TypeError:
Console.PrintError(2)
return result
def _exponential(self, num1, num2):
result = "error"
try:
result = str(float(num1) ** float(num2))
except TypeError:
Console.PrintError(2)
return result
def _modulus(self, num1, num2):
result = "error"
try:
result = str(float(num1) % float(num2))
except TypeError:
Console.PrintError(2)
return result
@staticmethod
def find_all(a_str, sub_str): # Find all substring position
start = 0
subList = []
while start < len(a_str):
start = a_str.find(sub_str, start)
if start == -1:
return
subList.append(start)
start += len(sub_str) # use start += 1 to find overlapping matches
return subList
class Console:
errorStatus = False
@staticmethod
def PrintError(errorCode, add_txt=""):
Console.errorStatus = True
print("[ERROR]: ", end="")
if errorCode == 0:
print("The string contains undefined characters")
elif errorCode == 1:
print("The string syntax is wrong")
elif errorCode == 2:
print("The string contains inconvenient operation order")
elif errorCode == 3:
pass
@staticmethod
def PrintWarning(warningCode, add_text=""):
print("[Warning]: ", end="")
if warningCode == 0:
print("Unknown letter found in the string")
elif warningCode == 1:
print("You made a parentheses mistake please fix it")
elif warningCode == 2:
pass
elif warningCode == 3:
pass
class Calculator(BasicOperations):
def __init__(self): # default constructor
self.MainStr = ""
self.Result = 0.0
def GetString(self):
Console.errorStatus = False
print("\nEnter the operation you want to calculate: ")
self.MainStr = str(input())
self.__checkString(self.MainStr)
# Editing string and checking
def __checkString(self, mainStr): # private
ALLOWEDCHARS = "0123456789.*/+-^%()[]"
# ALLOWEDNAMES = ("sin", "cos", "tan", "cot") if the trigonometric operator allowed
# Remove the spaces and make lower
mainStr = mainStr.replace(" ", "")
mainStr = mainStr.lower()
# Check String
tempStr = mainStr
# for names in ALLOWEDNAMES: tempStr = tempStr.replace(names, "")
for char in tempStr:
if not (char in ALLOWEDCHARS):
Console.PrintError(0)
self.GetString()
return 0
self.__mainSplitter(mainStr)
# Parentheses Splitter
def __mainSplitter(self, fullString):
indexOfPar = [] # This is for to save of the indexes of initial parentheses
tempStr = ""
stepNum = 1
i = 0
while i < len(fullString): # Remove all parentheses
if fullString[i] == "(":
indexOfPar.append(i)
i += 1
elif fullString[i] == ")":
if len(indexOfPar) > 0:
self.__printString(fullString, stepNum)
stepNum += 1
tempStr = fullString[(indexOfPar[-1] + 1):i]
tempStr = self.__calculator(tempStr)
fullString = fullString[0:indexOfPar[-1]] + tempStr + fullString[i + 1:]
i = indexOfPar[-1]
indexOfPar.pop(-1)
else:
Console.PrintWarning(1)
break
else:
i += 1
if len(indexOfPar) == 0: # check the list to be sure about that parentheses list is empty
self.__printString(fullString, stepNum)
self.Result = self.__calculator(fullString)
else:
Console.PrintWarning(1)
if not Console.errorStatus:
print("\n[RESULT] : " + str(self.Result))
else:
print("\n[RESULT] : error")
Console.errorStatus = False
# Finding operations and numbers in the string
def __extraction(self, subString):
i = 0
tempList = []
numbers = []
operators = []
tempNum = ""
DIGITS = "0123456789."
OPERATIONS = "*/+-^%"
while i < len(subString):
if subString[i] in DIGITS:
tempNum += subString[i]
elif subString[i] in OPERATIONS:
if tempNum != "": # Check the tempNum for empty then add it to numbers list
tempList.append(tempNum)
numbers.append(tempNum)
tempNum = ""
if subString[i] in "+-": # Is the symbol sign or operation ?
if len(tempList) > 0:
if tempList[-1] in OPERATIONS: # Is the last item an Operation ?
tempNum += subString[i]
else:
tempList.append(subString[i])
operators.append(subString[i])
else:
tempNum += subString[i]
else:
tempList.append(subString[i])
operators.append(subString[i])
elif subString[i] == "e": # When the number is scientific
tempNum += subString[i] + subString[i + 1]
i += 1
else: # When the char is neither Digits nor operations
Console.PrintError(0)
i += 1
else: # The last number has to be still in the tempNum
tempList.append(tempNum)
numbers.append(tempNum)
tempStr = ""
return numbers, operators
# Calculating math operations
def __calculator(self, subString):
i = 0
numbers, operators = self.__extraction(subString)
self.__printSteps(numbers, operators)
isFirst, isSecond, isThird = False, False, False
# Calculating exponential and modulus operations [first part]
while i < len(operators):
if operators[i] == "^":
numbers[i] = self._exponential(numbers[i], numbers[i + 1])
if numbers[i] == "error":
break
operators.pop(i)
numbers.pop(i + 1)
isFirst = True
elif operators[i] == "%":
numbers[i] = self._modulus(numbers[i], numbers[i + 1])
if numbers[i] == "error":
break
operators.pop(i)
numbers.pop(i + 1)
isFirst = True
else:
i += 1
i = 0
if isFirst:
self.__printSteps(numbers, operators)
# Calculating multiplication and division operations [second part]
while i < len(operators):
if operators[i] == "*":
numbers[i] = self._multiplication(numbers[i], numbers[i + 1])
if numbers[i] == "error":
break
operators.pop(i)
numbers.pop(i + 1)
isSecond = True
elif operators[i] == "/":
numbers[i] = self._division(numbers[i], numbers[i + 1])
if numbers[i] == "error":
break
operators.pop(i)
numbers.pop(i + 1)
isSecond = True
else:
i += 1
i = 0
if isSecond:
self.__printSteps(numbers, operators)
# Calculating addition and subtraction operations [third part]
while i < len(operators):
if operators[i] == "+":
numbers[i] = self._addition(numbers[i], numbers[i + 1])
if numbers[i] == "error":
break
operators.pop(i)
numbers.pop(i + 1)
isThird = True
elif operators[i] == "-":
numbers[i] = self._subtraction(numbers[i], numbers[i + 1])
if numbers[i] == "error":
break
operators.pop(i)
numbers.pop(i + 1)
isThird = True
else:
i += 1
if isThird:
self.__printSteps(numbers, operators)
return numbers[0]
# Printing calculation steps
def __printSteps(self, numbers, operators):
print("==>", end="")
for index, num in enumerate(numbers):
print(" " + num, end="")
if int(index) < len(operators):
print(" " + operators[int(index)], end="")
print("\n", end="")
# Printing current string
def __printString(self, string, stepNo):
DIGITS = "0123456789.()"
OPERATIONS = "*/+-^%"
tempStr = ""
i = 0
while i < len(string):
if string[i] in DIGITS:
tempStr += string[i]
elif string[i] in OPERATIONS:
if i == 0:
tempStr += string[i] + " "
elif tempStr[-1] == " ": # check for the last char was operations
tempStr += string[i]
elif tempStr[-1] == "(": # check for the last char was parentheses
tempStr += string[i]
else:
tempStr += " " + string[i] + " "
elif string[i] == "e":
tempStr += string[i] + string[i + 1]
i += 1
else:
Console.PrintWarning(0)
i += 1
print("\n[STEP " + str(stepNo) + "]")
print("[CURRENT STRING] : " + tempStr)
print("\n________ WELCOME TO THE ADVANCED CALCULATOR ________")
print("------------------------------------------------")
print("This calculator can handle difficult algebraic equations. You are able")
print("to use those operators; '*' for multiply, '/' for divide, '+' for sum,")
print("'-' for subtraction, '%' for mod, '^' for exponential. Also you are able")
print("to use parentheses to explain your calculation well. And the spaces in")
print("the string are not important.")
print("Ex. string : (((5+6)*45)^3/77 + 17 - (4.5 * -4)+ 174) / (5^4 % 7)")
print("------------------------------------------------")
newCalc = Calculator()
while True:
newCalc.GetString()
print("__________________________________________________")
print("\nDo you want to do a new calculation ? (\"y\":yes, \"n\":no) : ", end="")
cont = str(input())
if cont == "y" or cont == "Y" or cont == "yes":
pass
else:
break