实验3-1 求一元二次方程的根
这个题目的纯虚根问题搞的头大,其实没意思。
#include <stdio.h>
#include <math.h>
int main(void)
{
double a, b, c;
scanf("%lf%lf%lf", &a, &b, &c);
double delta = b * b - 4 * a * c;
if (a == 0 && b == 0 && c == 0)
{
printf("Zero Equation\n");
}
else if (a == 0 && b == 0 && c != 0)
{
printf("Not An Equation\n");
}
else if (a == 0 && b != 0)
{
printf("%.2f\n", -c / b);
}
else if (a != 0)
{
if (delta == 0)
{
printf("%.2f\n", -b / 2 / a);
}
else if (delta > 0)
{
printf("%.2f\n", (-b + sqrt(delta)) / (2 * a));
}
else
{
if (b == 0)
{
printf("%.2f+%.2fi\n%.2f-%.2fi\n", 0 / 2 / a, sqrt(-delta) / 2 / a, 0 / 2 / a, sqrt(-delta) / 2 / a);
}
else
{
printf("%.2f+%.2fi\n%.2f-%.2fi\n", -b / 2 / a, sqrt(-delta) / 2 / a, -b / 2 / a, sqrt(-delta) / 2 / a);
}
//printf("%.2f+%.2fi\n%.2f-%.2fi\n", (b == 0 ? b : -b) / 2 / a, sqrt(-delta) / 2 / a, (b == 0 ? b : -b) / 2 / a, sqrt(-delta) / 2 / a);
}
}
return 0;
}