1029: C语言-解方程
题目描述
求方程 的根,用三个函数分别求当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>
void k(double a,double b,double m);
void kk(double a,double b,double m);
void kkk(double a,double b,double m);
int main()
{
double a,b,c,m;
scanf("%lf %lf %lf",&a,&b,&c);
m=b*b-4*a*c;
if(m>0) k(a,b,m);
if(m==0) kk(a,b,m);
if(m<0) kkk(a,b,m);
return 0;
}
void k(double a,double b,double m)
{
printf("x1=%0.3lf x2=%0.3lf",(-b+sqrt(m))/(2.0*a),(-b-sqrt(m))/(2.0*a));
}
void kk(double a,double b,double m)
{
printf("x1=%0.3lf x2=%0.3lf",(-b+sqrt(m))/(2.0*a),(-b-sqrt(m))/(2.0*a));
}
void kkk(double a,double b,double m)
{
printf("x1=%0.3lf+%0.3lfi x2=%0.3lf-%0.3lfi",(-b)/(2.0*a),sqrt(-m)/(2.0*a),(-b)/(2.0*a),sqrt(-m)/(2.0*a));
}