#include<stdio.h>
#include<math.h>
int main()
{
float a, b, c, j;
printf("请分别输入二元一次方程的三个系数:\n");
scanf("%f%f%f", &a, &b, &c);
j = (b * b) - (4 * a * c);
if (a == 0 && b == 0 && c == 0)
printf("x1有无限个取值,x2也有无限个取值\n");
else if (a == 0 && b == 0 && c != 0)
printf("无解\n");
else if (a == 0 && b != 0 && c != 0)
printf("二元一次方程变为一元一次方程,有一个解,x1=x2=%.2f\n", -(c / b));
else if (a != 0 && b != 0 && c == 0)
{
if (j >= 0)
{
printf("方程有两个解,其中一个解是0,另一个解是%.2f\n", -(b / a));
}
else
printf("无解\n");
}
else if (a != 0 && b != 0 && c != 0)
{
if (j > 0)
printf("方程有两个解,分别为x1=%.2f,x2=%.2f\n", (-b + sqrt(j))/(2*a),( -b - sqrt(j))/(2*a));
else if (j == 0)
printf("方程有两个相同的解,x1=x2=%.2f\n", (-b + sqrt(j))/(2*a));
else
{
printf("方程没有实根,但是有虚根\n");
printf("x1=%.2f+%.2fi\n", -b / (2 * a), sqrt(-j) / (2 * a));
printf("x2=%.2f-%.2fi\n", -b / (2 * a), sqrt(-j) / (2 * a));
}
}
return 0;
}
编程求ax2+bx+c=0的解
最新推荐文章于 2023-08-01 21:40:57 发布