用c语言求一元二次方程,若bb-4ac小于零用含i的式子表示,输入a,b,c,输出根
时间: 2024-11-06 11:16:16 浏览: 44
在C语言中,计算一元二次方程的根通常涉及到判别式的判断,即`b² - 4ac`。如果这个值小于0,这意味着原方程没有实数解,而是有两个共轭复数根,可以用下面的公式表示:
设方程一般形式为 `ax² + bx + c = 0`,其根可以通过求解求根公式得到:
\[ x = \frac{-b ± √{b² - 4ac}}{2a} \]
当`b² - 4ac < 0` 时,我们采用虚部表示:
\[ x = \frac{-b}{2a} ± i\sqrt{\frac{4ac - b²}{4a^2}} \]
其中 `i` 是虚数单位,满足 `i² = -1`。
下面是简单的C程序示例,用于计算并以复数形式表示这样的根:
```c
#include <stdio.h>
#include <math.h>
void complex_roots(double a, double b, double c) {
double discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
double real_part = -b / (2 * a);
double imaginary_part = sqrt(-discriminant) / (2 * a);
printf("Roots are complex conjugates: %.2f + %.2fi and %.2f - %.2fi\n", real_part, imaginary_part, real_part, imaginary_part);
} else {
// 实数根处理(这里仅给出一般思路,需根据实际情况添加)
double root1 = (-b + sqrt(discriminant)) / (2 * a);
double root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are: %.2f and %.2f\n", root1, root2);
}
}
int main() {
double a, b, c;
printf("Enter coefficients a, b, c for the quadratic equation: ");
scanf("%lf %lf %lf", &a, &b, &c);
complex_roots(a, b, c);
return 0;
}
```
阅读全文
相关推荐














