MCPcopy Create free account
hub / github.com/ActiveState/code / Infix

Function Infix

recipes/Python/579122_Simple_Infix/recipe-579122.py:7–33  ·  view source on GitHub ↗
(expr)

Source from the content-addressed store, hash-verified

5# and aritmetic operators.
6# FB - 20151107
7def Infix(expr):
8 expr = list(expr)
9 stack = list()
10 num = ""
11 while len(expr) > 0:
12 c = expr.pop(0)
13 if c in "0123456789.":
14 num += c
15 else:
16 if num != "":
17 stack.append(num)
18 num = ""
19 if c in "+-*/":
20 stack.append(c)
21 elif c == ")":
22 num2 = stack.pop()
23 op = stack.pop()
24 num1 = stack.pop()
25 if op == "+":
26 stack.append(str(float(num1) + float(num2)))
27 elif op == "-":
28 stack.append(str(float(num1) - float(num2)))
29 elif op == "*":
30 stack.append(str(float(num1) * float(num2)))
31 elif op == "/":
32 stack.append(str(float(num1) / float(num2)))
33 return stack.pop()
34
35expr = "((1.7+((2.8*3.6)/(0-9.4)))-5)"
36print expr

Callers 1

recipe-579122.pyFile · 0.70

Calls 4

listClass · 0.85
strFunction · 0.85
popMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected