zoukankan      html  css  js  c++  java
  • calc

    import re
    def AP(formula):
      formula = formula.replace('+-','-')
      formula = formula.replace('-+','-')
      formula = formula.replace('--','+')
      return formula
    def compute_MD(formula):   ope = re.findall('[*/]',formula)   mun = re.split('[*/]',formula)   for index,i in enumerate(ope):     mun[index]=float(mun[index])   while ope!=[]:     if ope[index] == '*':       mun[index] *= float(mun[index+1])
    del mun[index+1],ope[index]   elif ope[index] == '/':   mun[index] /= float(mun[index+1])
    del mun[index+1],ope[index]   return mun[index]
    def compute_AP(f1,f2):   for index,i in enumerate(f1):     f2[index] = float(f2[index])   while len(f1) > 0:   if f1[index] == '-':     f2[index]-=float(f2[index+1])
        del f2[index+1],f1[index]   else:     f2[index] += float(f2[index + 1])     del f2[index +1], f1[index]   return f2[index]
    def compute(formula):   formula = AP(formula)   ope = re.findall('[+-]',formula)   mun = re.split('[+-]',formula)   for index,i in enumerate(mun):     if mun[index] == '':     mun[index+1] = '-' + mun[index + 1]   del mun[index],ope[index]   while mun[index].endswith('/') or mun[index].endswith('*'):     mun[index+1] = AP('-' + mun[index]+mun[index+1])     del mun[index],ope[index]   for index,i in enumerate(mun):     if i.find('*')>0 or i.find('/')>0 :       mun[index] = compute_MD(i)   if ope == []:     return mun[index]   return compute_AP(ope,mun)
    def calc(formula):   parenthesise_flag = True   while parenthesise_flag:     formula = formula.replace(' ','')     m = re.search('([^()]+)',formula)     if m:       m2 = str(compute(m.group().strip('()')))       formula = formula.replace(m.group(),m2)     else:       formula = compute(formula)       parenthesise_flag = False     return formula
    if __name__ == '__main__': res = calc(''' 公式内容 ''') print('结果:',res)

      

  • 相关阅读:
    【Quartz】1、Quartz使用说明
    【Servlet】1、Servlet监听器及相关接口
    【IDEA&&Eclipse】5、IntelliJ IDEA常见配置
    Chris Richardson微服务实战系列
    Traefik Kubernetes 初试
    用友iuap云运维平台支持基于K8s的微服务架构
    DCOS中监控和弹性伸缩方案经验
    使用微服务架构改造遗留系统
    kubernetes中port、target port、node port的对比分析,以及kube-proxy代理
    基于prometheus监控k8s集群
  • 原文地址:https://www.cnblogs.com/Leaders543/p/11724141.html
Copyright © 2011-2022 走看看