这个一元二次方程为什么答案错误
程序代码:
#include<stdio.h> #include<math.h> int main() { int solve(float,float,float); float a,b,c,f; int black; scanf("%f %f %f",&a,&b,&c); f = b * b - 4 * a * c; black = solve(a,b,c); if(black == 0) printf("无解"); if(black == 1){ if(a != 0 && b != 0) printf("x1 = x2 = %g",(-b)/(2 * a)); if(a == 0) printf("x = %g",(-c)/b); if(b == 0){ if((-c)/a < 0) printf("x = -%g",sqrt(c/a)); else printf("x = %g",sqrt((-c)/a)); } } if(black == 2){ if(f > 0) printf("x1 = %g, x2 = %g",((-b) + sqrt(f))/(2*a),((-b) - sqrt(f))/(2*a)); if(f < 0) if(a < 0) printf("x1 = %g%gi, x2 = %g+%gi",(-b)/(2*a),sqrt(-f)/(2*a),(-b)/(2*a),-sqrt(-f)/(2*a)); else printf("x1 = %g%gi, x2 = %g+%gi",(-b)/(2*a),sqrt(-f)/(2*a),(-b)/(2*a),-sqrt(-f)/(2*a)); } if(black == 3) printf("无穷多解"); return 0; } int solve(float a,float b,float c) { float flag; flag = b * b - 4 * a * c; if(((a == 0 && b != 0) || (a != 0 && b == 0)) && c != 0) return 1; if(a == 0 && b == 0 && c != 0) return 0; if((a == 0 || b == 0) && c == 0) return 3; if(flag > 0 && flag < 1E-4) return 1; if(flag == 0) return 1; if(flag > 0) return 2; if(flag < 0) return 2; if(c == 0 && (a != 0 || b != 0)) return 3; }