#include
#include
int main ( )
{
double a,b,c,disc,x1,x2,p,q;
scanf("%lf%lf%lf",&a,&b,&c);
disc=b*b-4*a*c;
if(disc<0)
printf("This equation has't real roots\n");
else
{
p=-b/(2.0*a);
q=sqrt(disc)/(2.0*a);
x1=p+q;
x2=p-q;
printf("real roots:\nx1=%7.2f\nx2=%7.2f\n",x1,x2);
}
return 0;
}
#include
#include
int main(void) //求方程ax^2+bx+c=0的解
{
float a,b,c,disc,x1,x2,realpart,imagpart;
scanf("%f,%f,%f",&a,&b,&c);
printf("The equation");
if(fabs(a)<=1e-6) //a=0,此方程不为二元一次方程,fabs()为求绝对值函数
printf("is not a quadratic\n");
else
{
disc=b*b-4*a*c;
if(fabs(disc)<=1e-6) //disc=0,方程有两个相等的实数根
printf("has two equal roots:%8.4f\n",-b/(2*a));
else if(disc>1e-6) //disc>0,方程有两个不相等的实数根
{
x1=(-b+sqrt(disc))/(2*a); //sqrt()为开平方根函数
x2=(-b-sqrt(disc))/(2*a);
printf("has distinct real roots:%8.4f and %8.4f\n",x1,x2);
}
else //disc<0,方程有两个虚根
{
realpart=-b/(2*a);
imagpart=sqrt(-disc)/(2*a);
printf("has complex roots: \n");
printf("%8.4f+%8.4fi\n",realpart,imagpart);
printf("%8.4f-%8.4fi\n",realpart,imagpart);
}
}
}
书上例题做练习时留下的
这道题很多书上都是作为习题来讲的。