一元二次方程!!!!c++的收藏起来哈!!
//*********************************************
//RootConstructor.cpp
#include <iostream>
#include <cmath>
using namespace std;
class RootConstructor
{
private:
int a,b,c;
double x1,x2;
public:
RootConstructor(int i,int j,int k)
{
a=i; b=j; c=k;
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
}
void disp()
{
cout<<"您输入的方程是:\n";
cout<<a<<"x^2";
if (b>0)
cout <<"+" <<b <<"x";
else
cout<<b <<"x";
if (c>0)
cout<<"+"<<c<<"=0";
else
cout<<c<<"=0";
cout<<"\n下面是方程的根:\n";
cout <<"x1=" <<x1 <<endl;
cout <<"x2=" <<x2 <<endl;
}
};
int main()
{
RootConstructor objS(1,-5,6);
objS.disp();
return 0;
}
//*********************************************
[解决办法]
- C/C++ code
//*********************************************//RootConstructor.cpp#include <iostream>#include <cmath>using namespace std;class RootConstructor {private: double a,b,c; double x1,x2;public: RootConstructor(double i,double j,double k) { a=i; b=j; c=k; if (b*b-4*a*c<0.000001) { x1=0.0; x2=0.0; } else { x1=(-b+sqrt(b*b-4.0*a*c))/(2.0*a); x2=(-b-sqrt(b*b-4.0*a*c))/(2.0*a); } } void roots() { cout<<"您输入的方程是:\n"; cout<<a<<"x^2"; if (b<-0.000001) cout<< b <<"x"; else if (0.000001<b) cout<< "+"<< b <<"x"; if (c<-0.000001) cout<< c <<"=0"; else if (0.000001<c) cout<< "+"<< c <<"=0"; if (x1==0.0 && x2==0.0) { cout<<"\n此方程无解。\n"; } else { cout<<"\n下面是方程的根:\n"; cout <<"x1=" <<x1 <<endl; cout <<"x2=" <<x2 <<endl; } }};int main() { RootConstructor equation(1.0,-5.0,6.0); equation.roots(); return 0;}//*********************************************
[解决办法]