#include
#include
#include
using namespace std;
class complex
{
private:
double real;
double unreal;
public:
complex(void):real(0),unreal(0){};
complex(double v_real,double v_unreal):real(v_real),unreal(v_unreal){};
complex(const complex & comx){
this->real = comx.get_real();
this->unreal = comx.get_unreal();
};
bool operator==(const complex& comx){
if(this->real == comx.real && this->unreal == comx.unreal){
return true;
}
return false;
};
//加法相关运算 (a+bi)+(c+di)=(a+c)+(b+d)i.
complex operator+(const complex& comx) {
complex result((this->get_real()+comx.get_real()),(this->get_unreal()+comx.get_unreal()));
return result;
};
complex operator+(const double a) {
complex tmp(a,0);
return (*this)+tmp;
};
void operator+=(const complex& comx) {
*this = (*this)+comx;
};
void operator+=(const double a) {
complex tmp(a,0);
*this += tmp;
};
//减法相关运算 (a+bi)-(c+di)=(a-c)+(b-d)i
complex operator-(const complex& comx) {
complex result((this->get_real()-comx.get_real()),(this->get_unreal()-comx.get_unreal()));
return result;
};
complex operator-(const double a) {
complex tmp(a,0);
return (*this)-tmp;
};
void operator-=(const complex& comx) {
*this = *this-comx;
};
void operator-=(const double a) {
complex tmp(a,0);
*this -= tmp;
};
//乘法相关运算 (a+bi)(c+di)=(ac-bd)+(bc+ad)i
complex operator*(const complex& comx) {
complex result((this->get_real())*(comx.get_real())-(this->get_unreal())*(comx.get_unreal()),
(this->get_unreal())*(comx.get_real())+(this->get_real())*(comx.get_unreal()));
return result;
};
complex operator*(const double a) {
complex tmp(a,0);
return (*this)*tmp;
};
void operator*=(const complex& comx) {
*this = (*this) *(comx);
};
void operator*=(const double a) {
complex tmp(a,0);
*this *=tmp;
};
//除法相关运算 :(a+bi)/(c+di)=(a+bi)*(c-di)* (1/(c*c+d*d))
complex operator/(const complex& comx){
complex tmp(comx.get_real(),comx.get_unreal()*(-1));
return (*this)*tmp*(1/(pow(comx.get_real(),2)+pow(comx.get_unreal(),2)));
}
complex operator/(const double a){
complex tmp(a,0);
return (*this)/tmp;
}
void operator/=(const complex& comx){
*this = (*this)/comx;
}
void operator/=(const double a){
complex tmp(a,0);
*this/=tmp;
}
virtual ~complex(void){};
double get_real() const {return this->real;};
double get_unreal()const {return this->unreal;};
};
int main(int argc, char *argv[])
{
complex a;
complex c(5,0);
complex b(3,3);
//(a+bi)(c+di)=(ac-bd)+(bc+ad)i
a = c*b;
cout< a=a/5;
cout<
/* cout< cout< cout<
/* if(a==c) cout< a=b;
if(a==b) {cout< system("PAUSE");
return EXIT_SUCCESS;
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/20625855/viewspace-1252297/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/20625855/viewspace-1252297/