题目描述
求方程 的根,用三个函数分别求当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>//为了引用sqrt()函数(开平方)
void f1(double a,double b,double c);
void f2(double a,double b,double c);
void f3(double a,double b,double c);
int main()
{
double a,b,c;
scanf("%lf%lf%lf",&a,&b,&c);
if((b*b-4*a*c)>0)
{
f1(a,b,c);
}
else if((b*b-4*a*c)==0)
{
f2(a,b,c);
}
else//b*b-4*a*c<0时,方程是复数解
{
f3(a,b,c);
}
return 0;
}
void f1(double a,double b,double c)
{
printf("x1=%.3lf x2=.3%lf",(-b+sqrt(b*b-4*a*c))/(2*a),
(-b-sqrt(b*b-4*a*c))/(2*a));
}
void f2(double a,double b,double c)
{
printf("x1=.3%lf x2=.3%lf",(-b)/(2*a),(-b)/(2*a));
}
void f3(double a,double b,double c)
{
printf("x1=%.3lf+%.3lfi x2=%.3lf%.3lfi",(-b)/(2*a),sqrt(-(b*b-4*a*c))/(2*a),
(-b)/(2*a),-sqrt(-(b*b-4*a*c))/(2*a));
}