#include <stdio.h> #include <math.h> int main() { double a = 0;//初始化 double b = 0; double c = 0; while (scanf("%lf %lf %lf", &a, &b, &c) != EOF) //多组输入 { double w = b * b - 4 * a * c; //判别式公式 double p1 = (-b+0) - sqrt(w);//+0防止精度误差 double p2 = (-b+0) + sqrt(w); double xa = p1 / (2 * a);//x1 double xb = p2 / (2 * a);//x2 if (a == 0)//a=0 { printf("Not quadratic equation\n"); } else if (a != 0) { if (w == 0.0) { if (a > 0)//p1精度出现误差 { printf("x1=x2=%.2lf\n", xa); } if (a < 0)//xa,xb精度出现误差 { printf("x1=x2=%.2lf\n", (-b)/(2*a)); } } if (w > 0.0) { printf("x1=%.2f;x2=%.2f\n", xa, xb); } if (w < 0.0) { float actual = -b / (2 * a); float virtual = sqrt(-w) / (2 * a); printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n", actual, virtual, actual, virtual); } } } return 0; }