计算器<代码>

  1 import re
  2 l_no = "-4.0*-4+((-1-8.0*2*-1)-(-9.456/1.57))/8+-8*7"
  3 true_tr = "-4.0*-4+((-1-8.0*2*-1)-(-9.456/1.57))/8+-8*7"
  4 trunswer = eval(true_tr)
  5 print(trunswer)
  6 l = re.sub(" +","",l_no)  # 替换 没有+号也能实现  有第四个元素 为替换几次
  7 l_list = list(l)          # 还有一个subn方法 会返回一个替换几次的数字
  8 def check_w(formula):                     #  check函数
  9     formula=re.sub("--","+",formula)
 10     formula=re.sub("-\+|\+-","+",formula)    # 管道符号|的意思是or 反斜杠\ 消除特殊意义
 11     a = re.findall("[a-z]|[A-Z]",formula)    # 中括号会消除特殊字符 并且都是or关系
 12     b = re.findall("\(\)",formula)        # 找空括号
 13     if a or b:
 14         print("存在字母空括号")
 15         exit()
 16     return formula
 17 l_no = check_w(l_no)
 18 s = l_no
 19 def left_parenthesis(formula):         # find the left parenthesis  --
 20     a = re.search("\(", formula)
 21     if a == None:                      # 有一种个简单方法“\([^()]+\)” 中括号消除特殊意思
 22         return None                    # 但是除了\ ^ -三个 “\(([^()]+)\)”会匹配括号内容
 23     b = a.span()                       # 因为findall是优先匹配括号内容
 24     return b[1]
 25 def right_parenthesis(formula):          # find the right parenthesis --
 26     a = re.search("\)", formula)
 27     if a == None:
 28         return None
 29     b = a.span()
 30     return b[1]
 31 def code_muldiv(numcc):                # function for multiplication and division
 32     result = []                        # 这是一种算法 所有factor制成列表 所有mathematical symbol
 33     for i in numcc:                    # (-+或者*/)制成一个列表 之后利用列表有顺序来进行计算
 34         jishufuhao = 0                 # 正好适合8*-8这种形式
 35         jishushu = 1
 36         fuhao2 = []
 37         num = []
 38         fuhao2 = re.findall("\*|/",i)
 39         num = re.split("[*/]",i)             # 将所有因数(除数)整理成一个列表
 40         flag2 =True
 41         while flag2:
 42             if fuhao2[jishufuhao] == "*":
 43                 ga = float(num[jishushu-1]) * float(num[jishushu])
 44                 num[jishushu] = ga
 45                 jishushu+=1
 46                 jishufuhao+=1
 47             elif fuhao2[jishufuhao] == "/":
 48                 ha = float(num[jishushu - 1]) / float(num[jishushu])
 49                 num[jishushu] = ha
 50                 jishushu += 1
 51                 jishufuhao += 1
 52             if jishufuhao+1 > len(fuhao2):
 53                 flag2 = False
 54         result.append(num[jishushu-1])
 55     return result
 56 def code_addsub(numcc):       # function for addition and subtraction
 57     result = []
 58     for i in numcc:
 59         jishufuhao = 0
 60         jishushu = 1
 61         fuhao2 = []
 62         num = []
 63         i = re.sub("--","+",i)
 64         i = re.sub("\+-|-\+","-",i)
 65         i = re.sub("^\+","",i)
 66         a = re.findall("^-\d+\.?\d*",i)    ### 如果formula为 -9+8 ,这个函数的功能
 67         if a == []:                        ### 就是将其转换为  0+8-9 这样便将minus
 68             pass                           ### 转化为无符号式子
 69         else:                              ###
 70             i = re.sub(a[0],"0",i,1)       ###
 71             i = "".join([i,a[0]])          ###
 72         fuhao2 = re.findall("\+|-",i)
 73         num = re.split("[+-]",i)
 74         if fuhao2 ==[]:
 75             return numcc
 76         flag2 = True
 77         while flag2:
 78             if fuhao2[jishufuhao] == "+":
 79                 ga = float(num[jishushu-1]) + float(num[jishushu])
 80                 num[jishushu] = ga
 81                 jishushu+=1
 82                 jishufuhao+=1
 83             elif fuhao2[jishufuhao] == "-":
 84                 ha = float(num[jishushu - 1]) - float(num[jishushu])
 85                 num[jishushu] = ha
 86                 jishushu += 1
 87                 jishufuhao += 1
 88             if jishufuhao+1 > len(fuhao2):
 89                 flag2 = False
 90         result.append(num[jishushu-1])
 91     return result
 92 def exchange_l(formula):           # for calculating mul div
 93     flag4 = True
 94     while flag4:
 95         need_md = re.findall("\d+\.?\d*[*/]-?\d+\.?\d*",formula)
 96         if need_md == []:  # 上边这个正则 是匹配乘除并且两边是3.9这样的数字
 97             break
 98         e = need_md
 99         k = code_muldiv(e)
100         out_s = formula.replace(str(need_md[0]),str(k[0]))
101         formula =out_s
102     return formula
103 flag2 = True
104 while flag2:
105     flag = True
106     a = 0
107     y_y=right_parenthesis(s)
108     if y_y == None:                       # 判断 没有括号时,直接计算
109         formula_jj = exchange_l(l)
110         final_result = code_addsub([formula_jj])
111         l = l.replace(l, str(final_result[0]), 1)
112         s = l
113         print("result:",s)
114         break
115     else:
116         y = right_parenthesis(s) - 1
117     while flag:                              # 多次寻找左括号 并且切片后再寻找
118         position_psis = left_parenthesis(s)
119         if position_psis == None:
120             position_psis = 0
121             break
122         s= s[position_psis:]             # 将找到的左括号后面的字符串切片
123         a += position_psis               # 记录下序列号
124         if a > y:                        # 若是左括号序列号比右括号大 则停止寻找
125             flag = False
126     a = a - position_psis
127     need_l = l[a:y]                      # 需要处理的括号内容
128     ex_input = l[a - 1:y + 1]
129     need_l =str(need_l)   # 3+2-3
130     formula_jj = exchange_l(need_l)      # 进行乘除处理
131     final_result = code_addsub([formula_jj]) # 进行加减处理
132     l = l.replace(ex_input, str(final_result[0]), 1) # 用结果替换掉字符串
133     s = l                                            # 赋值再次循环用

 

 
import re
l_no = "-4.0*-4+((-1-8.0*2*-1)-(-9.456/1.57))/8+-8*7"
true_tr = "-4.0*-4+((-1-8.0*2*-1)-(-9.456/1.57))/8+-8*7"
trunswer = eval(true_tr)
print(trunswer)
l = re.sub(" +","",l_no) # 替换 没有+号也能实现 有第四个元素 为替换几次
l_list = list(l) # 还有一个subn方法 会返回一个替换几次的数字
def check_w(formula): # check函数
formula=re.sub("--","+",formula)
formula=re.sub("-\+|\+-","+",formula) # 管道符号|的意思是or 反斜杠\ 消除特殊意义
a = re.findall("[a-z]|[A-Z]",formula) # 中括号会消除特殊字符 并且都是or关系
b = re.findall("\(\)",formula) # 找空括号
if a or b:
print("存在字母空括号")
exit()
return formula
l_no = check_w(l_no)
s = l_no
def left_parenthesis(formula): # find the left parenthesis --
a = re.search("\(", formula)
if a == None: # 有一种个简单方法“\([^()]+\)” 中括号消除特殊意思
return None # 但是除了\ ^ -三个 “\([^()]+\)”会匹配括号内容
b = a.span() # 因为findall是优先匹配括号内容
return b[1]
def right_parenthesis(formula): # find the right parenthesis --
a = re.search("\)", formula)
if a == None:
return None
b = a.span()
return b[1]
def code_muldiv(numcc): # function for multiplication and division
result = [] # 这是一种算法 所有factor制成列表 所有mathematical symbol
for i in numcc: # -+或者*/)制成一个列表 之后利用列表有顺序来进行计算
jishufuhao = 0 # 正好适合8*-8这种形式
jishushu = 1
fuhao2 = []
num = []
fuhao2 = re.findall("\*|/",i)
num = re.split("[*/]",i) # 将所有因数(除数)整理成一个列表
flag2 =True
while flag2:
if fuhao2[jishufuhao] == "*":
ga = float(num[jishushu-1]) * float(num[jishushu])
num[jishushu] = ga
jishushu+=1
jishufuhao+=1
elif fuhao2[jishufuhao] == "/":
ha = float(num[jishushu - 1]) / float(num[jishushu])
num[jishushu] = ha
jishushu += 1
jishufuhao += 1
if jishufuhao+1 > len(fuhao2):
flag2 = False
result.append(num[jishushu-1])
return result
def code_addsub(numcc): # function for addition and subtraction
result = []
for i in numcc:
jishufuhao = 0
jishushu = 1
fuhao2 = []
num = []
i = re.sub("--","+",i)
i = re.sub("\+-|-\+","-",i)
i = re.sub("^\+","",i)
a = re.findall("^-\d+\.?\d*",i) ### 如果formula -9+8 ,这个函数的功能
if a == []: ### 就是将其转换为 0+8-9 这样便将minus
pass ### 转化为无符号式子
else: ###
i = re.sub(a[0],"0",i,1) ###
i = "".join([i,a[0]]) ###
fuhao2 = re.findall("\+|-",i)
num = re.split("[+-]",i)
if fuhao2 ==[]:
return numcc
flag2 = True
while flag2:
if fuhao2[jishufuhao] == "+":
ga = float(num[jishushu-1]) + float(num[jishushu])
num[jishushu] = ga
jishushu+=1
jishufuhao+=1
elif fuhao2[jishufuhao] == "-":
ha = float(num[jishushu - 1]) - float(num[jishushu])
num[jishushu] = ha
jishushu += 1
jishufuhao += 1
if jishufuhao+1 > len(fuhao2):
flag2 = False
result.append(num[jishushu-1])
return result
def exchange_l(formula): # for calculating mul div
flag4 = True
while flag4:
need_md = re.findall("\d+\.?\d*[*/]-?\d+\.?\d*",formula)
if need_md == []: # 上边这个正则 是匹配乘除并且两边是3.9这样的数字
break
e = need_md
k = code_muldiv(e)
out_s = formula.replace(str(need_md[0]),str(k[0]))
formula =out_s
return formula
flag2 = True
while flag2:
flag = True
a = 0
y_y=right_parenthesis(s)
if y_y == None: # 判断 没有括号时,直接计算
formula_jj = exchange_l(l)
final_result = code_addsub([formula_jj])
l = l.replace(l, str(final_result[0]), 1)
s = l
print("result:",s)
break
else:
y = right_parenthesis(s) - 1
while flag: # 多次寻找左括号 并且切片后再寻找
position_psis = left_parenthesis(s)
if position_psis == None:
position_psis = 0
break
s= s[position_psis:] # 将找到的左括号后面的字符串切片
a += position_psis # 记录下序列号
if a > y: # 若是左括号序列号比右括号大 则停止寻找
flag = False
a = a - position_psis
need_l = l[a:y] # 需要处理的括号内容
ex_input = l[a - 1:y + 1]
need_l =str(need_l) # 3+2-3
formula_jj = exchange_l(need_l) # 进行乘除处理
final_result = code_addsub([formula_jj]) # 进行加减处理
l = l.replace(ex_input, str(final_result[0]), 1) # 用结果替换掉字符串
s = l # 赋值再次循环用
posted @ 2016-09-14 16:11  gege4105  Views(204)  Comments(0Edit  收藏  举报