#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define EPS 1e-6
int main ( )
{
float a,b,c,disc,p,q;
printf("plase enter the coefficients a,b,c");
scanf("%f,%f,%f",&a,&b,&c);
if (fabs(a)<=EPS)//判断a=0
{
printf("it is not a quadratic equation!\n");
exit(0);//必须和#include<stdlib.h>连用
}
disc=b*b-4*a*c;
p=-b/(2*a);
q=sqrt(fabs(disc))/(2*a);//fabs()为取绝对值,sqrt()为取平方根
if (fabs(disc)<=EPS)//float为实数型,必须和实型作比较,不能写成(0==disc)
{printf("x1=x2=%.2f\n",p);
}
else
{
if (disc>EPS)
{
printf("x1=%.2f,x2=%.2f",p+q,p-q);
}
else
{printf("x1=%.2f+%.2fi",p,q);//输出一对共轭复根
printf("x2=%.2f-%.2fi",p,q);
}
}
return 0;}
计算二元一次方程的根
最新推荐文章于 2024-07-01 17:59:29 发布