Python
- voluntas
- @ignis_fatuus
- ブン
- @Linda_pp
- 清楚なC++メイドBOT
- @tzik_tack
- 長谷川一輝
- wraith13
- @jj1bdx
- @cpp_akira
- 安藤敏彦
- @srz_zumix
- Siv3D
- takezoh
- まろ
- @okdshin
- @hnokx
- @ishidakei
- @take_cheeze
- TAKEI Yuya
- @mumumu
- I (@wx257osn2)
- Tommy6
- @tyottyoworks
- ___shanon
- わたやん
- @KorekaraSEDB
- @kariya_mitsuru
- @ciniml
- @beam2d
- @grafi_tt
- @nekketsuuu
- LouiS0616
- @volanja
- 大鎌広
- むてら
- ガチKGB
- 三重野賢人
x
87
converted_expr.append(calc(op_stack.pop()[1]))
1
import sys
2
3
def add(a, b):
4
return a + b
5
6
def mul(a, b):
7
return a * b
8
9
def add_num(n):
10
return lambda s: s.append(n)
11
12
def calc(f):
13
def do_calc(s):
14
s[-2] = f(s[-2], s[-1])
15
s.pop()
16
return do_calc
17
18
q = sys.stdin.readline().rstrip()
19
20
converted_expr = []
21
op_stack = []
22
23
zero = ord('0')
24
25
current = -1
26
27
prev_kokka = False
28
29
for c in q:
30
v = ord(c) - zero
31
if c == '+':
32
if current >= 0:
33
converted_expr.append(add_num(current))
34
current = -1
35
while len(op_stack) > 0 and op_stack[-1][0] >= 0:
36
converted_expr.append(calc(op_stack.pop()[1]))
37
op_stack.append((0, add))
38
elif c == '*':
39
if current >= 0:
40
converted_expr.append(add_num(current))
41
current = -1
42
while len(op_stack) > 0 and op_stack[-1][0] >= 1:
43
converted_expr.append(calc(op_stack.pop()[1]))
44
op_stack.append((1, mul))
45
elif c == '(':
46
if current >= 0:
47
converted_expr.append(add_num(current))
48
current = -1
49
while len(op_stack) > 0 and op_stack[-1][0] >= 1:
50
converted_expr.append(calc(op_stack.pop()[1]))
51
op_stack.append((1, mul))
52
op_stack.append((-1, None))
53
elif c == ')':
54
if current >= 0:
55
converted_expr.append(add_num(current))
56
current = -1
57
while len(op_stack) > 0 and op_stack[-1][0] >= 0:
58
converted_expr.append(calc(op_stack.pop()[1]))
59
op_stack.pop()
60
elif 0 <= v and v <= 9:
61
if current < 0:
62
current = 0
$ python3 prog.py
Stdin
3+4(2*6+3)
Start
63
0
Finish