编写完整代码
#include<stdio.h>
#include<math.h> // 需要使用 sqrt() 函数
int main()
{
float a, b, c, determinant, r1, r2, real, imag;
printf("\nEnter coefficients a, b and c: ");
scanf("%f%f%f", &a, &b, &c);
determinant = b*b - 4*a*c;
if(determinant > 0) // 两个根都是实数
{
r1 = (-b + sqrt(determinant))/2*a; // 括号很重要
r2 = (-b - sqrt(determinant))/2*a;
printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2);
}
else if(determinant == 0) // 两个根都是实数且相等
{
r1 = r2 = -b/(2*a); // 括号很重要
printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2);
}
/*
Determinant < 0 - 两个根都是虚数,形式为 real + i*imaginary
*/
else
{
real = -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("\n\n\nRoots are %.2f + i%.2f and %.2f - i%.2f ", real, imag, real, imag);
}
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}