团队-科学计算器-模块测试过程
-
项目托管平台地址:https://gitee.com/Marly/codes/vbnfqyx3mzpsk8062rc9e77 模块测试: 不带括号fun功能,
-
代码部分:
-
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 sums='1+1+1+2+3+45+6/6+6*9'
print(1+1+1+2+3+45+6/6+6*9) -
测试:s='1+1+1+2+3+45+6/6+6*9'
print(1+1+1+2+3+45+6/6+6*9)
-
-
calculate功能
-
代码:
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)
elif expression[i]==')':
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() -
测试方法:
-
s='(5+5)+5*5/25-8+9'
print((5+5)+5*5/25-8+9) - 遇到的其他的问题:
- 代码修复的次数较多,首先并没有考虑括号的问题。