您的位置:首页 > 其它

求一元二次方程的根

2016-11-17 16:21 253 查看
#include <stdio.h>

#include <math.h>

void main()

{
double a,b,c;
double s,x1,x2;
printf("Please input a,b,c:\n");
scanf("%lf%lf%lf",&a,&b,&c);
if(a>=-(1e-6) && a<=(1e-6))
printf("Sorry! You have a wrong number a.\n");
else
{
s=b*b-4*a*c;
if(s>(1e-6))
{/*计算两不相等的实数根*/
x1=(-b+sqrt(s))/(2*a);

            x2=(-b-sqrt(s))/(2*a);
printf("There are two different real:\nx1=%5.2f,x2=%5.2f\n",x1,x2);
}
else
if(s>=-(1e-6) && s<=(1e-6))
{/*计算两相等的实数根*/
x1=x2=-b/(2*a);
printf("There are two equal real:\nx1=%5.2f\n",x1);
}
else
{/*计算两不相等的共轭复根*/
s=-s;
x1=-b/(2*a);
x2=fabs(sqrt(s)/(2*a));
printf ("There are two different complex:\nx1=%5.2f+%5.2fi,x2=%5.2f-%5.2fi\n",x1,x2,x1,x2);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: