c语言输入参数a,b,c,求一元二次方程ax2 +bx +c =0 的根。(带复根)
时间: 2023-12-10 14:41:57 浏览: 183
c代码-求ax平方+bx+c+=0
5星 · 资源好评率100%
好的,这里给出计算一元二次方程根的C语言函数代码:
```c
#include <stdio.h>
#include <math.h>
void quadratic_equation(double a, double b, double c) {
double delta = b * b - 4 * a * c;
if (delta > 0) {
double x1 = (-b + sqrt(delta)) / (2 * a);
double x2 = (-b - sqrt(delta)) / (2 * a);
printf("x1 = %lf, x2 = %lf\n", x1, x2);
} else if (delta == 0) {
double x = -b / (2 * a);
printf("x = %lf\n", x);
} else {
double real_part = -b / (2 * a);
double imaginary_part = sqrt(-delta) / (2 * a);
printf("x1 = %lf + %lfi, x2 = %lf - %lfi\n", real_part, imaginary_part, real_part, imaginary_part);
}
}
int main() {
double a, b, c;
printf("请输入一元二次方程的系数:\n");
printf("a = ");
scanf("%lf", &a);
printf("b = ");
scanf("%lf", &b);
printf("c = ");
scanf("%lf", &c);
quadratic_equation(a, b, c);
return 0;
}
```
输入参数a、b、c后,调用quadratic\_equation函数计算方程的根并输出。如果delta大于0,方程有两个不等实根;如果delta等于0,方程有一个实根;如果delta小于0,方程有两个共轭复根。
阅读全文