习题专题-求一元二次方程
原创
©著作权归作者所有:来自51CTO博客作者进击的企鹅的原创作品,请联系作者获取转载授权,否则将追究法律责任
求一元二次方程
#include<stdio.h>
#include<math.h>
int main()
{
double a = 0;
double b = 0;
double c = 0;
scanf("%lf %lf %lf", &a, &b, &c);
double x1 = 0;
double x2 = 0;
double B = 0;
double C = 0;
B = (b * b) - (4 * a * c);
C = sqrt(B);
x1 = (-b + C) / (2 * a);
x2 = (-b - C) / (2 * a);
if (a!=0)
{
if (B>=0)
{
if (x1 == x2)
{
printf("x1=x2=%.5lf", x1);
}
else if (x1 != x2)
{
if (x1 > x2)
{
printf("x1=%.5lf;x2=%.5lf", x2, x1);
}
else if (x1 < x2)
{
printf("x1=%.5lf;x2=%.5lf", x1, x2);
}
}
}
if (B<0)
{
printf("No answer!");
}
}
return 0;
}
运行结果: