Skip to content

Commit da3fcbb

Browse files
committed
Merge branch 'develop' into release
2 parents c10d1b3 + 43150b5 commit da3fcbb

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

calculator.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
import sys
2-
2+
import math
33

44
class Parser:
55
def parse(self, expr):
66
tokens = []
7+
functions = ['sin', 'cos']
78
currentlyNumber = False
89
currentlyFrac = False
10+
currentlyFunc = False
911
number = 0
12+
func = ''
1013
for c in expr:
14+
if currentlyNumber and c not in '0123456789.':
15+
currentlyNumber = False
16+
currentlyFrac = False
17+
tokens.append(float(number))
18+
elif currentlyFunc and not ('a' <= c and c <= 'z'):
19+
if func in functions:
20+
tokens.append(func[0])
21+
currentlyFunc = False
22+
else:
23+
return False
1124
if ('0' <= c and c <= '9') or c == '.':
1225
if c == '.':
1326
if currentlyFrac is False:
@@ -20,11 +33,13 @@ def parse(self, expr):
2033
currentlyNumber = True
2134
number = c
2235
continue
23-
elif currentlyNumber:
24-
currentlyNumber = False
25-
currentlyFrac = False
26-
tokens.append(float(number))
27-
if c in "+-*/()":
36+
elif 'a' <= c and c <= 'z':
37+
if currentlyFunc:
38+
func += c
39+
else:
40+
currentlyFunc = True
41+
func = c
42+
elif c in "+-*/^()":
2843
tokens.append(c)
2944
elif c == ' ':
3045
continue
@@ -37,7 +52,7 @@ def parse(self, expr):
3752
if isinstance(prev_token, str):
3853
if prev_token == '(' and tokens[i] == ')':
3954
return False
40-
if prev_token in "(+-*/":
55+
if prev_token in "(+-*/^sc":
4156
if tokens[i] == '+':
4257
tokens[i] = '#'
4358
elif tokens[i] == '-':
@@ -49,7 +64,6 @@ def parse(self, expr):
4964
return tokens
5065

5166
class Calculator:
52-
5367
def shuntingYard(self, tokens):
5468
out = []
5569
operator = []
@@ -65,16 +79,22 @@ def shuntingYard(self, tokens):
6579
return False
6680
else:
6781
operator.pop()
82+
elif token in "sc":
83+
operator.append(token)
6884
elif token in "#_":
69-
while operator and operator[-1] in "#_":
85+
while operator and operator[-1] in "#_sc":
86+
out.append(operator.pop())
87+
operator.append(token)
88+
elif token == '^':
89+
while operator and operator[-1] in "#_^sc":
7090
out.append(operator.pop())
7191
operator.append(token)
7292
elif token in "*/":
73-
while operator and operator[-1] in "#_*/":
93+
while operator and operator[-1] in "#_^*/sc":
7494
out.append(operator.pop())
7595
operator.append(token)
7696
elif token in "+-":
77-
while operator and operator[-1] in "#_*/+-":
97+
while operator and operator[-1] in "#_^*/+-sc":
7898
out.append(operator.pop())
7999
operator.append(token)
80100
while operator:
@@ -95,7 +115,7 @@ def calculate(self, string, postfix = False):
95115
return False
96116
if isinstance(token, float):
97117
stack.append(token)
98-
elif token in "#_":
118+
elif token in "#_sc":
99119
if stack:
100120
first = stack.pop()
101121
else:
@@ -125,9 +145,15 @@ def evaluate(self, operation, first, second = 0):
125145
return first * second
126146
elif operation == '/':
127147
return first / second
148+
elif operation == '^':
149+
return math.pow(first, second)
128150
elif operation == '#':
129151
return first
130152
elif operation == '_':
131153
return -first
154+
elif operation == 's':
155+
return math.sin(first)
156+
elif operation == 'c':
157+
return math.cos(first)
132158
else:
133159
return False

0 commit comments

Comments
 (0)