• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小路子我爱你
博客园    首页    新随笔    联系   管理    订阅  订阅

团队科学计算器-模块开发过程

  • 项目托管平台地址:https://gitee.com/Marly/codes/vbnfqyx3mzpsk8062rc9e77
    fun功能,实现了不含括号的表达式计算 实现的过程:

    def fun(s):
    l = re.findall('([\d\.]+|/|-|\+|\*)',s)
    sum=0
    while 1:
    if '*' in l and '/' not in l:
    md(l, '*')
    elif '*' not in l and '/' in l:
    md(l, '/')
    elif '*' in l and '/' in l:
    a = l.index('*')
    b = l.index('/')
    if a < b:
    md(l, '*')
    else:
    md(l, '/')
    else:
    if l[0]=='-':
    l[0]=l[0]+l[1]
    del l[1]
    sum += float(l[0])
    for i in range(1, len(l), 2):
    if l[i] == '+' and l[i + 1] != '-':
    sum += float(l[i + 1])
    elif l[i] == '+' and l[i + 1] == '-':
    sum -= float(l[i + 2])
    elif l[i] == '-' and l[i + 1] == '-':
    sum += float(l[i + 2])
    elif l[i] == '-' and l[i + 1] != '-':
    sum -= float(l[i + 1])
    break
    return sum

    calculate功能,:实现了带有括号的递归运算。实现过程是
  • def calculate(expression):
    ex=[]
    ans=0
    if '(' not in expression:
    ans=fun(expression)
    return ans
    for i in range(len(expression)):
    if expression[i]=='(':
    ex.append(i)
    elif expression[i]==')':
    temp=0
    sub=expression[ex[len(ex)-1]+1:i]
    temp=fun(sub)
    expression=expression[0:ex[len(ex)-1]]+str(temp)+expression[i+1:len(expression)+1]
    ex.pop()

    
    
posted @ 2017-10-26 18:51  菇--凉  阅读(230)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3