import
re
def
md(l,x):
a
=
l.index(x)
if
x
=
=
'*'
and
l[a
+
1
] !
=
'-'
:
k
=
float
(l[a
-
1
])
*
float
(l[a
+
1
])
elif
x
=
=
'/'
and
l[a
+
1
] !
=
'-'
:
k
=
float
(l[a
-
1
])
/
float
(l[a
+
1
])
elif
x
=
=
'*'
and
l[a
+
1
]
=
=
'-'
:
k
=
-
(
float
(l[a
-
1
])
*
float
(l[a
+
2
]))
elif
x
=
=
'/'
and
l[a
+
1
]
=
=
'-'
:
k
=
-
(
float
(l[a
-
1
])
/
float
(l[a
+
2
]))
del
l[a
-
1
], l[a
-
1
], l[a
-
1
]
l.insert(a
-
1
,
str
(k))
return
l
def
fun(s):
l
=
re.findall(
'([\d\.]+|/|-|\+|\*)'
,s)
sum
=
0
while
1
:
if
'*'
in
l
and
'/'
not
in
l:
md(l,
'*'
)
elif
'*'
not
in
l
and
'/'
in
l:
md(l,
'/'
)
elif
'*'
in
l
and
'/'
in
l:
a
=
l.index(
'*'
)
b
=
l.index(
'/'
)
if
a < b:
md(l,
'*'
)
else
:
md(l,
'/'
)
else
:
if
l[
0
]
=
=
'-'
:
l[
0
]
=
l[
0
]
+
l[
1
]
del
l[
1
]
sum
+
=
float
(l[
0
])
for
i
in
range
(
1
,
len
(l),
2
):
if
l[i]
=
=
'+'
and
l[i
+
1
] !
=
'-'
:
sum
+
=
float
(l[i
+
1
])
elif
l[i]
=
=
'+'
and
l[i
+
1
]
=
=
'-'
:
sum
-
=
float
(l[i
+
2
])
elif
l[i]
=
=
'-'
and
l[i
+
1
]
=
=
'-'
:
sum
+
=
float
(l[i
+
2
])
elif
l[i]
=
=
'-'
and
l[i
+
1
] !
=
'-'
:
sum
-
=
float
(l[i
+
1
])
break
return
sum
def
calculate(expression):
ex
=
[]
ans
=
0
if
'('
not
in
expression:
ans
=
fun(expression)
return
ans
for
i
in
range
(
len
(expression)):
if
expression[i]
=
=
'('
:
ex.append(i)
#ex=[6,7]
elif
expression[i]
=
=
')'
:
#14
temp
=
0
sub
=
expression[ex[
len
(ex)
-
1
]
+
1
:i]
temp
=
fun(sub)
expression
=
expression[
0
:ex[
len
(ex)
-
1
]]
+
str
(temp)
+
expression[i
+
1
:
len
(expression)
+
1
]
ex.pop()
return
calculate(expression)
s
=
'1 - 2 * ( (60-30 +(-40/5+3) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
print
(
1
-
2
*
( (
60
-
30
+
(
-
40
/
5
+
3
)
*
(
9
-
2
*
5
/
3
+
7
/
3
*
99
/
4
*
2998
+
10
*
568
/
14
))
-
(
-
4
*
3
)
/
(
16
-
3
*
2
) ))
#1735397.4095238098
s3
=
'3*(4+50)-((100+40)*5/2-3*2*2/4+9)*(((3+4)-4)-4)'
#518.0
print
(
3
*
(
4
+
50
)
-
((
100
+
40
)
*
5
/
2
-
3
*
2
*
2
/
4
+
9
)
*
(((
3
+
4
)
-
4
)
-
4
))
print
(calculate(s))
#1735397.4095238098
print
(calculate(s3))
1,在没有括号的情况下:
def
multdiv(l,x):
#定义最小的乘除运算单元,l是列表,x代表*或/
a
=
l.index(x)
#首先获取乘除运算符的位置
if
x
=
=
'*'
and
l[a
+
1
] !
=
'-'
:
#判断*,/后面的一个操作符是否是‘-’如果是的话,分别进行处理
k
=
float
(l[a
-
1
])
*
float
(l[a
+
1
])
elif
x
=
=
'/'
and
l[a
+
1
] !
=
'-'
:
k
=
float
(l[a
-
1
])
/
float
(l[a
+
1
])
elif
x
=
=
'*'
and
l[a
+
1
]
=
=
'-'
:
k
=
-
(
float
(l[a
-
1
])
*
float
(l[a
+
2
]))
elif
x
=
=
'/'
and
l[a
+
1
]
=
=
'-'
:
k
=
-
(
float
(l[a
-
1
])
/
float
(l[a
+
2
]))
del
l[a
-
1
], l[a
-
1
], l[a
-
1
]
#删除掉列表里刚做运算的三个元素,比如,3 * 2
l.insert(a
-
1
,
str
(k))
#将刚计算的结果插入到列表中然后执行下一次计算
return
l
def
fun(l):
sum
=
0
print
(l)
while
1
:
#先将乘除运算计算完,在计算加减
if
'*'
in
l
and
'/'
not
in
l:
#先判断,如果只有*的话,先计算 *
multdiv(l,
'*'
)
elif
'*'
not
in
l
and
'/'
in
l:
#如果只有 /的话,先计算 /
multdiv(l,
'/'
)
elif
'*'
in
l
and
'/'
in
l:
#如果既有 / 也有 *的话,先获取他们的下标,
a
=
l.index(
'*'
)
#根据下标判断先执行哪个
b
=
l.index(
'/'
)
if
a < b:
multdiv(l,
'*'
)
else
:
multdiv(l,
'/'
)
else
:
#当上面的乘除计算完之后,就可以计算加减了
print
(l)
if
l[
0
]
=
=
'-'
:
#这里需要判断一下,如果列表里第一个符号是‘-’
l[
0
]
=
l[
0
]
+
l[
1
]
#的话,表示第一个数是负数,所以我们需要将列表第一和第二项合并起来
del
l[
1
]
sum
+
=
float
(l[
0
])
#做完上面的处理后列表中就只剩加减计算了,
for
i
in
range
(
1
,
len
(l),
2
):
if
l[i]
=
=
'+'
and
l[i
+
1
] !
=
'-'
:
#判断+,-后面的一个操作符是否是‘-’如果是的话,分别进行处理
sum
+
=
float
(l[i
+
1
])
elif
l[i]
=
=
'+'
and
l[i
+
1
]
=
=
'-'
:
sum
-
=
float
(l[i
+
2
])
elif
l[i]
=
=
'-'
and
l[i
+
1
]
=
=
'-'
:
sum
+
=
float
(l[i
+
2
])
elif
l[i]
=
=
'-'
and
l[i
+
1
] !
=
'-'
:
sum
-
=
float
(l[i
+
1
])
break
return
sum
#最后返回这个不含括号表达式的结果
2在有括号的情况下
def
calculate(expression):
ex
=
[]
#存储'('出现的位置
ans
=
0
#保存结果
if
'('
not
in
expression:
#如果括号都处理完成了,直接调用fun函数返回结果
ans
=
fun(expression)
return
ans
for
i
in
range
(
len
(expression)):
if
expression[i]
=
=
'('
:
ex.append(i)
#ex=[6,7] #纪录 '(' 出现的位置
elif
expression[i]
=
=
')'
:
#遇到 ')'后。就可以计算第一个括号里的值
temp
=
0
#定义一个变量 存储括号表达式的结果
sub
=
expression[ex[
len
(ex)
-
1
]
+
1
:i]
#获取括号里的表达式
temp
=
fun(sub)
#调用fun函数计算括号里的表达式的值
expression
=
expression[
0
:ex[
len
(ex)
-
1
]]
+
str
(temp)
+
expression[i
+
1
:
len
(expression)
+
1
]
#去掉刚才的括号表达式,并用temp代替,返回一个新的表达式
ex.pop()
#删除刚才计算完的括号表达式里面 '(' 的位置
return
calculate(expression)
#递归计算新的表达式,直道所有的括号处理完毕