C语言从键盘任意输入a,b,c的值,编程计算并输出一元二次方程ax2+bx+c=0的根,当a=0时,输出"It is not a quadratic equation!\n",当a≠0时,分b2−4ac>0、b2−4ac=0、b2−4ac<0三种情况计算并输出方程的根。 提示:公式为: **输入格式要求:"%f,%f,%f" 提示信息:"Please enter the coefficients a,b,c:" **输出格式要求:"It is not a quadratic equation!\n" "x1 = x2 = %.2f\n" "x1 = %.2f, x2 = %.2f\n" "x1 = %.2f+%.2fi, " "x2 = %.2f-%.2fi\n"
时间: 2024-02-21 19:57:14 浏览: 132
好的,以下是您需要的C语言代码:
```c
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, delta, x1, x2, realPart, imagPart;
printf("Please enter the coefficients a,b,c:");
scanf("%f,%f,%f", &a, &b, &c);
if (a == 0)
{
printf("It is not a quadratic equation!\n");
}
else
{
delta = b * b - 4 * a * c;
if (delta > 0)
{
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
printf("x1 = %.2f, x2 = %.2f\n", x1, x2);
}
else if (delta == 0)
{
x1 = x2 = -b / (2 * a);
printf("x1 = x2 = %.2f\n", x1);
}
else
{
realPart = -b / (2 * a);
imagPart = sqrt(-delta) / (2 * a);
printf("x1 = %.2f+%.2fi, x2 = %.2f-%.2fi\n", realPart, imagPart, realPart, imagPart);
}
}
return 0;
}
```
希望能够帮到您!
阅读全文