公式都给出了,根据bb-4a*c的值,分别计算出方程的解。
#include <stdio.h>
#include <math.h>
void main() {
float a, b, c, e, x1, x2;
printf(" 请输入二元一次方程 ax2+bx+c=0中a,b,c的值\n");
scanf("%f%f%f", &a, &b, &c);
e = b * b - 4 * a*c;
if (a == 0 && b != 0) {
x1 = -(c / b);
printf("x=%.5f\n", x1);
}
else if (e == 0) {
x1 = -(b / (2 * a));
printf("x=%.5f\n", x1);
}
else if (e>0) {
e = (float)pow(e, 0.5);
x1 = (-b + e) / (2 * a); x2 = (-b - e) / (2 * a);
printf("x1=%.5f;x2=%.5f\n", x1, x2);
}
else if (e<0) {
e = (float)pow(-1 * e, 0.5);
x1 = -b / (2 * a); x2 = e / (2 * a);
if (x1 == 0) {
printf("x1=%.5fi;x2=-%.5fi\n", x2, x2);
}
else {
printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi\n", x1, x2, x1, x2);
}
}
else if (a == 0 && b == 0&&c==0) {
printf("无意义\n");
}
else
{
printf("错误\n");
}
}