(C语言网)题目 1028: [编程入门]自定义函数求一元二次方程

(C语言网)题目 1028: [编程入门]自定义函数求一元二次方程_第1张图片

#include 
#include 

double big(double a,double b,double c)
{
	double d=b*b-4*a*c;
	double e=(-b+sqrt(d))/(2*a);
	double f=(-b-sqrt(d))/(2*a);
	    if(a==0&&b!=0)
	    {
	        printf("x1=%.3f x2=%.3f",-c/b,-c/b);
	    }
	    else
	    {
	        printf("x1=%.3f x2=%.3f",e,f);
	    
	    }
 } 
 
 double small(double a,double b,double c)
 {
 	double d=b*b-4*a*c;
 	double j=-b/(2*a);
	double	h=sqrt(-d)/(2*a);
    if(j==0)
    {     
     printf("x1=%.3f+%.3fi x2=%.3f-%.3fi",0.0,h,0.0,h);
    }
    else
    {
  	  printf("x1=%.3f+%.3fi x2=%.3f-%.3fi",j,h,j,h);
    }
 }

double same (double a,double b,double c)
{
	printf("x1=%.3f x2=%.3f",-b/(2*a),-b/(2*a));
}

int main ()
{
	double a,b,c;
	scanf("%lf %lf %lf",&a,&b,&c);
	
	double d=b*b-4*a*c;
	
	if(d > 0)
	big(a,b,c);
	if(d<0)
	small(a,b,c);
	if(d == 0)
	same(a,b,c);
	
	return 0;
}

 

你可能感兴趣的:(C/C++,c语言,蓝桥杯,开发语言)