计算器开发过程

s='1 - 2 * ( (60-30 + (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'

1、查找最优先的圆括号:re.search(r'\([^()]+\)', s)

2、乘除运算:

(1)、先用正则表达式查找最左边的乘法或除法,并提取其中的数字和符号:

re.search(r'(?P<first_num>[0-9]+) *(?P<symbol>[\*/]) *(?P<second_num>[0-9]+)', res).groupdict()

posted on 2019-04-11 21:53  Treelight  阅读(268)  评论(0编辑  收藏  举报