华为面试


def eval(input_str):
    op1 = ('+','-')
    op2 = ('*','/')
    
    temp = []
    result = 0
    num = ""
    for s in input_str:
        if s not in op1 and s not in op2:
           num = num + s
        else:
            temp.append(int(num))
            temp.append(s)
            num = ""
    temp.append(int(num))

    print(temp)

    run = False
    temp1 = []
    for i, d in enumerate(temp):
        if run:
            run = False
            continue
        if d not in op2:
           temp1.append(d)
        else:
            if d == '*':
                r = temp1[-1] * temp[i+1]
            else:
                r = int(temp1[-1] / temp[i+1])
            temp1.pop()
            temp1.append(r)
            run = True
    print(temp1)

    run = False
    temp2 = []
    for i, d in enumerate(temp1):
        if run:
            run = False
            continue
        if d not in op1:
           temp2.append(d)
        else:
            if d == '+':
                r = temp2[-1] + temp1[i+1]
            else:
                r = temp2[-1] - temp1[i+1]
            temp2.pop()
            temp2.append(r)
            run = True
    print(temp2)

    result = temp2[0]
    return result

if __name__ == "__main__":
    str = "10+5*3/2-7*2+1"
    print(eval(str))