希望有用,敬请采纳 ^_^
# include
# include
int main()
{
void root2(double a,double b,double disc); //定义方程有两个根时的函数
void root1(double a,double b); //定义方程只有一个根时的函数
void root0();//定义方程没有实数解是的函数
double a,b,c,disc;
printf("请输入a,b,c的值:");
scanf("%lf %lf %lf",&a,&b,&c);
if(a==0) //一元二次方程二次项系数不为0
{
printf("data error\n");
}
else
{
disc=b*b-4*a*c;
if(disc>0)
root2(a,b,disc);
else if(disc==0)
root1(a,b);
else
root0();
}
return 0;
}
void root2(double a,double b,double disc)
{
double x1,x2;
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("x1=%lf\nx2=%f\n",x1,x2);
}
void root1(double a,double b)
{
double x;
x=(-b)/(2*a);
printf("x1=x2=%lf\n",x);
}
void root0()
{
printf("方程没有实数解\n");
}