1
0
mirror of https://github.com/renbaoshuo/202401-programming-assignments.git synced 2024-11-27 14:36:19 +00:00
202401-programming-assignments/【实践课外】2.选择结构1/7-7 求一元二次方程的根.c

45 lines
1.1 KiB
C
Raw Normal View History

2024-10-18 10:46:44 +00:00
#include <math.h>
#include <stdio.h>
double no_neg_zero(double x) {
return x == -0.0 ? 0.0 : x;
}
int main() {
double a, b, c;
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0 && b == 0) {
if (c == 0) {
printf("Zero Equation\n");
} else {
printf("Not An Equation\n");
}
} else if (a == 0) {
double x = -c / b;
printf("%.2lf\n", no_neg_zero(x));
} else {
double delta = b * b - 4.0 * a * c;
if (delta > 0) {
double x1 = (-b + sqrt(delta)) / (2 * a);
double x2 = (-b - sqrt(delta)) / (2 * a);
printf("%.2lf\n%.2lf\n", no_neg_zero(x1), no_neg_zero(x2));
} else if (delta == 0) {
double x = -b / (2 * a);
printf("%.2lf\n", no_neg_zero(x));
} else if (delta < 0) {
double real = -b / (2 * a);
double imag = sqrt(-delta) / (2 * a);
printf("%.2lf+%.2lfi\n%.2lf-%.2lfi\n", no_neg_zero(real), no_neg_zero(imag), no_neg_zero(real), no_neg_zero(imag));
}
}
return 0;
}