Skip to content

Commit ff45c99

Browse files
committed
Pycalc release v1.0.9.
1 parent b86e3f9 commit ff45c99

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

final_task/pycalc/pycalc.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ def stack_from_string(self, input_string):
125125
break_bool = True
126126
break
127127

128+
pattern = r"log\(.+,"
129+
tmp = re.search(pattern, input_string)
130+
while tmp:
131+
pos = tmp.start()
132+
first_part = input_string[:pos]
133+
second_part = input_string[pos:]
134+
second_part = re.sub("^log", "log2", second_part)
135+
input_string = first_part + second_part
136+
tmp = re.search(pattern, input_string)
137+
128138
str_and_tag = namedtuple("str_and_tag", ("s", "tag"))
129139
string_as_stack = PyCalc.lexer(input_string, self.token_exprs, str_and_tag)
130140

@@ -183,7 +193,7 @@ def rpn_from_stacked_string(self, stack):
183193
elif item == ",":
184194
while temporary_stack[-1] != "(":
185195
rpn_stack.append(temporary_stack.pop())
186-
elif self.operators[temporary_stack[-1]].priority < self.operators[item].priority:
196+
elif self.operators[temporary_stack[-1]].priority <= self.operators[item].priority and item == "^":
187197
temporary_stack.append(item)
188198
else:
189199
temp_priority = self.operators[item].priority
@@ -255,7 +265,10 @@ def calculate(self, input_string):
255265
print(rerror.args[0])
256266
exit(1)
257267
except ValueError as verror:
258-
print(verror.args[0])
268+
print("ERROR: unknown operand!")
269+
exit(1)
270+
except Exception:
271+
print("ERROR: unknown error!")
259272
exit(1)
260273
return result
261274

@@ -287,6 +300,7 @@ def get_math_operators(math_priority, tag_operators, tag_constants, tuple_templa
287300
tag_constants)})
288301

289302
math_operators.update({'log': tuple_template(math.log, math_priority, 1, 'log', tag_operators)})
303+
math_operators.update({'log2': tuple_template(math.log, math_priority, 2, 'log2', tag_operators)})
290304

291305
return math_operators, math_constants
292306

@@ -326,5 +340,6 @@ def main():
326340
main()
327341

328342
# calc = PyCalc()
329-
# result = calc.calculate('6 < = 6')
343+
# result = calc.calculate('sin(e^log(e^e^sin(23.0),45.0) + cos(3.0+log10(e^-e)))')
344+
# # result = calc.calculate('5+2*2^2^2^(2-1)*sin(pi/2)')
330345
# print(result)

0 commit comments

Comments
 (0)