文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

简单的计算功能,还需要优化

2023-01-31 00:32

关注
#还有需要优化的
#!/usr/bin/env python3 Example = '1-2*((60-30+(9-2*5/-3+7/3*99/4*2998+10*568/14)*(-40/5))-(-4*3)/(16-3*2))' import re # 乘法函数 def multiplication(): value = 0 for n, i in enumerate(new_value): if n != 0: fa = '%.2f'%(float(new_value[n-1])) fb = '%.2f'%(float(new_value[n])) value = '%.2f'%(float(fa) * float(fb)) return value # 除法函数 def division(): value = 0 for n, i in enumerate(new_value): if n != 0: fa = '%.2f'%(float(new_value[n - 1])) fb = '%.2f'%(float(new_value[n])) value = '%.2f'%(float(fa) / float(fb)) return value # 加法函数 def addition(): value =0 for n, i in enumerate(new_value): if n != 0: fa = '%.2f'%(float(new_value[n - 1])) fb = '%.2f'%(float(new_value[n])) value = '%.2f'%(float(fa) + float(fb)) return value # 减法函数 def subtraction(): value = 0 for n, i in enumerate(new_value): if n != 0: fa = '%.2f'%(float(new_value[n - 1])) fb = '%.2f'%(float(new_value[n])) value = '%.2f'%(float(fa) - float(fb)) return valuewhile True: brackets = re.search("\([^()]*\)",Example) #匹配括号内容,'\('中\表示转义,serach函数匹配所有字符串 if brackets: take_off_brackets = brackets.group().strip('()')#脱掉括号 while True: element = re.findall('-?\d+\.\d+|\d+', take_off_brackets) if len(element) > 1: #计算take_off_brackets中的元素数量是否大于1 print(take_off_brackets) if '*' in take_off_brackets or '/' in take_off_brackets: #判断字符串中带有'*'或者'/' multiplication_and_division = re.search('(\d+\.\d+|\d+)(\*|\/)(-?\d+\.\d+|-?\d+)',take_off_brackets)#匹配字符串中的乘法和除法, # 其中'\d+\.\d+'表示匹配浮点,'\d+'表示整数,(\d+\.\d+|\d+)表示匹配浮点或者整数,括号表示这是一组,(\*|\/)表示匹配'*'或者'/', # (-?\d+\.\d+|-?\d+)中-?\d+\.\d+表示正浮点数或者负浮点数,-?\d+表示正数或者负数 if multiplication_and_division: mul_and_div = multiplication_and_division.group() if '*' in mul_and_div: new_value = mul_and_div.split('*') #将mul_and_div以*隔开生成列表 replace = multiplication() take_off_brackets = re.sub('(\d+\.\d+|\d+)\*(-?\d+\.\d+|-?\d+)',str(replace),take_off_brackets,1) # print(take_off_brackets) continue elif '/' in mul_and_div: new_value = mul_and_div.split('/') replace = division() take_off_brackets = re.sub('(\d+\.\d+|\d+)\/(-?\d+\.\d+|-?\d+)',str(replace),take_off_brackets,1) # print(take_off_brackets) continue else: break else: break elif '+' in take_off_brackets or '-' in take_off_brackets: addition_and_subtraction = re.search('(\d+\.\d+|\d+)(\+|\-)(-?\d+\.\d+|-?\d+)',take_off_brackets) if addition_and_subtraction: add_and_sub = addition_and_subtraction.group() if '+' in add_and_sub: if '++' in add_and_sub: new_value = add_and_sub.split('+', 1) replace = addition() take_off_brackets = re.sub('(\d+\.\d+|\d+)\+(-?\d+\.\d+|-?\d+)', str(replace), take_off_brackets, 1) # print(take_off_brackets) continue else: new_value = add_and_sub.split('+') replace = addition() take_off_brackets = re.sub('(\d+\.\d+|\d+)\+(-?\d+\.\d+|-?\d+)', str(replace), take_off_brackets, 1) # print(take_off_brackets) continue elif '-' in add_and_sub: if '--' in add_and_sub: new_value = add_and_sub.split('-', 1) replace = subtraction() take_off_brackets = re.sub('(\d+\.\d+|\d+)\-(-?\d+\.\d+|-?\d+)', str(replace), take_off_brackets, 1) # print(take_off_brackets) continue else: new_value = add_and_sub.split('-') replace = subtraction() take_off_brackets = re.sub('(\d+\.\d+|\d+)\-(-?\d+\.\d+|-?\d+)', str(replace), take_off_brackets, 1) # print(take_off_brackets) continue else: break else: break else: break else: # take_off_brackets = brackets.group().strip('()') aa = re.sub('\+','\+',brackets.group()) #将字符串中的'+'、'-'、'*'、'/'都加上转义符'/'这样替换的时候才能找到 bb = re.sub('\-','\-',aa) cc = re.sub('\*','\*',bb) dd = re.sub('\/','\/',cc) Example = re.sub(dd, str(take_off_brackets).strip('()'), Example, 1) brackets = re.search("\([^()]*\)", Example) tt = re.sub('\(','\(',brackets.group()) yy = re.sub('\)','\)',tt) Example = re.sub(yy,take_off_brackets,Example) print(Example) break else: while True: if '*' in Example or '/' in Example: multiplication_and_division = re.search('(\d+\.\d+|\d+)(\*|\/)(-?\d+\.\d+|-?\d+)', Example) if multiplication_and_division: mul_and_div = multiplication_and_division.group() if '*' in mul_and_div: new_value = mul_and_div.split('*') #将mul_and_div以*隔开生成列表 replace = multiplication() Example = re.sub('(\d+\.\d+|\d+)\*(-?\d+\.\d+|-?\d+)',str(replace),Example,1) print(Example) continue elif '/' in mul_and_div: new_value = mul_and_div.split('/') replace = division() Example = re.sub('(\d+\.\d+|\d+)\/(-?\d+\.\d+|-?\d+)',str(replace),Example,1) print(Example) continue else: break else: break elif '+' in Example or '-' in Example: addition_and_subtraction = re.search('(\d+\.\d+|\d+)(\+|\-)(-?\d+\.\d+|-?\d+)', Example) if addition_and_subtraction: add_and_sub = addition_and_subtraction.group() if '+' in add_and_sub: if '++' in add_and_sub: new_value = add_and_sub.split('+', 1) replace = addition() Example = re.sub('(\d+\.\d+|\d+)\+(-?\d+\.\d+|-?\d+)', str(replace), Example, 1) print(Example) continue else: new_value = add_and_sub.split('+') replace = addition() Example = re.sub('(\d+\.\d+|\d+)\+(-?\d+\.\d+|-?\d+)', str(replace), Example, 1) print(Example) continue elif '-' in add_and_sub: if '--' in add_and_sub: new_value = add_and_sub.split('-', 1) replace = subtraction() Example = re.sub('(\d+\.\d+|\d+)\-(-?\d+\.\d+|-?\d+)', str(replace), Example, 1) print(Example) continue else: new_value = add_and_sub.split('-') replace = subtraction() Example = re.sub('(\d+\.\d+|\d+)\-(-?\d+\.\d+|-?\d+)', str(replace), Example, 1) print(Example) continue else: break else: break

 

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯