求一元二次方程的根

程序流程图:

代码:

 #include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,s,x1,x2;
printf("please in put a b c:\n");
scanf("%f,%f,%f",&a,&b,&c);
if(a>=-(1e-6) && a<(1e-6))
printf("sorry!you have a rong number \n");
else
{
s=b*b-4*a*c;
if(s>(1e-6))
{
x1=(-b+sqrt(s))/(2*a);
x2=(-b-sqrt(s))/(2*a);
printf("there are two different real:\nx1=%f\tx2=%f\t",x1,x2);
}
else if(s>=-(1e-6) && s<=(1e-6))
{
x1=x2=-b/(2*a);
printf("there are two equal real :\n",x1);
}
else
{
s=-s;
x1=-b/(2*a);
x2=fabs(sqrt(s)/(2*a));
printf("there are two complex:\n");
printf("x1=%f+%fi,x2=%f+%fi\n",x1,x2,x1,x2);}

结果:

更多推荐

c语言学习-求一元二次方程的根