import re
s= input('input you gongshi:')
#s='-100*(333+(-22)/2*5.5+(5.5-1))-100'
s=s.replace(' ','') #
print(s)
#计算+-*/
def fa(a,b,c):
if c=='+':
rest=a+b
elif c=='-':
rest=a-b
elif c=='*':
rest=a*b
elif c=='/':
rest=a/b
return rest
def getNum(n):
print(n)
int1=re.findall('\d+[.]?[\d]*',n) #获取()中表达式
print(int1)
if int1.__len__()<2: #判断是否为负数
fnum=re.findall('\((.*?)\)',n)
chang=int(fnum[0])
elif int1.__len__()>2: #()中有多个运算符
chang=de(n)
else: #运算式中只有一个运算符开始运算
opertional=re.findall('[^(\d.)]',n) #获取运算符号
print(opertional)
opertional=opertional[0]
print("Int:%s" %int1)
a=float(int1[0]) #将字符转为数字
b=float(int1[1])
chang=fa(a,b,opertional)
print(chang)
return chang
def de(s1):
if re.search(r'\d+[.]?[\d]*(\*|/)\d+[.]?[\d]*',s1): #先乘除
ret=re.search(r'\d+[.]?[\d]*(\*|/)\d+[.]?[\d]*',s1)
num=ret.group()
print('Num: %s'%num)
chang=getNum(num)
s1=re.sub(r'(\d+[.]?[\d]*(\*|/)\d+[.]?[\d]*){1}',str(chang),s1,count=1)
#print(news)
elif re.search(r'\d+[.]?[\d]*(\+|-)\d+[.]?[\d]*',s1): #再加减
ret=re.search(r'\d+[.]?[\d]*(\+|-)\d+[.]?[\d]*',s1)
num=ret.group()
print('Num..........: %s'%num)
chang=getNum(num)
s1=re.sub(r'(\d+[.]?[\d]*(\+|-)\d+[.]?[\d]*){1}',str(chang),s1,count=1)
return s1
def yusuan(news):
global s,flag
if re.search(r'\([^()]+\)',news):
ret=re.search(r'\([^()]+\)',news)
num=ret.group()
print(num)
chang=getNum(num)
print(chang)
news=re.sub(r'(\([^()]+\)){1}',str(chang),news,count=1)
s=news
print(news)
elif re.search(r'\d+[.]?[\d]*(\*|/)\d+[.]?[\d]*',news):
ret=re.search(r'\d+[.]?[\d]*(\*|/)\d+[.]?[\d]*',news)
num=ret.group()
chang=getNum(num)
news=re.sub(r'(\d+[.]?[\d]*(\*|/)\d+[.]?[\d]*){1}',str(chang),news,count=1)
s=news
#print(news)
elif re.search(r'\d+[.]?[\d]*(\+|-)\d+[.]?[\d]*',news):
ret=re.search(r'\d+[.]?[\d]*(\+|-)\d+[.]?[\d]*',news)
num=ret.group()
chang=getNum(num)
news=re.sub(r'(\d+[.]?[\d]*(\+|-)\d+[.]?[\d]*){1}',str(chang),news,count=1)
s=news
#print(news)
else:
print(news)
flag=1
return news
flag=0
while flag==0:
s=s.replace('+-','-') #算式替换
s=s.replace('--','+')
yusuan(s)