C语言 解方程。。大家帮忙看看这个程序吧~~
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,d,x,y,x1,x2;
printf("Please input three float numbers:");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
{
printf("The equation is not quadratic.");
}
else
{
d=b*b-4*a*c;
if(d=0)
{
x=(-b)/(2*a);
printf("The equation has two equal roots:x");
}
else if(d>0)
{
y=sqrt(d);
x1=((-b)+y)/(2*a);
x2=((-b)-y)/(2*a);
printf("The equation has two distinct real roots:%f and %f.",x1,x2);
}
else if(d<0)
{
y=sqrt(d);
x1=((-b)+y)/(2*a);
x2=((-b)-y)/(2*a);
printf("The equation has two complex roots:%f and %f.",x1,x2);
}
}
system("PAUSE");
return 0;
} 展开
// 解一个一元二次方程
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,d,x,y,x1,x2;
while(1){
printf("\nPlease input three float numbers:");
scanf("%f,%f,%f",&a,&b,&c);
printf("The equation is: %f x*x + %f x + %f = 0\n",a,b,c);
if(a==0)
{
printf("The equation is not quadratic.");
}
else
{
d=b*b-4*a*c;
if(fabs(d)<1e-10) // if(d==0.0) //if(d=0)
{
x=(-b)/(2*a);
printf("The equation has two equal roots:%f\n",x);//printf("The equation has two equal roots:x");
}
else if(d>0)
{
y=sqrt(d);
x1=((-b)+y)/(2*a);
x2=((-b)-y)/(2*a);
printf("The equation has two distinct real roots:%f and %f.\n",x1,x2);
}
else if(d<0)
{
y=sqrt(-d);
x1=((-b)+0)/(2*a);
x2=y/(2*a);
printf("The equation has a pair of conjugate complex roots:%f +%f i and %f %f i .\n",x1,x2,x1,-x2);
}
}
}// system("PAUSE");
return 0;
}
结果:
Please input three float numbers:1.2,2.2,3.3
The equation is: 1.200000 x*x + 2.200000 x + 3.300000 = 0
The equation has a pair of conjugate complex roots:-0.916667 +1.381927 i and -0.916667 -1.381927 i .
Please input three float numbers:1,2.1,1
The equation is: 1.000000 x*x + 2.100000 x + 1.000000 = 0
The equation has two distinct real roots:-0.729844 and -1.370156.
Please input three float numbers:1.1,2.2,1.1
The equation is: 1.100000 x*x + 2.200000 x + 1.100000 = 0
The equation has two equal roots:-1.000000
Please input three float numbers:
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,d,x,y,x1,x2;
printf("Please input three float numbers:");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
{
printf("The equation is not quadratic.");
}
else
{
d=b*b-4*a*c;
if(d==0)//改成==
{
x=(-b)/(2*a);
printf("The equation has two equal roots:%f\n",x);// 少%f
}
else if(d>0)
{
y=sqrt(d);
x1=((-b)+y)/(2*a);
x2=((-b)-y)/(2*a);
printf("The equation has two distinct real roots:%f and %f\n",x1,x2);
}
else if(d<0)
{
y=sqrt(abs(d));//改成绝对值
x1=-b/(2*a);
x2=-b/(2*a);
printf("The equation has two complex roots:%f+%fi and %f-%fi\n",x1,y,x2,y);//少虚部
}
}
system("PAUSE");
return 0;
}
修改过后的,注释部分是修改的