作业帮 > 综合 > 作业

如何用C++求解方程ax^2+bx+c=0的解

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/23 12:39:00
如何用C++求解方程ax^2+bx+c=0的解
#include
#include
class Equation{
\x05float a,b,c;
\x05
public:
\x05float x[2];
\x05Equation(float a1=0.0,float b1=0.0,float c1=0.0)
\x05{
\x05\x05a=a1;
\x05\x05b=b1;
\x05\x05c=c1;
\x05}
\x05~Equation(){}
\x05long CalResult();
\x05
};
long Equation::CalResult()
{
\x05long temp;
\x05if ((b*b-4*a*c)==0)
\x05{
\x05\x05temp=1;
\x05\x05x[0]=(float)(-(b/2*a));
\x05\x05return temp;
\x05}
\x05if ((b*b-4*a*c)>0)
\x05{
\x05\x05temp=2;
\x05\x05x[0]=(-b+sqrt(b*b-4*a*c))/(2*a);
x[1]=(-b-sqrt(b*b-4*a*c))/(2*a);
\x05\x05return temp;
\x05}
\x05else
\x05{
\x05\x05temp=0;
\x05}
\x05return temp;
}
int main()
{
\x05float a,b,c;
\x05char ch;
\x05do{
\x05\x05cout