紧急求救,如何用建立的复数类求一元二次方程的根?????????
复数类如下:
#include<iostream.h>
#include <cmath>
// class
class complex
{
private:
double a,b;
double _copy;
public:
double getreal();
double getimag();
double getZ();
void solution(double a,double b,double c,double p,complex &x1,complex &x2);
complex(int x)
{
a=x;b=0;
}
complex(double av,double bv):a(av),b(bv)
{
}
complex(){}
complex(complex& _copy)
{
a=_copy.a;
b=_copy.b;
}
friend ostream& operator<<(ostream& i,complex& j);
friend istream& operator>>(istream& g,complex& i);
friend complex operator+(complex& x,complex& y);
friend complex operator-(complex& x,complex& y);
friend complex operator*(complex& x,complex& y);
friend complex operator/(complex& x,complex& y);
complex operator=(complex& y);
void output()
{
cout<<"z="<<sqrt((a*a)+(b*b))<<endl;
}
void put()
{
cout<<"("<<a<<"+"<<b<<"i"<<")"<<endl;
}
void gen()
{int sign=1, double g,h;
if(b<0)
sign=-1;
else
sign=1;
g=sqrt((sqrt((a*a)+(b*b))+a)/2);
h=sign*sqrt((sqrt((a*a)+(b*b))-a)/2);
cout<<"the sqrt-root is: "<<g<<"+"<<h<<"i"<<endl;
}
};
ostream& operator<<(ostream& stream ,complex& x)
{
x.put();
return stream;
}
//input
istream& operator>>(istream& stream,complex& x)
{
cout<<"\treal=";
cin>>x.a;
cout<<"\timag=";
cin>>x.b;
return stream;
}
//add
complex operator+(complex& x,complex& y)
{
return complex(x.a+y.a,x.b+y.b);
}
//sub
complex operator-(complex& x,complex& y)
{
return complex(x.a-y.a,x.b-y.b);
}
//mul
complex operator*(complex& x,complex &y)
{
return complex((x.a*y.a)-(x.b*y.b),(x.a*y.b)+(x.b*y.a));
}
//div
complex operator/(complex& x,complex& y)
{
return complex(((x.a*y.a+x.b*y.b))/(y.a*y.a+y.b*y.b),(x.b*y.a-x.a*y.b)/(y.a*y.a+y.b*y.b));
}
//assignment
complex complex::operator=(complex& y)
{
this->a=y.a;
this->b=y.b;
return *this;
}
double complex::getreal()
{
return a;
}
double complex::getimag()
{
return b;
}
double complex::getZ()
{
return sqrt(a*a+b*b);
}
void main()
{
//class complex
complex p,q;
cin>>p;
cout<<p<<endl;
p.output();
p.gen();
cin>>q;
cout<<q<<endl;
q.output();
q.gen();
cout<<"copy constructor"<<endl;
complex _copy;
_copy=p;
_copy.put();
complex div,mul,sub,add;
cout<<"add a+b"<<endl;
add=q+p;
add.put();
cout<<"sub a-b"<<endl;
sub=q-p;
sub.put();
cout<<"mul a*b"<<endl;
mul=p*q;
mul.put();
cout<<"div a/b"<<endl;
div=q/p;
div.put();
}
求高手建一个求一元二次方程的函数