一元二次函数的计算
#include<stdio.h><span style="white-space:pre"> </span>//程序中要调用求平方根
#include<math.h>
#define EXP 0.00000000001<span style="white-space:pre"> </span>//判断是否等于零
int main()
{
double a;
double b;
double c;
double disc;<span style="white-space:pre"> </span>//disc是判别式sqrt(b*b-4*a*c)
printf("请分别输入a,b,c的系数\n");<span style="white-space:pre"> </span>
scanf("%lf %lf %lf",&a,&b,&c);<span style="white-space:pre"> </span>//输入双精度浮点型变量的值要用格式声明"%lf"
disc = sqrt(b*b-4*a*c);
if(a>= -EXP&&a<= EXP){ //a=0时不是一元二次方程
printf("不是一元二次方程");
}
else{
if((disc >= -EXP)&&(disc <= EXP)){ //disc=0时
double x;
printf("方程只有一个实数根:X=%lf",-(b)/(2*a));
}
if(disc>0.0){ //disc>o时
double x1,x2;
x1 = (-b+disc)/(2*a);
x2 = (-b-disc)/(2*a);
printf("x1 = %lf\nx2 = %lf",x1,x2);
}
if(disc<0.0){ //disc<o时
printf("有两个共轭复数根");
}
}
return 0;
}