C语言:一元二次方程
考虑了a与0的关系和△与0的关系。
#include<stdio.h>
#include<math.h>
int main()
{
double a, b, c, x1, x2, d,x3 ,x0;
printf("输入方程的三个系数:");
scanf_s("%lf %lf %lf", &a, &b, &c);
if (a != 0)
{
d = sqrt(b * b - 4 * a * c);
if (d == 0)
{
x0 = (-b) / (2 * a);
printf("%.2lf\n", x0);
}
else if(d>0)
{
x1 = (-b + d) / (2 * a);
x2 = (-b - d) / (2 * a);
printf("%.2lf %.2lf\n", x1, x2);
}
else
printf("x无解");
}
else
{
x3 = -c / b;
printf("%.2lf\n", x3);
}
return 0;
}