#!/usr/bin/env python
# -*- coding:utf-8 -*-
# ------------------------------------------------------------
# ******************** day23_5_Calculator_使用正则表达式计算复杂表达式 *******************
# ******************** day23_5_Calculator_使用正则表达式计算复杂表达式 *******************
# =====>>>>>>内容概览
# =====>>>>>>内容概览
# ------------------------------------------------------------
# # 1、逐个测试
# # # 测试内容#a = "1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4-3)/ (16-3*2) )"
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 2、完成对等式的基本计算
# # # 测试内容#a = "1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4-3)/ (16-3*2) )"
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 3、实现交互式等式计算
# # # 测试内容 a = "1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4-3)/ (16-3*2) )"
# ------------------------------------------------------------
# ------------------------------------------------分割线-------------------------------------------------
# ------------------------------------------------分割线-------------------------------------------------
# ------------------------------------------------分割线-------------------------------------------------
""" # ------------------------------------------------------------ # # 1、逐个测试 # # # 测试内容#a = "1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4-3)/ (16-3*2) )" # ------------------------------------------------------------ """ # =====>>>>>>内容概览 # =====>>>>>>内容概览 # import re # # a = "1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4-3)/ (16-3*2) )" # # print(eval(a)) # # a = a.replace(" ","") # # # 取出内容 # re_tmp = re.search("([^()]+)", a) # tmp = eval(re_tmp.group()) # a = a.replace(re_tmp.group(),str(tmp)) # # # # # # +-连续处理 # # # re_tmp = re.search("(+-)", a) # # # a = a.replace(re_tmp.group(),"-") # # # a = 1-2*((60-30-8.0*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4-3)/(16-3*2)) # re_tmp = re.search("([^()]+)", a) # tmp = eval(re_tmp.group()) # a = a.replace(re_tmp.group(),str(tmp)) # # # a = 1-2*((60-30-8.0*173545.88095238098)-(-4-3)/(16-3*2)) # re_tmp = re.search("([^()]+)", a) # tmp = eval(re_tmp.group()) # a = a.replace(re_tmp.group(),str(tmp)) # # # a = 1-2*(-1388337.0476190478-(-4-3)/(16-3*2)) # re_tmp = re.search("([^()]+)", a) # tmp = eval(re_tmp.group()) # a = a.replace(re_tmp.group(),str(tmp)) # # # a = 1-2*(-1388337.0476190478--7/(16-3*2)) # re_tmp = re.search("([^()]+)", a) # tmp = eval(re_tmp.group()) # a = a.replace(re_tmp.group(),str(tmp)) # # # a = 1-2*(-1388337.0476190478--7/10) # re_tmp = re.search("([^()]+)", a) # tmp = eval(re_tmp.group()) # a = a.replace(re_tmp.group(),str(tmp)) # # # a = 1-2*-1388336.3476190479 # re_tmp = re.search("([^()]+)", a) # # tmp = eval(re_tmp.group()) # # a = a.replace(re_tmp.group(),str(tmp)) # # print("re_tmp.group(): ",re_tmp) # # print("re_tmp.group(): ",re_tmp.group()) # # print(a_2.group()) # print("tmp: ",tmp) # print("a: ",a) """ # ------------------------------------------------------------ # # 2、完成对等式的基本计算 # # # 测试内容#a = "1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4-3)/ (16-3*2) )" # ------------------------------------------------------------ """ # # import re # # a = "1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4-3)/ (16-3*2) )" # a = a.replace(" ","") # while True: # a = a.replace(" ","") # # 取出内容 # re_tmp = re.search("([^()]+)", a) # # if not re_tmp : # if re_tmp == None: # a = eval(a) # break # tmp = eval(re_tmp.group()) # a = a.replace(re_tmp.group(),str(tmp)) # # print("re_tmp(): ",re_tmp) # print("tmp: ",tmp) # print("a: ",a) # # """ # ------------------------------------------------------------ # # 3、实现交互式等式计算 # # # 测试内容 a = "1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4-3)/ (16-3*2) )" # ------------------------------------------------------------ """ # # import re # flag_input = False # flag_continue = False # while True: # while not flag_input: # a = input("Please enter the expression to be evaluated:") # # print(re.search("[^0-9+-*/() ]", a)) # if re.search("[^0-90+-*/() ]", a) == None: # # print("输入正确") # flag_input = True # # 去空格 # a = a.replace(" ", "") # break # else: # print("输入错误") # continue # # # 取出内容 # re_tmp = re.search("([^()]+)", a) # # None说明是最后一次等式 # if re_tmp == None: # a = eval(a) # print("计算结果: ", a) # if input("If continue,please input continue; And input any world to exit!") =="continue": # flag_input = False # 开启输入标志 # continue # else: # break # tmp = eval(re_tmp.group()) # a = a.replace(re_tmp.group(),str(tmp)) #