C语言求根

来源:百度知道 编辑:UC知道 时间:2024/05/26 10:57:23
求一元二次方程的根,要考虑到所有的情况

先判断b^2-4ac
再用求根公式搞定

#include <stdio.h>
#include <math.h>
void main()
{
float a,b,c,x1,x2,judge;
printf("please input three coefficient:");
scanf("%f,%f,%f",&a,&b,&c);
if(a==0)
printf("not one yuan quadratic equation");
else
{
judge=b*b-4*a*c;
if(judge<0)
printf("no roots");
else if (judge==0)
printf("x1=x2=%f",(-b)/(2*a));
else printf("x1=%f,x2=%f",((-b)+sqrt(b*b-4*a*c))/(2*a),((-b)+sqrt(b*b-4*a*c))/(2*a));
}
}