C语言,求一元二次方程的根,求解答!
1个回答
展开全部
#include<stdio.h>
#include<math.h>
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<math.h>
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;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询