Home>Article>Backend Development> Write a calculator method using python regular expressions

Write a calculator method using python regular expressions

高洛峰
高洛峰 Original
2017-03-22 10:14:58 2178browse

Write a calculator method using python regular expressions

#!/usr/bin/evnpython
import re
def deal_negative_issue(calc_list):
new_calc_list =[] #Define a new empty list, and then put the data into the new list after filtering is completed
forindex,item in enumerate(calc_list):
ifitem.strip().endswith("*") or item.strip().endswith("/"): #Select thestring ending with a multiplication sign or division sign
NEW_CALC_LIST.APEND ("%S-%S"%(CALC_LIST [Index], CALC_LIST [Index+1])) "*" or "/") in item: #Find the options with multiplication and division in the list. I tried this way of writing and it seems that it only matches *, but 40/5 alone cannot match
elif ("/ ") in item or ("*") in item:

print(item)new_calc_list.append(item)

print("new_calc_ist",new_calc_list)

returnnew_calc_list
def deal_unusual_issue(formula):
formula = formula.replace("++","+")
formula = formula.replace(" +-", "-")
formula = formula.replace("-+", "-")
formula = formula.replace("--", "+")
formula = formula .replace("- -", "+")
return formula

# def addition_and_subtraction(formula):
# sub_calc_list = re.split("[+-]",formula)
# sub_operator_list = re.findall("[+-]",formula)
# print(type(sub_calc_list), sub_operator_list)
# totle_res = None
# for index,i in enumerate(sub_calc_list ):
# ‐ ’ s ’ s ’s ’ s ’ s ’ s ’ s ’ s to t 1-- t t t t t t t to t o t t h e s t o f totle_res: #sub_res is true, indicating that this is not the first time
Loop# Determine whether it is plus or subtraction
# Totle_res+=
Float(i)
Else:TOTLE_RES-= Float (i)









## else:
# sub_res use with using out out out out through out off ‐ ‐ ‐ ‐ ‐ ‐ , use using using using through out out through out out through out through through off ‐ ‐ ‐ ‐ ‐ ‐ and ‐ to to calc_list = re.split("[+-]",formula) #Separate each multiplication and division operation by +- sign
calc_list = deal_negative_issue(calc_list)
print(calc_list)
for item in calc_list:
sub_calc_list = re.split("[*/]",item)
sub_operator_list = re.findall("[*/]",item) #Separate the multiplication and division signs in a list
print (sub_calc_list,sub_operator_list)
sub_res = None
for index,i in enumerate(sub_calc_list):### if sub_res: #sub_res is true, indicating that this is not the first cycle###if sub_operator_list[index-1]=="*": #Determine whether it is addition or subtraction based on the index in sub_operator_list,
sub_res /= float(i )
out ’ way way way ‐ ‐ ‐ ‐ and ‐ ‐ ‐ ‐ to ‐ ‐ ‐ ‐ res ‐ ‐ res ‐ ‐ ‐ res ‐ ‐ res ‐ ‐ res ‐ ‐ res ‐ ‐ ‐ )
formula = deal_unusual_issue(formula)
print(formula)

totle_calc_list = re.split("[+-]", formula)
totle_operator_list = re.findall("[+-]", formula) # Separate the multiplication and division signs in a list
print(totle_calc_list, totle_operator_list)
totle_res = None
for index, item in enumerate( totle_calc_list):
if totle_res: #sub_res is true, indicating that this is not the first cycle
if totle_operator_list[index-1]=="+": #Determine whether it is addition or subtraction through the index in sub_operator_list.
totle_res += float(item)
totle_res -= float(item)
else:
totle_res=float (item)
# return formula
print( "[result]=", totle_res)
return totle_res

def calc(formula):
parentheses_flag = True
calc_res = None

while
parentheses_flag:
m = re.search("\([^()]*\)",formula) #Find the bottom brackets, one by one
print(m)
if m:
#Print (type (m.group ()))
Subformula = m.group (). Strip ("()")#Dip out the found brackets
Subres = Calculating (Subformula)formula = formula.replace(m.group(),str(subres))else:
print("No brackets")
print("The final result is",calculating(formula))
parentheses_flag = False

if name == '
main
':
res = calc("1 - 2 * ( (60-30 +(9-2*5/ -3 + 7 /3*99/4*2998 +10 * 568/14 ) * (40/5)) - (4*3)/ (16-3*2) )")
#res = calc ("2 * (40/5)")

#Note: -40/5 function is not implemented

The above is the detailed content of Write a calculator method using python regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn