import math
while True:
try:
a,b,c=map(float,input().split(' '))
derta=b*b-4*a*c
x1=x2=0
if a != 0:
if derta == 0:
if b == 0:
x1 = x2 = 0
else:
x1 = x2 = -b / (2 * a)
print('x1=x2=%0.2f' % x1)
elif derta > 0:
x1 = ((-b) - math.sqrt(derta)) / (2 * a)
x2 = ((-b) + math.sqrt(derta)) / (2 * a)
print('x1=%0.2f;x2=%0.2f' % (x1, x2))
else:
if b == 0:
real = b / (2 * a)
else:
real = (-b) / (2 * a)
image = math.sqrt(-derta) / (2 * a)
print('x1=%0.2f-%0.2fi;x2=%0.2f+%0.2fi' % (real, image, real, image))
else:
print('Not quadratic equation')
except:
while True:
try:
a,b,c=map(float,input().split(' '))
derta=b*b-4*a*c
x1=x2=0
if a != 0:
if derta == 0:
if b == 0:
x1 = x2 = 0
else:
x1 = x2 = -b / (2 * a)
print('x1=x2=%0.2f' % x1)
elif derta > 0:
x1 = ((-b) - math.sqrt(derta)) / (2 * a)
x2 = ((-b) + math.sqrt(derta)) / (2 * a)
print('x1=%0.2f;x2=%0.2f' % (x1, x2))
else:
if b == 0:
real = b / (2 * a)
else:
real = (-b) / (2 * a)
image = math.sqrt(-derta) / (2 * a)
print('x1=%0.2f-%0.2fi;x2=%0.2f+%0.2fi' % (real, image, real, image))
else:
print('Not quadratic equation')
except:
break
以上要注意几点:1复数的表示,在格式化输出中没有直接以复数形式打印的
2很容易犯错的一点:当b=0时,结果为-0,此时(-b)变成-0
3 多行输入,且不知道输入几行,用try—except