1.b*b+4a*c=0,有两个相等实根
2.b*b+4a*c>0,有两个不相等实根
3.b*b+4a*c<0,有两个共轭实根(p-qi)(p+qi)
#include<stdio.h>
#include<math.h>
int main()
{
printf("请计算a*x*x+b*x+c=0的值\n");
double a = 0.0, b = 0.0, c = 0.0;
double shishu = 0.0, gonge = 0.0;
printf("请输入a,b,c的值:");
scanf("%lf %lf %lf",&a,&b,&c);
double m = 0.0,x1=0.0,x2=0.0;
m = sqrt(b * b - 4 * a * c);
if (m > 0)
{
x1 = (-b + m) / (2*a);
x2 = (-b - m) / (2*a);
printf("x1=%8.4lf x2=%8.4lf\n", x1, x2);
}
else if (m ==0 )
{
x1 = x2 = (-b) / (2 * a);
printf("x1=x2=%lf\n",x2);
}
else
{
shishu =( -b) / (2 * a);
gonge = sqrt(-(b * b - 4 * a * c)) / (2 * a);
printf("x1=%8.4lf+%8.4lfi\n",shishu,gonge);
printf("x2=%8.4lf-%8.4lfi\n", shishu, gonge);
}
return 0;
}
输出相等结果:
输出不相等实根:
输出共轭复根: