Python之字符串计算(计算器)

Python之字符串计算(计算器)

import re

expression = '-1-2*((60+2*(-3-40.0+42425/5)*(9-2*5/3+357/553/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))+56+(56-45)'
question = eval(expression)
print(question)


def arithmetic(expression='1+1'):
    #     content = re.search('\(([\-\+\*\/]*\d+\.?\d*)+\)', expression)  # (-3-40.0/5)
    content = re.search('\(([-+*/]*\d+\.?\d*)+\)', expression)  # (-3-40.0/5)
    print(111, content)
    if content:
        content = content.group()
        content = content[1:-1]
        print('content:', content)
        replace_content = next_arithmetic(content)
        expression = re.sub('\(([-+*/]*\d+\.?\d*)+\)', replace_content, expression, count=1)
        print('next_expression:', expression)
    else:
        answer = next_arithmetic(expression)
        return answer
    return arithmetic(expression)


def next_arithmetic(content):
    while True:
        next_content_mul_div = re.search('\d+\.?\d*[*/][-+]?\d+\.?\d*', content)  # 找出带有*/的式子
        if next_content_mul_div:  # 如果content含有带有*/的式子
            next_content_mul_div = next_content_mul_div.group()
            print('next_content_mul_div:', next_content_mul_div)
            mul_div_content = mul_div(next_content_mul_div)  # 计算出带有*/的式子
            print('mul_div_content:', mul_div_content)
            content = re.sub('\d+\.?\d*[*/][-+]?\d+\.?\d*', str(mul_div_content), content, count=1)  # 把带有*/的式子计算出来后替换掉
            print('content:', content)
            continue
        next_content_add_sub = re.search('-?\d+\.?\d*[-+][-+]?\d+\.?\d*', content)  # 找出带有-+的式子
        if next_content_add_sub:  # 如果content含有带有+-的式子
            next_content_add_sub = next_content_add_sub.group()
            print('next_content_add_sub:', next_content_add_sub)
            add_sub_content = add_sub(next_content_add_sub)  # 计算出带有-+的式子
            print('add_sub_content:', add_sub_content)
            add_sub_content = str(add_sub_content)
            content = re.sub('-?\d+\.?\d*[-+]-?\d+\.?\d*', str(add_sub_content), content, count=1)  # 把带有-+的式子计算出来后替换掉
            print('content:', content)
            continue
        else:
            break
    return content


def add_sub(content):
    if '+' in content:
        content = content.split('+')
        print(content)
        content = float(content[0]) + float(content[1])
        return content
    elif '-' in content:
        content = content.split('-')
        # 减法情况有多种
        if content[0] == '-' and content[2] == '-':
            # content = content.split('-')
            print(content)
            content = -float(content[1]) - float(content[-1])
            return content
        if content[0] == '-':
            # content = content.split('-')
            print(content)
            content = -float(content[1]) - float(content[-1])
            return content
        if content[1] == '-' and content[2] == '-':
            # content = content.split('-')
            print(content)
            content = -float(content[0]) + float(content[-1])
            return content
        if content[1] == '':
            # content = content.split('-')
            print(content)
            content = float(content[0]) - float(content[2])
            return content
        if content[0] == '' and content[2] != '':
            print(content)
            content = -float(content[1]) - float(content[2])
            return content
        if content[0] == '' and content[2] == '':
            print(content)
            content = -float(content[1]) + float(content[3])
            return content
        else:
            # content = content.split('-')
            print(content)
            content = float(content[0]) - float(content[1])
            return content


def mul_div(content):
    if '*' in content:
        content = content.split('*')
        print(content)
        content = float(content[0]) * float(content[1])
        return content
    elif '/' in content:
        content = content.split('/')
        print(content)
        content = float(content[0]) / float(content[1])
        return content


# expression = '1-2*((60+2*(-3-40.0/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))'
expression = '-1-2*((60+2*(-3-40.0+42425/5)*(9-2*5/3+357/553/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))+56+(56-45)'
answer = arithmetic(expression)
print(answer)
posted @ 2019-05-07 15:34  B站-水论文的程序猿  阅读(1390)  评论(0编辑  收藏  举报