c语言,求解一元二次方程ax^2+bx+c=0的根程序如图,问若求解分三个函数,其形参如何设置
1个回答
展开全部
#include <stdio.h>
#include <math.h>
int main()
{
bool equalroot(float d,float a,float b);
bool tworealroot(float d,float a,float b);
bool complexroot(float d,float a,float b);
float a,b,c,d;
scanf("%f%f%f",&a,&b,&c);
printf("the equation ");
if(fabs(a)<1e-6)
{
printf("is not quadratic\n");
}
else
{
d=b*b-4*a*c;
if(equalroot(d,a,b)) return 0;
if(tworealroot(d,a,b)) return 0;
if(complexroot(d,a,b)) return 0;
}
return 0;
}
bool equalroot(float d,float a,float b) //计算等根情况
{
if(fabs(d)<=1e-6)
{
printf("has two equal roots:\n");
printf("x1=x2=%8.4lf\n",-b/(2*a));
return true;
}
return false;
}
bool tworealroot(float d,float a,float b) //计算不同实根情况
{
float x1,x2;
if(d>=0)
{
x1=((-b+sqrt(d))/(2*a));
x2=((-b-sqrt(d))/(2*a));
printf("has two real roots:\n");
printf("x1=%8.4lf, x2=%8.4lf\n",x1,x2);
return true;
}
return false;
}
bool complexroot(float d,float a,float b) //计算复根情况
{
float jp,ip;
if(d<0)
{
jp=-b/(2*a);
ip=sqrt(-d)/(2*a);
printf("has two complex root:\n");
printf("x1=%8.4lf +%8.4lfi\n",jp,ip);
printf("x1=%8.4lf -%8.4lfi\n",jp,ip);
return true;
}
return false;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询