程序代码:
#include <stdio.h>
#include <math.h>
#include <complex.h>
int main( void )
{
double a,b,c;
if( 3 != scanf("%lf%lf%lf",&a,&b,&c) )
return 1;
if( a == 0 )
{
if( b == 0 )
printf( "---fuck---\n" );
else
printf( "%lg\n", -c/b );
}
else
{
double delta = b*b-4*a*c;
if( delta > 0 )
{
double sd = sqrt( delta );
double x1 = (-b+sd)/(2*a);
double x2 = (-b-sd)/(2*a);
printf( "%lg, %lg\n", x1, x2 );
}
else if( delta < 0 )
{
double _Complex sd = csqrt( delta );
double _Complex x1 = (-b+sd)/(2*a);
double _Complex x2 = (-b-sd)/(2*a);
printf( "%lg%+lgi, %lg%+lgi\n", creal(x1), cimag(x1), creal(x2), cimag(x2) );
}
else // delta == 0
{
printf( "%lg\n", -b/(2*a) );
}
}
return 0;
}
输入 0 0 3 输出 ---fuck---
输入 0 2 6 输出 -3
输入 2 3 1 输出 -0.5, -1
输入 5 4 1 输出 -0.4+0.2i, -0.4-0.2i
输入 4 4 1 输出 -0.5