C语言 解方程。。大家帮忙看看这个程序吧~~
发布网友
发布时间:2024-04-18 04:55
我来回答
共1个回答
热心网友
时间:2024-04-23 12:19
// 解一个一元二次方程
#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: