-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalLogic.py
137 lines (134 loc) · 4.96 KB
/
calLogic.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
##### calLogic.py
# Main calculator functionality
# Meant to be called from outside
# Point of entry is def interpret(args, ans)
#
#imports
import math
import trig
##### INTERPRET
def parse_args(args):
operators = ["+","-","*",'/']
temp_storage = ['']*32
result = list(args)
#parse all operators and slot them in individual indexes
index = 0
check = 0
for element in result:
if(element in operators) and check == 0:
index = index + 1
temp_storage[index] = element
index = index + 1
check = 1 #check to prevent 2 consecutive operators
else:
temp_storage[index] = temp_storage[index]+element
check = 0
#strip empty space
temp_storage[:] = (value for value in temp_storage if value != '')
#prevent first element from being an operator
if temp_storage[0] in operators:
if temp_storage[0] == '-':
temp_storage[1] = '-'+temp_storage[1]
temp_storage.pop(0)
return temp_storage
#### INTERPRET
def interpret(args,ans):
#check if passed args is already a parsed array
if isinstance(args,list):
temp_storage = args
else:
temp_storage = parse_args(args)
#parse all exceptions: exponent, sqrt, logarithms and trig functions
index = 0
for element in temp_storage:
factor = 1
if("√" in element): #parse sqrt
sqrt_index = element.index("√")
if(sqrt_index!=0): #check factor before sqrt
factor=float(element[:sqrt_index])
temp_storage[index] = factor * math.sqrt(float(element[(sqrt_index+1):]))
elif("^" in element): #parse exponent
exp_index = element.index("^")
base = float(element[:exp_index])
exp = float(element[(exp_index+1):])
temp_storage[index] = math.pow(base,exp)
elif("log" in element): #parse logarithms
log_index = element.index("log")
if(log_index != 0):
factor = float(element[:log_index])
temp_storage[index] = factor * math.log10(float(element[(log_index+3):]))
elif("sin" in element): #parse trig functions
sin_index = element.index("sin")
value = float(element[(sin_index+3):])
if(sin_index != 0):
factor = float(element[:sin_index])
temp_storage[index] = factor * trig.sin(value)
elif("cos" in element):
cos_index = element.index("cos")
value = float(element[(cos_index+3):])
if(cos_index != 0):
factor = float(element[:cos_index])
temp_storage[index] = factor * trig.cos(value)
elif("tan" in element):
tan_index = element.index("tan")
value = float(element[(tan_index+3):])
if(tan_index != 0):
factor = float(element[:tan_index])
if(trig.tan(value) == "undefined"):
return "undefined"
temp_storage[index] = factor * trig.tan(value)
elif("cot" in element):
cot_index = element.index("cot")
value = float(element[(cot_index+3):])
if(cot_index != 0):
factor = float(element[:cot_index])
if(trig.cot(value) == "undefined"):
return "undefined"
temp_storage[index] = factor * trig.cot(value)
elif("ans" in element):
ans_index = element.index("ans")
if(ans_index != 0):
factor = float(element[:ans_index])
temp_storage[index] = factor * float(ans)
index = index + 1
#return solved expression to JS
return solve(temp_storage,ans)
##### SOLVE
def solve(temp_storage,ans):
#first solve multiplication and division
index = 0
for element in temp_storage:
if(temp_storage[index]=='*' or temp_storage[index]=='/'):
if(temp_storage[index]=='*'):
temp_storage[index-1]=float(temp_storage[index-1])*float(temp_storage[index+1])
else:
temp_storage[index-1]=float(temp_storage[index-1])/float(temp_storage[index+1])
temp_storage.pop(index)
temp_storage.pop(index)
continue
index = index + 1
#now solve addition and subtraction
val_storage = 0
#redundant negative first element check
if(temp_storage[0]=='-'):
val_storage = float(temp_storage[1])*-1
temp_storage.pop(0)
else:
val_storage = float(temp_storage[0])
#now finally solve
index = 0
for element in temp_storage:
if(element == '+'):
val_storage = val_storage + float(temp_storage[index+1])
elif(element == '-'):
val_storage = val_storage - float(temp_storage[index+1])
index = index + 1
return val_storage
##### log
def debug(args):
try:
for element in args:
print(">",element, end =" ")
print("")
except:
print("> invalid args passed to log()")