-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
167 lines (148 loc) · 5.17 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
def solve(num1, num2, operator):
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "/":
result = num1/num2
elif operator == "*":
result = num1*num2
elif operator == "^":
result = num1 ** num2
return result
def calculate(cont, val=0):
if not cont:
while True:
try:
num1 = int(input("Enter the first number: "))
break
except:
print("This value needs to be a number")
else:
num1 = val
while True:
try:
if cont:
num2 = int(input("Enter next number: "))
else:
num2 = int(input("Enter the second number: "))
break
except:
print("This value needs to be a number")
while True:
operation = input("Enter the operator (+, -, /, *, ^): ")
if operation not in "+-/*^":
print("Your operation must be of the following: +, -, /, *, ^")
else:
break
result = solve(num1, num2, operation)
print(num1, operation, num2, "=", result)
return result
def normalCalculator():
result = calculate(False)
while True:
cont = input("Do you want to continue the calculation (y/N)? ")
if cont != "y" and cont != "Y":
break
result = calculate(True, result)
def backSearchNum(expression, index):
num = ""
while index >= 0:
index -= 1
if index >= 0 and expression[index] in "0123456789.":
num = expression[index] + num
elif index == 0 and expression[index] == "-":
num = expression[index] + num
else:
break
return num
def forwardSearchNum(expression, index):
num = ""
if expression[index + 1] == "-":
num = "-"
index += 1
while index < len(expression):
index += 1
if index < len(expression) and expression[index] in "0123456789.":
num = num + expression[index]
elif index == 0 and expression[index] == "-":
num = num + expression[index]
else:
break
return num
def pemdas(expression, operators):
index = 0
while index < len(expression):
letter = expression[index]
if letter in operators:
num1 = backSearchNum(expression, index)
if letter == "-" and num1 == "":
expression = expression[:index] + "0" + expression[index:]
continue
num2 = forwardSearchNum(expression, index)
value = solve(float(num1), float(num2), letter)
expression = expression[:index - len(num1)] + str(value) + expression[index + 1 + len(num2):]
index = index - len(num1) + len(str(value)) - 1
index += 1
return expression
def eval(expression):
returnVal = 0
operators = ["^", "/*", "-+"]
for operatorSet in operators:
expression = pemdas(expression, operatorSet)
return expression
def parseExpression(expression):
expression = " ".join(expression.split())
expression = expression.replace(" ", "")
base = True
start = 0
end = 0
counter = 0
operators = "^+-/*"
if expression.count("(") != expression.count(")"):
raise Exception()
index = -1
while index < len(expression) - 1:
index += 1
letter = expression[index]
if letter == "(":
counter += 1
if base:
base = False
start = index
elif letter == ")":
counter -= 1
if not counter and not base:
end = index
replaceVal = parseExpression(expression[start+1: end])
if start == 0 or expression[start - 1] in operators:
expression = expression[:start] + str(replaceVal) + expression[end + 1:]
else:
expression = expression[:start] + "*" + str(replaceVal) + expression[end + 1:]
base = True
index = start + len(str(replaceVal)) - 1
if expression[0] == "*":
expression = expression[1:]
if base:
return eval(expression)
def advancedCalculator():
expression = input("Type in a calculation to complete. You may use operations (+, -, /, *, ^). This result will follow PEMDAS. (eg. (3 + 2 * 3)(4^2) = 144.0\n")
try:
print(expression, "=", str(parseExpression(expression)))
except:
print("Something went wrong. Please make sure that expression is correct. (eg. Same number of opening and closing parantheses or number may be too big)")
if __name__ == "__main__":
while True:
while True:
option = input("Advanced (A) or Normal (N) calculator? ")
if option not in "AaNn":
print("You need to choose either A for Advanced or N for Normal")
else:
break
if option == "A" or option == "a":
advancedCalculator()
else:
normalCalculator()
cont = input("Do you want to do another calculation (y/N)? ")
if cont != "y" and cont != "Y":
break