zoukankan      html  css  js  c++  java
  • 计算器加减乘除(去括号待完善)

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-

    import re


    def cheng(suanshi):
    try:
    while 1:
    x = re.search(r'-?d+(.d+)?[*/]d+(.d+)?', suanshi)
    a, b, c = re.split(r'([*/])', x.group())
    a = float(a)
    c = float(c)
    if b == '*':
    res = a * c
    elif b == '/':
    res = float(a / c)
    suanshi = suanshi.replace(x.group(), str(res), 1)
    except Exception:
    obj = re.search('([^()]+)', suanshi)
    print(obj)
    if not obj.group():
    return suanshi[1:-1]
    return suanshi


    def jia(suanshi):
    try:
    while 1:
    x = re.search(r'-?d+(.d+)?[-+]d+(.d+)?', suanshi)
    lst = re.split(r'([-+])', x.group())
    if len(lst) == 5:
    q, w, a, b, c = lst
    if b == '+':
    res = - float(a) + float(c)
    elif b == '-':
    res = -(float(a) + float(c))
    elif len(lst) == 3:
    a, b, c = lst
    if b == '+':
    res = float(a) + float(c)
    elif b == '-':
    res = float(a) - float(c)
    suanshi = suanshi.replace(x.group(), str(res), 1)
    print(suanshi)
    except Exception:
    return suanshi


    # user = input('请输入算式:')
    user = '1-2*((60-30+(-40/5)*(9-2*5/4+7/2+10*14/7))-(-4*3)/(16-3*2))'
    while 1:
    lst = re.findall('([^()]+)', user)
    print(lst)
    if not lst:
    res = jia(cheng(user))[1:-1]
    user = user.replace('--', '+')
    user = user.replace('+-', '-')
    continue
    for i in lst:
    user = user.replace(i, cheng(i), 1)
    print(user)
    lst2 = re.findall('([^()]+)', user)
    for j in lst2:
    user = user.replace(j, jia(j), 1)
    print(user)
  • 相关阅读:
    图片圆角
    ios三张图片组合一张
    使用atomic一定是线程安全的吗?
    webservice异常
    Spring AOP中pointcut expression表达式解析 及匹配多个条件
    spring和mybatis的整合配置
    spring4配置文件详解
    MyBatis的接口式编程Demo
    关于中国电信天翼开放平台短信接口的一些使用
    HttpInvoker http请求工具类
  • 原文地址:https://www.cnblogs.com/zjx1/p/10828176.html
Copyright © 2011-2022 走看看