C语言 编写程序 从键盘输入一元二次方程的三个参数(实数),计算并输出该方程的两个根
展开全部
我也刚学C,费了好几个小时,终于把这个问题搞定了!
已经运行过了,结果跟谭版结果一样,敬请放心使用。
#include "stdio.h"
#include "math.h"
void main()
{ double a,b,c,x1,x2,disc,p,q;
printf("please input a,b,c:\n");
scanf("%lf,%lf,%lf",&a,&b,&c);
if (fabs(a)<1e-6)
printf("The equation is not a quadratic");
else
{ disc=b*b-4*a*c;
if (disc<0)
{
p=-b/(2*a);
q=sqrt(-disc)/(2*a);
printf("The equation has two complex roots:\n");
printf ("%8.4f+%8.4f i\n",p,q);
printf ("%8.4f-%8.4f i\n",p,q);
}
else
if (fabs(disc)<1e-6)
printf("the equation has two equal roots:%8.4f\n",-b/(2*a));
else
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("The equation has distinct real roots:%8.4f and %8.4f\n",x1,x2);
}
}
}
已经运行过了,结果跟谭版结果一样,敬请放心使用。
#include "stdio.h"
#include "math.h"
void main()
{ double a,b,c,x1,x2,disc,p,q;
printf("please input a,b,c:\n");
scanf("%lf,%lf,%lf",&a,&b,&c);
if (fabs(a)<1e-6)
printf("The equation is not a quadratic");
else
{ disc=b*b-4*a*c;
if (disc<0)
{
p=-b/(2*a);
q=sqrt(-disc)/(2*a);
printf("The equation has two complex roots:\n");
printf ("%8.4f+%8.4f i\n",p,q);
printf ("%8.4f-%8.4f i\n",p,q);
}
else
if (fabs(disc)<1e-6)
printf("the equation has two equal roots:%8.4f\n",-b/(2*a));
else
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("The equation has distinct real roots:%8.4f and %8.4f\n",x1,x2);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询