#41278: python


suyueh (suyueh)

School : No School
ID : 272111
IP address : [101.10.93.239]
Last Login :
2024-12-10 22:48:32
a017. 五則運算 | From: [125.229.229.84] | Post Date : 2024-07-16 14:00

import sys
def count(a, b, op):
    if (op=='+'):
        return a+b
    if (op=='-'):
        return a-b
    if (op=='*'):
        return a*b
    if (op=='/'):
        return a//b
    if (op=='%'):
        return a%b
    return -999
def calculate(expr):
    exprs = expr.split()
    opers = []
    nums = []
    prior = {'+':1, '-':1, '*':2, '/':2, '%':2, '(':-1}
    for ele in exprs:
        if ele=='(':
            opers.append('(')
        elif ele==')':
            while opers[-1] != '(':
                val2 = nums.pop(); val1 = nums.pop();
                op = opers.pop()
                result = count(val1, val2, op)
                nums.append(result)
            opers.pop()
        elif ( ele in ['+', '-', '*', '/', '%'] ):
            while (opers and prior[ele]<=prior[opers[-1]]):
                val2 = nums.pop(); val1 = nums.pop();
                op = opers.pop()
                result = count(val1, val2, op)
                nums.append(result)
            opers.append(ele)
        else:
            nums.append( int(ele) )
    while (opers):
        val2 = nums.pop(); val1 = nums.pop();
        op = opers.pop()
        result = count(val1, val2, op)
        nums.append(result)
    return nums[-1]
for inp in sys.stdin:
    ans = calculate(inp)
    print(ans)
 
ZeroJudge Forum