c语言,求解一元二次方程ax^2+bx+c=0的根程序如图,问若求解分三个函数,其形参如何设置
答案:1 悬赏:20 手机版
解决时间 2021-11-11 18:28
- 提问者网友:沦陷
- 2021-11-11 07:21
c语言,求解一元二次方程ax^2+bx+c=0的根程序如图,问若求解分三个函数,其形参如何设置
最佳答案
- 五星知识达人网友:大漠
- 2021-11-11 07:42
#include
#include
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
");
}
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:
");
printf("x1=x2=%8.4lf
",-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:
");
printf("x1=%8.4lf, x2=%8.4lf
",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:
");
printf("x1=%8.4lf +%8.4lfi
",jp,ip);
printf("x1=%8.4lf -%8.4lfi
",jp,ip);
return true;
}
return false;
}
#include
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
");
}
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:
");
printf("x1=x2=%8.4lf
",-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:
");
printf("x1=%8.4lf, x2=%8.4lf
",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:
");
printf("x1=%8.4lf +%8.4lfi
",jp,ip);
printf("x1=%8.4lf -%8.4lfi
",jp,ip);
return true;
}
return false;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯