08/10/13 22:17:21
>>258#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, d, x;
printf("a^2 * x + b * x + c = 0となるa, b, cを入力してください\n");
scanf("%lf %lf %lf", &a ,&b, &c);
if (a == 0) {
puts("2次方程式ではありません");
return 1;
}
b /= a;
if (c == 0) {
printf("x = %g, 0\n", -b);
} else {
c /= a;
b /= 2; /* x^2 + 2b'x + c = 0の形へ */
d = b * b - c; /* 判別式 */
if (d > 0) {
if (b > 0) {
x = -b - sqrt(d);
} else {
x = -b + sqrt(d);
}
printf("x = %g, %g\n", x, c / x);
} else if (d < 0) {
printf("x = %g ± %gi\n", -b, sqrt(-d));
} else {
printf("x = %g\n", -b);
}
}
return 0;
}