C语言 求解ax^2+bx+2=0

如题所述

#include<stdio.h>
#include
<math.h>
main()
{float
a,b,c,d,disc,x1,x2,realpart,imagpart;
scanf("%f,%f,%f",&a,&b,&c);
输入系数
printf("the
equation");
屏幕打印“the
equation”
if(fabs(a)
<=le-6)-------------------1e-6是10的-6次方的意思,a为0,则为一次方程
printf("is
not
a
quaratic");
//屏幕打印“is
not
a
quaratic”,即如果上面条件成立,屏幕上出现
//the
equation
is
not
a
quaratic;
else
{disc=b*b-4*a*c;
//根的判别式b^2-4ac
if(fabs(disc)
<=le-6
//如果等于0(接近0,因为float型的数值不会严格相等);
printf("has
tow
equal
roots:%8.4\n",-b/(*a));
//屏幕打印"has
tow
equal
roots
:"和(-b/a)的值;四位小数
else
if(disc>le-6)
//如果判别式值大于0,则
{x1=(-b+sqrt(disc))/(2*a);
//根据公式算出x1,x2;
{x2=(-b-sqrt(disc))/(2*a);
printf("has
distinct
real
roots:%8.4f
and%8.4f\n",x1,x2);//屏幕打印“has
distinct
real
roots”,"x1,x2
//的值"格式同上,
//接下来应该判别disc小于0的情况;
if
(disc<le-6)
{printf("has
no
real
roots");
realpart=-b/(2*a);
//实根
imagpart=sqrt(-disc)/(2*a);//虚根的模
printf("but
tow
complex
roots");
printf("the
real
part
is
%8.4f
and
the
imagpart
is
%8.4f\n
i",realpart,imagpart);//第一个根的实部和虚部
printf("the
real
part
is
%8.4f
and
the
imagpart
is
-
%8.4f\n
i",realpart,imagpart);//第二个根的实部和虚部
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-04-02
fabs(a)
<=le-6:a的绝对值小于或等于10的-6次方。
我是这样理解其用意的:这个判断应该是要保证a的绝对值不会太小,因为假如a的绝对值太小的话,则可以认为a约等于0,而a=0则方程就不是二次方程了。所以我猜那个输出(the
equation
is
not
a
quaratic)的意思应该是说这个方程不是二次方程。
相似回答