# include <stdio.h>
# include <math.h>
float x1, x2, p, q;
void smaller_than_zero(float a, float b, float disc)
{
p = -b /(2 * a);
q = sqrt(-disc) / (2 * a);
}
void equal_to_zero(float a, float b, float disc)
{
x1=x2=(-b) / (2 * a);
}
void greater_than_zero(float a, float b, float disc)
{
x1=(-b + sqrt(disc)) / (2 * a);
x2=(-b - sqrt(disc)) / (2 * a);
}
int main(void)
{
float a, b, c;
printf("请输入一元二次方程的三个系数\n");
scanf("%f%f%f", &a, &b, &c);
while(a=0)
{
printf("这不是一个一元二次方程\n");
printf("请输入一元二次方程的三个系数\n");
scanf("%f%f%f", &a, &b, &c);
}
float disc;
disc = b * b - 4 * a * c;
if(disc < 0)
{
smaller_than_zero(a, b, disc);
printf("x1=%f+%fi, x2=%f-%fj", p, q, p, q);
}
else if(disc =+ 0)
{
equal_to_zero(a, b, disc);
printf("x1=x2=%f", x1);
}
else
{
greater_than_zero(a, b, disc);
printf("x1=%f, x2=%f", x1, x2);
}
return 0;
}
输出结果是
请输入一元二次方程的三个系数
2 4 1
x1=-1.#INF00, x2=-1.#INF00
第28行:while(a == 0) //while(a=0)
第42行:else if(disc == 0)//if(disc =+ 0)