代码
三种情况都有 但还有很多优化空间,包括函数变量的调用,输出等等。
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,c,disc;
int bigger_zero(float a,float b,float c,float disc);
int zero(float a,float b,float c,float disc);
int smaller_zero(float a,float b,float c,float disc);
scanf("%f,%f,%f",&a,&b,&c);
disc = b*b-4*a*c;
if (disc>0)
bigger_zero(a,b,c,disc);
else if(disc<0)
smaller_zero(a,b,c,disc);
else
zero(a,b,c,disc);
return 0;
}
int bigger_zero(float a,float b,float c,float disc){
float x1 = (-b+sqrt(disc))/(a*2);
float x2 = (-b-sqrt(disc))/(a*2);
printf("x1=%f,x2=%f\n",x1,x2);
return 0;
}
int zero(float a,float b,float c,float disc){
float x = -b/(2*a);
printf("x1=x2=%f\n",x);
return 0;
}
int smaller_zero(float a,float b,float c,float disc)
{
float realpart = -b/(2*a);
float imagepart = sqrt(-disc)/(2*a);
printf("complex root x1=%f+%fi,x2=%f%fi",realpart,imagepart,realpart,imagepart);
}
|