题目描述
求方程 的根,用三个函数分别求当b^2-4ac大于0、等于0、和小于0时的根,并输出结果。从主函数输入a、b、c的值。
输入
a b c
输出
x1=? x2=?
样例输入
4 1 1
样例输出
x1=-0.125+0.484i x2=-0.125-0.484i
#include<stdio.h>
#include<math.h>
double q,p;
double fun1(double a,double b,double c,double s)
{
p=-b/(2*a);
q=(sqrt(s))/(2*a);
printf("x1=%.3lf x2=%.3lf\n",p+q,p-q);
return 0;
}
double fun2(double a,double b,double c)
{
p=-b/(2*a);
printf("x1=%.3lf x2=%.3lf\n",p,p);
return 0;
}
double fun3(double a,double b,double c,double s)
{
p=-b/(2*a);
q=sqrt(-s)/(2*a);
printf("x1=%.3lf+%.3lfi x2=%.3lf-%.3lfi\n",p,q,p,q);
return 0;
}
int main()
{
double a,b,c,s;
scanf("%lf%lf%lf",&a,&b,&c);
s=b*b-4*a*c;
if(s>0)fun1(a,b,c,s);
else if(s==0)fun2(a,b,c);
else fun3(a,b,c,s);
return 0;
}