• python 计算器


    计算器 等式

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @File  : calculators.py
    # @Author: Anthony.waa
    # @Date  : 2018/8/13 0013
    # @Desc  :
    
    example = '- 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 *568/14 )) - (-4*3)/ (16-3*2) )'
    
    
    import re
    
    bracket = re.compile(r'([^()]+)') # 找括号
    multiplys = re.compile(r'(d+.?d**-d+.?d*)|(d+.?d**d+.?d*)')   # 找乘法
    divides = re.compile(r'(d+.?d*/-d+.?d*)|(d+.?d*/d+.?d*)')   # 找除法
    adds = re.compile(r'(-?d+.?d*+-d+.?d*)|(-?d+.?d*+d+.?d*)')    # 找加法
    subtracts = re.compile(r'(-?d+.?d*--d+.?d*)|(-?d+.?d*-d+.?d*)') # 找减法
    out_cal = re.compile(r'(?+?-?d+)?') # 验证等式是否计算完成
    out_strip = re.compile(r'[^(].*[^)]')   # 去除最后括号
    
    
    def Add(s):  # 加法运算
        result = re.split(r'+', adds.search(s).group())
        return s.replace(adds.search(s).group(), str(float(result[0]) + float(result[1])))
    
    
    def Subtract(s):  # 减法运算
        result = subtracts.search(s).group()
        if result.startswith('-'):
            result = result.replace('-', '+')
            res = Add(result).replace('+', '-')
        else:
            result = re.split(r'-', result)
            res = str(float(result[0]) - float(result[1]))
        return s.replace(subtracts.search(s).group(), res)
    
    
    def Multiply(s):  # 乘法运算
        result = re.split(r'*', multiplys.search(s).group())
        return s.replace(multiplys.search(s).group(), str(float(result[0]) * float(result[1])))
    
    
    def Divide(s):  # 除法运算
        result = re.split(r'/', divides.search(s).group())
        return s.replace(divides.search(s).group(), str(float(result[0]) / float(result[1])))
    
    
    def calculatoring():  # 开始计算
        while True:
            print('-'*90)
            print('提示: 输入(q/exit/n),任意一个即可退出运算')
            print('例如: - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 *568/14 )) - (-4*3)/ (16-3*2) )')
            beginning = input('请输入一个符合加减乘除法运算的等式:').strip()
            if beginning.lower() == 'q' or beginning.lower() == 'exit' or beginning.lower() == 'n':
                break
            else:
                beginning = ''.join([i for i in re.split('s+', beginning)])  # 去除等式中的空格
                if not beginning.startswith('('):
                    beginning = str('(%s)' % beginning)
                    while bracket.search(beginning):  # 循环计算括号中的等式
                        beginning = beginning.replace('--', '+')
                        beginning_search = bracket.search(beginning).group()  # 正式开始等式计算   beginning.....
                        # 计算顺序 除法--> 乘法 --> 减法 --> 加法
                        if divides.search(beginning_search):  # 除法
                            beginning = beginning.replace(beginning_search, Divide(beginning_search))
                        elif multiplys.search(beginning_search):  # 乘法
                            beginning = beginning.replace(beginning_search, Multiply(beginning_search))
                        elif subtracts.search(beginning_search):    # 减法
                            beginning = beginning.replace(beginning_search,Subtract(beginning_search))
                        elif adds.search(beginning_search): # 加法
                            beginning = beginning.replace(beginning_search,Add(beginning_search))
                        elif out_cal.search(beginning_search):  # 去除最后的括号 查看等式运算是否计算完毕
                            beginning = beginning.replace(beginning_search,out_strip.search(beginning_search).group())
                    print('该等式计算结果约为:%.2f'%float(beginning))
                    print()
    
    
    if __name__ == '__main__':
        calculatoring()
    

      

  • 相关阅读:
    SQL基础学习_03_数据更新
    SQL基础学习_02_查询
    SQL基础学习_01_数据库和表
    HCA数据下载
    Multiclonal Invasion in Breast Tumors Identified by Topographic Single Cell Sequencing
    gg_pie
    ggnetwork
    ggplot2画简单的heatmap
    简单R语言爬虫
    突变数据清洗
  • 原文地址:https://www.cnblogs.com/ipyanthony/p/9487850.html
Copyright © 2020-2023  润新知