C语言,求一元二次方程的根,求解答!
答案:1 悬赏:10 手机版
解决时间 2021-11-11 00:23
- 提问者网友:杀生予夺
- 2021-11-10 11:58
C语言,求一元二次方程的根,求解答!
最佳答案
- 五星知识达人网友:骨子里都是戏
- 2021-11-10 13:11
#include
#include
int main(void)
{
double a, b, c;
double x1, x2;
double delt;
if(scanf("%lf %lf %lf", &a, &b, &c) == 3)
{
if(a == 0)
{
if(b == 0 && c == 0)
printf("Zero Equation\n");
else if(b == 0 && c != 0)
printf("Not An Equation\n");
else
printf("%.2f\n", (- c/ b));
}
else
{
delt = pow(b,2) - 4*a*c;
if(delt > 0)
{
x1 = (-b + sqrt(delt)) / (2 * a);
x2 = (-b - sqrt(delt)) / (2 * a);
printf("%.2f\n", x1);
printf("%.2f\n", x2);
}
else if(delt == 0)
{
printf("%.2f\n", (-b / (2*a)));
}
else
{
x1 = - b / (2 * a);
x2 = sqrt(-delt) / (2 * a);
x2 = (x2 > 0) ? x2 : -x2;
if(b!=0)
{
printf("%.2f+%.2fi\n", x1, x2);
printf("%.2f-%.2fi\n", x1, x2);
}
else
{
printf("0.00+%.2fi\n",x2);
printf("0.00-%.2fi\n",x2);
}
}
}
}
return 0;
}
#include
int main(void)
{
double a, b, c;
double x1, x2;
double delt;
if(scanf("%lf %lf %lf", &a, &b, &c) == 3)
{
if(a == 0)
{
if(b == 0 && c == 0)
printf("Zero Equation\n");
else if(b == 0 && c != 0)
printf("Not An Equation\n");
else
printf("%.2f\n", (- c/ b));
}
else
{
delt = pow(b,2) - 4*a*c;
if(delt > 0)
{
x1 = (-b + sqrt(delt)) / (2 * a);
x2 = (-b - sqrt(delt)) / (2 * a);
printf("%.2f\n", x1);
printf("%.2f\n", x2);
}
else if(delt == 0)
{
printf("%.2f\n", (-b / (2*a)));
}
else
{
x1 = - b / (2 * a);
x2 = sqrt(-delt) / (2 * a);
x2 = (x2 > 0) ? x2 : -x2;
if(b!=0)
{
printf("%.2f+%.2fi\n", x1, x2);
printf("%.2f-%.2fi\n", x1, x2);
}
else
{
printf("0.00+%.2fi\n",x2);
printf("0.00-%.2fi\n",x2);
}
}
}
}
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯