1、a=0,不是二次方程。
2、一4ac=0,有两个相等实根
3、-4ac>0,有两个不等实根。
4、-4ac<0,有两个共辄复根。应当以p+qi和p 一qi的形式输出复根。其中,p=-b/2a , q=()/ 2a。
N-S图如下:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int main()
{
double a, b, c, disc, x1, x2, realpart, imagpart;
scanf("%lf%lf%lf", &a, &b, &c);
printf("The equation");
/*
因为求得是一个实数,不能精确的得到数字,存储是会有微小的误差
不能直接disc==0,所以用一个很小的数(约等于0)来判定
*/
if (fabs(a) <= 1e-6)
printf(" is not a quadratic\n");
else
{
disc = b * b - 4 * a * c;
if (fabs(disc) <= 1e-6)
/*
这里用绝对值是为了使disc无限逼近0,使得满足disc=0
*/
printf(" has two equal roots:%8.4f\n", -b / (2 * a));
else
if (disc > 1e-6)
{
x1 = (-b + sqrt(disc)) / (2 * a);
x2 = (-b - sqrt(disc)) / (2 * a);
printf(" has two different real roots:%8.4f and %8.4f\n", x1, x2);
}
else
{
realpart = -b / (2 * a);
imagpart = sqrt(-disc) / (2 * a);
printf(" has complex roots:\n");
//i是虚部
printf("%8.4f+%8.4fi\n", realpart, imagpart);
}
}
return 0;
}