-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.py
99 lines (95 loc) · 3.33 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
"""
Calculator.py
A calculator that takes input as a string from the user, and recursively solves
the given problem. Reports "divide by zero" errors and bracket mismatch errors.
"""
def add(a,b):
if a is None or b is None:
return None
return a + b
def minus(a,b):
if a is None or b is None:
return None
return a - b
def times(a,b):
if a is None or b is None:
return None
return a * b
def divide(a,b):
if b == 0:
print("Divide by zero error!")
return None
elif a is None or b is None:
return None
return a / b
def power(a,b):
if a is None or b is None:
return None
return pow(a, b)
def calculator(problem):
if problem.count("(") != problem.count(")"):
print("Bracket count mismatch error!")
return None
elif "(" in problem:
lastOpenBracket = problem.rindex("(")
nextCloseBracket = problem.find(")", lastOpenBracket)
if nextCloseBracket == -1:
print("Bracket order mismatch error!")
return None
innerTotal = calculator(problem[lastOpenBracket+1:nextCloseBracket])
if innerTotal is None:
return None
# Convert to string as a float (not exponential for small numbers)
innerPart = format(innerTotal, 'f')
leftPart = problem[0:lastOpenBracket]
if len(leftPart) > 0 and not leftPart[-1] in "+-*/^(":
leftPart += "*"
rightPart = problem[nextCloseBracket+1:]
if len(rightPart) > 0 and not rightPart[0] in "+-*/^)":
rightPart = "*" + rightPart
return calculator(leftPart+innerPart+rightPart)
elif "+" in problem:
parts = problem.split("+")
subtotal = calculator(parts[0])
for i in range(1, len(parts)):
subtotal = add(subtotal, calculator(parts[i]))
return subtotal
elif "-" in problem:
parts = problem.split("-")
subtotal = calculator(parts[0])
for i in range(1, len(parts)):
subtotal = minus(subtotal, calculator(parts[i]))
return subtotal
elif "*" in problem:
parts = problem.split("*")
subtotal = calculator(parts[0])
for i in range(1, len(parts)):
subtotal = times(subtotal, calculator(parts[i]))
return subtotal
elif "/" in problem:
parts = problem.split("/")
subtotal = calculator(parts[0])
for i in range(1, len(parts)):
subtotal = divide(subtotal, calculator(parts[i]))
return subtotal
elif "^" in problem:
parts = problem.split("^")
subtotal = calculator(parts[0])
for i in range(1, len(parts)):
subtotal = power(subtotal, calculator(parts[i]))
return subtotal
elif problem == "":
print("Error! Operators must be used between numbers.")
else:
return float(problem)
result = None
print("Enter problem(s), or 'Q' to quit. Use numbers and the symbols + - * / ^ ( ) only.")
while True:
calculation = input("> ")
if calculation.upper() == 'Q':
break
if not(result is None) and calculation[0] in "+-*/^":
calculation = format(result, 'f') + calculation
result = calculator(calculation)
if not(result is None):
print("=", result)