求解一元二次方程复数解的C语言代码
时间: 2023-11-16 10:53:45 浏览: 207
以下是求解一元二次方程复数解的C语言代码:
```c
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, delta, x1_real, x1_imag, x2_real, x2_imag;
printf("请输入一元二次方程的系数:\n");
printf("a = ");
scanf("%lf", &a);
printf("b = ");
scanf("%lf", &b);
printf("c = ");
scanf("%lf", &c);
delta = b * b - 4 * a * c;
if (delta >= 0)
{
x1_real = (-b + sqrt(delta)) / (2 * a);
x2_real = (-b - sqrt(delta)) / (2 * a);
printf("方程的实数解为:x1 = %lf, x2 = %lf\n", x1_real, x2_real);
}
else
{
x1_real = -b / (2 * a); x1_imag = sqrt(-delta) / (2 * a);
x2_real = -b / (2 * a);
x2_imag = -sqrt(-delta) / (2 * a);
printf("方程的复数解为:x1 = %lf + %lfi, x2 = %lf + %lfi\n", x1_real, x1_imag, x2_real, x2_imag);
}
return 0;
}
```
阅读全文