四则运算练习

时间:2023-01-09 09:52:10

递归:

def oper_two(num_one, oper, num_two):
    if oper == '+':
        return str(int(num_one) + int(num_two))
    if oper == '-':
        return str(int(num_one) - int(num_two))
    if oper == '*':
        return str(int(num_one) * int(num_two))
    if oper == '/':
        if num_two != '0':
            return str(int(num_one) / int(num_two))
    print 'oper_two error!'

#a = oper_two('4', '/', '2')
#print a

def compute_str(mystr):
    print "str is: " , mystr
    if len(mystr) < 4:
        return oper_two(mystr[0], mystr[1], mystr[2])
    if mystr[1] == '*' or mystr[1] == '/':
        return compute_str(compute_str(mystr[0:3]) + mystr[3:])
    elif mystr[3] == '*' or mystr[3] == '/':
        return compute_str(mystr[0:2] + compute_str(mystr[2:5]) + mystr[5:])
    else:
        return compute_str(compute_str(mystr[:3]) + mystr[3:])


def compute_allstr(allstr):
    print "allstr is:" , allstr
    first = allstr.find('(')
    last = allstr.rfind(')')
#    print first , 'and', last
    if first != -1 and last != -1:
        return compute_allstr(allstr[:first] + compute_str(allstr[first+1:last]) + allstr[last+1:])
    else:
        return compute_str(allstr)
    

#arith_str = "1+2*3-7/2"
#result = compute_str(arith_str)

arith_str = "1+2*(7-3)/2"
result = compute_allstr(arith_str)

print result

 输出:

allstr is: 1+2*(7-3)/2
str is:  7-3
allstr is: 1+2*4/2
str is:  1+2*4/2
str is:  2*4
str is:  1+8/2
str is:  8/2
str is:  1+4
5

def oper_two(num_one, oper, num_two):
    if oper == '+':
        return str(int(num_one) + int(num_two))
    if oper == '-':
        return str(int(num_one) - int(num_two))
    if oper == '*':
        return str(int(num_one) * int(num_two))
    if oper == '/':
        if num_two != '0':
            return str(int(num_one) / int(num_two))
    print 'oper_two error!'

#a = oper_two('4', '/', '2')
#print a

def compute_str(mystr):
    print "str is: " , mystr
    tmp_list = []
    if len(mystr) < 4:
        return oper_two(mystr[0], mystr[1], mystr[2])
    if mystr[1] == '*' or mystr[1] == '/':
        for i in mystr[0:3]:
            tmp_list.append(i)
        for i in mystr[3:]:
            tmp_list.append(i)
        return compute_str(tmp_list)
    elif mystr[3] == '*' or mystr[3] == '/':
        for i in mystr[0:2]:
            tmp_list.append(i)
        tmp_list.append(compute_str(mystr[2:5]))
        for i in mystr[5:]:
            tmp_list.append(i)
        return compute_str(tmp_list)
    else:
        tmp_list.append(compute_str(mystr[:3]))
        for i in mystr[3:]:
            tmp_list.append(i)
        return compute_str(tmp_list)


def compute_allstr(allstr):
    print "allstr is:" , allstr
    tmp_list = []
    count = 0
    first = -1
    last = -1
    for i in allstr[:]:
        count += 1
        if i == '(':
            first = count
    count = 0
    for i in allstr[-1::-1]:
        count += 1
        if i == ')':
            last = count
            
    print first , 'and', last
    if first != -1 and last != -1:
        for i in allstr[:first-1]:
            tmp_list.append(i)
        tmp_list.append(compute_str(allstr[first:-last]))
        for i in allstr[-last+1:]:
            tmp_list.append(i)
        return compute_allstr(tmp_list)
    else:
        return compute_str(allstr)
    

#arith_str = "1+2*3-7/2"
#result = compute_str(arith_str)


#arith_str = "1+2*(7-3)/2"
#result = compute_allstr(arith_str)

def str2list(mystr):
    mylist = []
    one_num = 0
    tmp_i = ''
    for i in mystr:
        if i.isdigit():
            one_num = one_num * 10 + int(i)
        elif i == '(':
            mylist.append(i)
        elif i == ')':
            mylist.append(one_num)
            one_num = 0
            mylist.append(i)
        else:
            if tmp_i != ')':
                mylist.append(one_num)
                one_num = 0
            mylist.append(i)
        tmp_i = i
        
    mylist.append(one_num)
    return mylist
#
#arith_str = "1+2*17-3/2"
arith_str = "1+2*(17-3)/2"

arith_list = str2list(arith_str)
print arith_list
result = compute_allstr(arith_list)

print result

 

 输出:

[1, '+', 2, '*', '(', 17, '-', 3, ')', '/', 2]
allstr is: [1, '+', 2, '*', '(', 17, '-', 3, ')', '/', 2]
5 and 3
str is:  [17, '-', 3]
allstr is: [1, '+', 2, '*', '14', '/', 2]
-1 and -1
str is:  [1, '+', 2, '*', '14', '/', 2]
str is:  [2, '*', '14']
str is:  [1, '+', '28', '/', 2]
str is:  ['28', '/', 2]
str is:  [1, '+', '14']
15

 

使用栈: