求解一元二次方程

代码如下:

import math
import cmath
a,b,c = eval(input().replace(' ',','))
delta = b*b - 4*a*c
if abs(delta) < 10**-6:    
    x1 = -b/(2*a)   
    if(abs(x1)<10**-6):        
        x1 = 0;   
    print("x1=x2={:.5f}".format(x1))
elif delta > 0:    
    x1 = (-b + math.sqrt(delta))/(2*a)    
    x2 = (-b - math.sqrt(delta))/(2*a)    
    print("x1={:.5f};x2={:.5f}".format(x1,x2))

else :    
    x1 = (-b + cmath.sqrt(delta))/(2*a)    
    x2 = (-b - cmath.sqrt(delta))/(2*a)    
    print("x1={0.real:.5f}{0.imag:+.5f}i;".format(x1),end = '')    
    print("x2={0.real:.5f}{0.imag:+.5f}i".format(x2))

结果如下:

 

你可能感兴趣的:(python)