用C语言编写程序实现一元二次方程的解?
3个回答
2009-11-14
展开全部
#include <stdio.h>
#include <math.h>
void main ()
{
double sqrt(double x);
int a,b,c;
double x1,x2,x,e,d,g,f;
scanf("%d %d %d",&a,&b,&c);
d=b*b-4*a*c;
if (a==0)
{ if (b!=0)
{x=-(double)c/(double)b;
if (x==0)
{x=-x;
printf("x=%.6lf\n",x);}
else
printf("x=%.6lf\n",x);
}
else
printf("Input error!\n");}
else if (d<0)
{d=-d;
e=sqrt(d);
g=-(double)b/(2*(double)a);
f=e/(2*a);
if (g!=0)
printf("x1=%.6lf+%.6lfi\nx2=%.6lf-%.6lfi\n",g,f,g,f);
else
printf("x1=%.6lfi\nx2=-%.6lfi\n",f,f);}
else if (d==0)
{ x1=x2=-b/(2*a);
printf("x1=x2=%.6lf\n",x1);}
else
{ e=sqrt(d);
x1=(-b+e)/(2*a);
x2=(-b-e)/(2*a);
printf("x1=%.6lf\nx2=%.6lf\n",x1,x2);}
}
这个可以,但也许不够简便
#include <math.h>
void main ()
{
double sqrt(double x);
int a,b,c;
double x1,x2,x,e,d,g,f;
scanf("%d %d %d",&a,&b,&c);
d=b*b-4*a*c;
if (a==0)
{ if (b!=0)
{x=-(double)c/(double)b;
if (x==0)
{x=-x;
printf("x=%.6lf\n",x);}
else
printf("x=%.6lf\n",x);
}
else
printf("Input error!\n");}
else if (d<0)
{d=-d;
e=sqrt(d);
g=-(double)b/(2*(double)a);
f=e/(2*a);
if (g!=0)
printf("x1=%.6lf+%.6lfi\nx2=%.6lf-%.6lfi\n",g,f,g,f);
else
printf("x1=%.6lfi\nx2=-%.6lfi\n",f,f);}
else if (d==0)
{ x1=x2=-b/(2*a);
printf("x1=x2=%.6lf\n",x1);}
else
{ e=sqrt(d);
x1=(-b+e)/(2*a);
x2=(-b-e)/(2*a);
printf("x1=%.6lf\nx2=%.6lf\n",x1,x2);}
}
这个可以,但也许不够简便
展开全部
#include <math.h>
main()
{
int z=0;
while(z==0)
{
float a,b,c,disc,x1,x2;
printf("input a,b,c:");
scanf("%f,%f,%f",&a,&b,&c);
disc=b*b-4*a*c;
if(disc>=0)z=1;
}
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("\nx1=%6.2f x2=%6.2f\n",x1,x2);
}
我高数学的不好,当b*b-4*a*c<0时的解我不会求~~~~只能让重输..
main()
{
int z=0;
while(z==0)
{
float a,b,c,disc,x1,x2;
printf("input a,b,c:");
scanf("%f,%f,%f",&a,&b,&c);
disc=b*b-4*a*c;
if(disc>=0)z=1;
}
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("\nx1=%6.2f x2=%6.2f\n",x1,x2);
}
我高数学的不好,当b*b-4*a*c<0时的解我不会求~~~~只能让重输..
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
# include<iostream.h>
# include<math.h>
void main(void)
{
double a,b,c,x1,x2;
cout<<"input a,b,c:";
cin>>a>>b>>c;
double s=b*b-4*a*c;
if(s>=o)
{
double sqrtVal=sqrt(s);
x1=(-b+sqrtVal)/(2*a);
x2=(-b-sqrtVal)/(2*a);
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl
}
else
cout<<"this question does not hava a real answer"<<endl;
}
这个是c++代码希望对你有所帮助!
# include<math.h>
void main(void)
{
double a,b,c,x1,x2;
cout<<"input a,b,c:";
cin>>a>>b>>c;
double s=b*b-4*a*c;
if(s>=o)
{
double sqrtVal=sqrt(s);
x1=(-b+sqrtVal)/(2*a);
x2=(-b-sqrtVal)/(2*a);
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl
}
else
cout<<"this question does not hava a real answer"<<endl;
}
这个是c++代码希望对你有所帮助!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询