好友
阅读权限10
听众
最后登录1970-1-1
|
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c;
double discriminant, x1, x2;
printf("请输入方程的三个系数(a, b, c):");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2 * a);
x2 = (-b - sqrt(discriminant)) / (2 * a);
printf("方程的解为:x1 = %.2lf, x2 = %.2lf", x1, x2);
} else if (discriminant == 0) {
x1 = -b / (2 * a);
printf("方程的解为:x1 = x2 = %.2lf", x1);
} else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("方程的解为:x1 = %.2lf + %.2lfi, x2 = %.2lf - %.2lfi", realPart, imaginaryPart, realPart, imaginaryPart);
}
return 0;
} |
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|