c语言求解一元二次方程

能帮忙看看错哪了吗orz
谢谢!

题目:求ax^2+bx+c=0的根。分别考虑d=b^2-4ac大于零,等于零和小于零三种情况。
a、b、c要求是浮点型。程序要对a、b、c的各种情况进行处理。如判断a是否为0,b^2-4ac分别为大于0、小于0、等于0。

输入3个浮点数,代表a,b,c。输出对应方程的根:
当该方程非一元二次方程时,输出“The equation is not quadratic.”。
当该一元二次方程有两个相等的实数根时,输出“The equation has two equal roots: xx.”。
当该一元二次方程有两个不相等的实数根时,输出“The equation has two distinct real roots: x1 and x2.”。
当该一元二次方程有两个不相等的虚数根时,输出“The equation has two complex roots: x1 and x2.”。
所有的实数均保留4位有效数字。
注意:1、运算中用到的所有数据要求用float型存储。
2、当有两个不同的根时,解为实数时先输出大的那个根,解为复数时先输出虚部为正的那个根。

#include <stdio.h>
#include <math.h>

int main()
{
float a,b,c,d,e,x1,x2,x3,x4;
scanf("%f%f%f",&a,&b,&c);
d=b * b - 4 * a * c;
e=fabs(sqrt(-d)/(2 * a));
x1=(-b + sqrt(d))/(2 * a);
x2=(-b - sqrt(d))/(2 * a);
x3=-b /(2 * a);
x4=-b /(2 * a);

if (fabs(a) <= 1e-6)
{
printf("The equation is not quadratic.");
}
else if (d > 0 && a != 0)
{
printf("The equation has two distinct real roots: %.4f and %.4f.",x1,x2);
}
else if (fabs(d) <= 1e-6 && a != 0)
{
printf("The equation has two equal roots: %.4f.",x1);
}
else
{
printf("The equation has two complex roots: %.4f+%.4fi and %.4f-%.4fi.",x3,e,x4,e);
}

return 0;
}

第1个回答  2019-10-10
1、本题要先判断a,如果a=0,则不是一元二次方程。
2、首先要判断d是否小于0,则只能有虚数解,d小于0时,就不能去开平方,否则会出错。
3、按照以上思路重新修改你的程序。本回答被网友采纳
第2个回答  2019-10-09
你这程序不是能跑呢麻
相似回答