python题库刷题训练软件_刷题 -- python计算器练习题

假设python只能简单处理+-/,不能处理括号。练习处理。练习正则。

网上有些无法很好处理负号,如下情况。暂未处理括号数字间缺少等情况。

(-1+(2-5(-1))(2-5))

-1+(2-5)*(2-5)

#/usr/bin/env python3

#mail infaaf@126.com

import re,sys

symbos_map={'+-':'-','++':'+','-+':'-','--':'+'}

# -1-(-1)*-1 =-2 #过程 -1--1*-1 => -1-- -1 => -1+-1 => -1-1 => -2

# 找最内括号,数字后面无符号,去除括号

# 找最内括号,有表示式,先乘除,乘除 从数字开始匹配, 1*-1 1*1

# 乘除完成,从左到右,带符号匹配。 -1+-1 不等于 -(1-1) ,需要处理-1-------1 情况(由于乘除时未处理符号)

def calc_element_3(v1,v2,symbol):

print("计算: %s,%s,%s"%(v1,v2,symbol))

'''带符号 + - * / '''

v1,v2=float(v1),float(v2)

if symbol=='+':return v1+v2

elif symbol =='-':return v1-v2

elif symbol == '*':return v1*v2

elif symbol =='/':return v1/v2

else:print(symbol);sys.exit()

def multi_divi(s):

''' s括号内表达式,用于处理乘除。找到1*-2,处理为-2 ,处理1次 '''

print("处理乘除: %s"%s)

re_seach_obj=re.search(r'([0-9.]+)([*/])([+-])?([0-9.]+)',s)

if re_seach_obj is not None:

s_match_str = re_seach_obj.group(0) # 1*-1

value1=re_seach_obj.group(1)

value2=re_seach_obj.group(4)

simblos=re_seach_obj.group(2)

simblo_ext=re_seach_obj.group(3)

ret=calc_element_3(value1,value2,simblos)

ret=simblo_ext+str(ret)

print(s_match_str,ret)

s=s.replace(s_match_str,ret)

return s

# res=multi_divi('-1-2*-2') # print(res)

def add_minu(s):

print("处理加减: %s"%s)

''' -1--1,1--1,-1+1,-1---1,-1---------1,用于从左往右处理加减,处理1次'''

if re.search(r'[*/]', s):

print("should do * / before + -: %s"%s)

sys.exit()

while re.search(r'[+\-*\\]{2,}',s): #-1-1 ,1+++++1 => -1-1 , 1+1

for symbos_key in symbos_map:

s=s.replace(symbos_key,symbos_map[symbos_key])

# print(s)

re_seach_obj = re.search(r'([+-]?[0-9.]+)([+-])([0-9.]+)', s)

if re_seach_obj:

s_match_str = re_seach_obj.group(0) # 1*-1

value1=re_seach_obj.group(1)

value2=re_seach_obj.group(3)

simblos=re_seach_obj.group(2)

ret=calc_element_3(value1,value2,simblos)

# print(s_match_str,ret)

s=s.replace(s_match_str,str(ret))

# print(s)

return s

# res=add_minu('1.0+1.5++++1')

def handler_expression(expression):

print("进入表达式处理%s"%expression)

while re.search('[*/]',expression):

expression=multi_divi(expression)

while re.search('[0-9.]+[+-]+[0-9.]+',expression):

expression=add_minu(expression)

return expression

# res=handler_expression('-1+---5*-2/-1++2+2+2') # print(res)

# a=handler_expression('1+2--5.0*-3.0')

# print(a)

def hadler_braces(s):

print(s)

flag=True

while flag:

re_obj=re.search('\([+\-*/0-9.]*\)',s)

if re_obj:

s_match_str=re_obj.group(0)

print("括号匹配: %s"%s_match_str)

if re.match('\([+\-]?([0-9.]*)\)',s_match_str):

print("仅剩余单个值: %s"%s_match_str)

s_match_str_match=re.match('\(([+\-]?[0-9.]*)\)',s_match_str).group(1)

s = s.replace(s_match_str, s_match_str_match)

print(s)

else:

print("调用处理%s"%s_match_str)

s_match_str_str=re.search('\(([+\-*/0-9.]*)\)',s).group(1)

ret=handler_expression(s_match_str_str)

s = s.replace(s_match_str, str(ret))

print(s)

else:

flag=False

return s

# no_braces_result=hadler_braces('(-1+(2-5*(-1))*(2-5))')

# result=handler_expression(no_braces_result)

# print(result)

if __name__ == '__main__':

while True:

exp=input("输入表达式: ")

exp=re.sub('\s','',exp)

no_braces_result=hadler_braces(str(exp))

result=handler_expression(no_braces_result)

print(result)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值