解一元二次方程a x2 b x c = 0,a、b、c由键盘输入。
分析:对系数a、b、c考虑以下情形
1) 若a = 0:
① b 0,则x=-c/b ;
② b = 0, 则:① c = 0, 则x无定根;
② c 0,则x无解。
2) 若a 0 ;
① b2-4 a c > 0,有两个不等的实根;
② b2-4 a c = 0,有两个相等的实根;
③ b2-4 a c
#include
main( )
{
float a,b,c,s,x1,x2;
double t;
printf(" please input a,b,c:");
scanf("%f %f %f" , &a , &b , &c);
if (a==0。
0)
if(b != 0。0)
printf("the root is :%f
",-c/b);
else if (c==0。
0)
printf("x is inexactive
");
else
printf("no root!
");
else
{
s = b * b - 4 * a * c ;
if(s > = 0。
0)
if(s > 0。0)
{
t = sqrt( s ) ;
x1 = -0。
5 *(b t) / a;
x 2 = - 0。5 * ( b - t ) / a ;
printf("There are two different roots:%f and%f
" ,x 1 , x 2 ) ;
}
else
printf("There are two equal roots:%f
",-0。
5*b/a);
else
{
t = sqrt( -s );
x1=-0。5*b/a; / *实部* /
x2=abs(0。
5*t/a); /*虚部的绝对值* /
printf("There are two virtual roots:");
printf("%f i%f%f-i%f
",x1,x2,x1,x2 );
}
}
}
运行结果如下:
please input a,b,c : 1 2 3
There are two virtual roots:
-1。
000000 i 1。000000 -1。000000 - i 1。000000
please input a,b,c : 2 5 3
There are two different roots : -1。
500000 and -1。000000
please input a,b,c : 0 0 3
No root!。
全部