Repository navigation
Expand file tree
/
Copy pathparseMath.py
More file actions
40 lines (37 loc) · 1.51 KB
/
Copy pathparseMath.py
File metadata and controls
40 lines (37 loc) · 1.51 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
# a simple function for parsing and executing math-based strings
# doesn't use eval(), so problems don't happen
def parseMath(string):
currentValue = ""
valueStack = []
for i in string:
if i in "+-*/":
if all([i in "1234567890." for i in currentValue]) and not currentValue == "":
valueStack.append(float(currentValue))
valueStack.append(i)
currentValue = ""
else:
raise ValueError("Malformed math string")
elif not i == " ":
currentValue += i
if currentValue != "":
valueStack.append(float(currentValue))
#print(valueStack)
for i in "*/+-":
while valueStack.count(i) > 0:
index = valueStack.index(i)
valueStack.pop(index)
valueA = valueStack.pop(index-1)
valueB = valueStack.pop(index-1)
if i == "*":
#print("%f * %f = %f" %(valueA,valueB,valueA*valueB))
valueStack.insert(index-1,valueA*valueB)
elif i == "/":
#print("%f / %f = %f" %(valueA,valueB,valueA/valueB))
valueStack.insert(index-1,valueA/valueB)
elif i == "+":
#print("%f + %f = %f" %(valueA,valueB,valueA+valueB))
valueStack.insert(index-1,valueA+valueB)
elif i == "-":
#print("%f - %f = %f" %(valueA,valueB,valueA-valueB))
valueStack.insert(index-1,valueA-valueB)
return valueStack[0]