C语言如何计算求解一元二次方程ax+bx+c=0的根
#include<stdio.h>
#include<math.h>
int fun1(double a,double b,double d)
{ double x1,x2;
x1=(b+sqrt(d))/(-1*2*a);
x2=(b-sqrt(d))/(-1*2*a);
printf("x1=%.2lf x2%.2lf",x1,x2);
}
int fun2(double a,double b,double d)
{double x1,x2;
x1=x2=(b+sqrt(d))/(-2*a);
printf("x1=%.2lf x2=%.2lf",x1,x2);
}
int fun3(double a,double b,double d)
{double x1,x2,y1,y2;
x1=(-b)/(2*a);
y1=sqrt(-d)/(2*a);
x2=(-b)/(2*a);
y2=sqrt(-d)/(2*a);
printf("x1=%.3lf+%.3lfi x2=%.3lf-%.3lfi",x1,y1,x2,y2);
}
int main()
{
double a,b,c,d;
double x1,x2;
scanf("%lf%lf%lf",&a,&b,&c);
d=b*b-4*a*c;//b*b-4*a*c有3中情况,因此定义3个函数来计算3种情况;
if(d>0)
fun1(a,b,d);
if(d==0)
fun2(a,b,d);
if(d<0)//这种情况结果为复数;
fun3(a,b,d);
return 0;
}
广告 您可能关注的内容 |