電卓作成
Posted: 2020年10月19日(月) 13:05
C言語で式を入力すると式の値が出てくるようなプログラミングを作成したいです。とりあえず足し算と掛け算と括弧の3つの演算記号の組み合わせだけで作りたいです。ソースコードを教えていただけると助かります。例:入力3+4(2*6+3) 出力63
import sys
def add(a, b):
return a + b
def mul(a, b):
return a * b
def add_num(n):
return lambda s: s.append(n)
def calc(f):
def do_calc(s):
s[-2] = f(s[-2], s[-1])
s.pop()
return do_calc
q = sys.stdin.readline().rstrip()
converted_expr = []
op_stack = []
zero = ord('0')
current = -1
prev_kokka = False
for c in q:
v = ord(c) - zero
if c == '+':
if current >= 0:
converted_expr.append(add_num(current))
current = -1
while len(op_stack) > 0 and op_stack[-1][0] >= 0:
converted_expr.append(calc(op_stack.pop()[1]))
op_stack.append((0, add))
elif c == '*':
if current >= 0:
converted_expr.append(add_num(current))
current = -1
while len(op_stack) > 0 and op_stack[-1][0] >= 1:
converted_expr.append(calc(op_stack.pop()[1]))
op_stack.append((1, mul))
elif c == '(':
if current >= 0:
converted_expr.append(add_num(current))
current = -1
while len(op_stack) > 0 and op_stack[-1][0] >= 1:
converted_expr.append(calc(op_stack.pop()[1]))
op_stack.append((1, mul))
op_stack.append((-1, None))
elif c == ')':
if current >= 0:
converted_expr.append(add_num(current))
current = -1
while len(op_stack) > 0 and op_stack[-1][0] >= 0:
converted_expr.append(calc(op_stack.pop()[1]))
op_stack.pop()
elif 0 <= v and v <= 9:
if current < 0:
current = 0
if prev_kokka:
while len(op_stack) > 0 and op_stack[-1][0] >= 1:
converted_expr.append(calc(op_stack.pop()[1]))
op_stack.append((1, mul))
current = current * 10 + v
else:
raise Exception("invalid character")
prev_kokka = c == ')'
if current >= 0:
converted_expr.append(add_num(current))
while len(op_stack) > 0:
converted_expr.append(calc(op_stack.pop()[1]))
calc_stack = []
for op in converted_expr:
op(calc_stack)
if len(calc_stack) != 1:
raise Exception("invalid expression")
print(calc_stack[0])