[python]代码库
# -*- coding: utf-8 -*-
import math
def quadratic(a1,b1,c1):
a = int(a1)
b = int(b1)
c = int(c1)
if b*b-4*a*c >0 :
t=b*b-4*a*c
t2 = math.sqrt(t)
x1 = -(b/(2*a))+(t2/(2*a))
x2 = -(b/(2*a))-(t2/(2*a))
z = 1
return x1,x2,z
elif b*b-4*a*c == 0 :
x1 = (-b/(2*a))
z=0
return x1,x1,z
else:
t=4*a*c-b*b
t2 = math.sqrt(t)
x = (-b/(2*a))
y = (t2/(2*a))
z = -1
return x,y,z
a = input('输入ax2+bx+c的a值:')
b = input('输入ax2+bx+c的b值:')
c = input('输入ax2+bx+c的c值:')
x,y,z = quadratic(a,b,c)
if z == 1:
print('该方程有两个根x1,x2分别为%.3f %.3f'%(x,y))
elif z == 0:
print('该方程有一个根x1=%.3f'%(x))
else :
print('该方程有两个虚根a,b分别为%.3f %.3f'%(x,y))