
1个回答
展开全部
#include<iostream.h>
#include<math.h>
class Equation{
float a,b,c;
public:
float x[2];
Equation(float a1=0.0,float b1=0.0,float c1=0.0)
{
a=a1;
b=b1;
c=c1;
}
~Equation(){}
long CalResult();
};
long Equation::CalResult()
{
long temp;
if ((b*b-4*a*c)==0)
{
temp=1;
x[0]=(float)(-(b/2*a));
return temp;
}
if ((b*b-4*a*c)>0)
{
temp=2;
x[0]=(-b+sqrt(b*b-4*a*c))/(2*a);
x[1]=(-b-sqrt(b*b-4*a*c))/(2*a);
return temp;
}
else
{
temp=0;
}
return temp;
}
int main()
{
float a,b,c;
char ch;
do{
cout<<"请输入一元二次方程系数"<<endl;
cin>>a>>b>>c;
Equation *obj=new Equation(a,b,c);
cout<<"方程有"<<obj->CalResult()<<"个根"<<endl;
if (obj->CalResult()==1)
{
cout<<"X="<<obj->x[0]<<endl;
}
if (obj->CalResult()==2)
{
cout<<"X1="<<obj->x[0]<<endl;
cout<<"X2="<<obj->x[1]<<endl;
}
delete obj;
cout<<"是否继续计算?(1)"<<endl;
cin>>ch;
}while(ch=='1');
return 0;
}
帮你找的 。。自己也学习
#include<math.h>
class Equation{
float a,b,c;
public:
float x[2];
Equation(float a1=0.0,float b1=0.0,float c1=0.0)
{
a=a1;
b=b1;
c=c1;
}
~Equation(){}
long CalResult();
};
long Equation::CalResult()
{
long temp;
if ((b*b-4*a*c)==0)
{
temp=1;
x[0]=(float)(-(b/2*a));
return temp;
}
if ((b*b-4*a*c)>0)
{
temp=2;
x[0]=(-b+sqrt(b*b-4*a*c))/(2*a);
x[1]=(-b-sqrt(b*b-4*a*c))/(2*a);
return temp;
}
else
{
temp=0;
}
return temp;
}
int main()
{
float a,b,c;
char ch;
do{
cout<<"请输入一元二次方程系数"<<endl;
cin>>a>>b>>c;
Equation *obj=new Equation(a,b,c);
cout<<"方程有"<<obj->CalResult()<<"个根"<<endl;
if (obj->CalResult()==1)
{
cout<<"X="<<obj->x[0]<<endl;
}
if (obj->CalResult()==2)
{
cout<<"X1="<<obj->x[0]<<endl;
cout<<"X2="<<obj->x[1]<<endl;
}
delete obj;
cout<<"是否继续计算?(1)"<<endl;
cin>>ch;
}while(ch=='1');
return 0;
}
帮你找的 。。自己也学习
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询