题解 | #计算一元二次方程#
计算一元二次方程
http://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include<stdio.h>
#include<math.h>
#include<complex.h>
int main()
{
float a,b,c,x1,x2,de,temp;
while(scanf("%f %f %f",&a,&b,&c)!=EOF)
{
de=b*b-4*a*c;
if(a==0)
{
printf("Not quadratic equation");
}
else
{
if(de>0)
{
x1=-sqrt((b*b-4*a*c)/(4*a*a))-b/(2*a);
x2=sqrt((b*b-4*a*c)/(4*a*a))-b/(2*a);
if(x1<x2)
{
printf("x1=%.2f;x2=%.2f\n",x1,x2);
}
else
{
temp=x1;
x1=x2;
x2=temp;
printf("x1=%.2f;x2=%.2f\n",x1,x2);
}
}
if(de==0)
{
x1=-b/(2*a);
printf("x1=x2=%.2f\n",x1);
}
if(de<0)
{
double e,r;
e=-b/(2*a);
r=sqrt(4*a*c-b*b)/(2*a);
printf("x1=%.2lf-%.2lfi;x2=%.2f+%.2lfi\n",e,r,e,r);
}
}
}
return 0;
}