【问题描述】
定义一个复数类,并重载运算符,以实现复数的加减乘除,相等与否,并显示其结果。
【代码实现】
// code.c
#include
using namespace std;
class Comoplex
{
friend ostream& operator<<(ostream& os ,const Comoplex& c); //友元
public:
Comoplex(double real = 0.0, double image = 0.0) //构造函数
:_real(real)
,_image(image)
{}
Comoplex(const Comoplex& c) //拷贝构造函数
{
_real = c._real;
_image = c._image;
}
~Comoplex()//析构函数
{
}
Comoplex& operator=(const Comoplex& c) //赋值运算符的重载
{
if(this != &c)
{
this->_real = c._real;
this->_image = c._image;
}
return *this;
}
bool operator==(const Comoplex &c)
{
return (this->_real == c._real) && (this->_image == c._image);
}
bool operator!=(const Comoplex &c)
{
return !(*this == c);
}
Comoplex operator+(const Comoplex &c) //加法
{
Comoplex tmp(*this);
tmp._real +=c._real;
tmp._image += c._image;
return tmp;//临时变量
}
Comoplex operator-(const Comoplex &c)
{
Comoplex tmp(*this);
tmp._real -= c._real;
tmp._image -= c._image;
return tmp;//临时变量
}
Comoplex operator*(const Comoplex &c)
{
Comoplex tmp(*this);
tmp._real = this->_real * c._real - this->_image * c._image;
tmp._image = this->_real * c._image + this->_image * c._real;
return tmp;//临时变量
}
Comoplex operator/(const Comoplex &c)
{
Comoplex tmp(*this);
tmp._real = (this->_real *c._real +this ->_image * c._image)/(c._real *c._real + c._image *c._image);
tmp._image = (this->_image*c._real - this->_real * c._image)/(c._real *c._real + c._image *c._image);
return tmp;//临时变量
}
Comoplex& operator+=(const Comoplex &c)
{
this->_real += c._real;
this->_image += c._image;
return *this;
}
Comoplex& operator-=(const Comoplex &c)
{
this->_real -= c._real;
this->_image -= c._image;
return *this;
}
Comoplex& operator*=(const Comoplex &c)
{
this->_real = this->_real * c._real - this->_image * c._image;
this->_image = this->_real * c._image + this->_image * c._real;
return *this;
}
Comoplex& operator/=(const Comoplex &c)
{
this->_real = (this->_real *c._real +this ->_image * c._image)/(c._real *c._real + c._image *c._image);
this->_image = (this->_image*c._real - this->_real * c._image)/(c._real *c._real + c._image *c._image);
return *this;
}
Comoplex& operator++()//前置++
{
this->_real++;
this->_image++;
return *this;
}
Comoplex operator++(int)//后置++
{
Comoplex tmp(*this);
this->_real++;
this->_image++;
return tmp;//临时变量
}
Comoplex& operator--()//前置--
{
this->_real--;
this->_image--;
return *this;
}
Comoplex operator--(int)//后置--
{
Comoplex tmp(*this);
this->_real--;
this->_image--;
return tmp;//临时变量
}
private:
double _real;
double _image;
};
ostream& operator<<(ostream& os ,const Comoplex& c) //输出运算符重载
{
os << c._real << "+" << c._image << "i" <
【测试代码】
//测试
int main()
{
Comoplex c1(2,3);
Comoplex c2(3,4);
Comoplex c3 = c1 + c2;
//Comoplex c4 = c1 - c2;
//Comoplex c5 = c1 * c2;
//Comoplex c6 = c1 / c2;
//Comoplex c7 = c1 += c2;
//Comoplex c8 = c1 -= c2;
//Comoplex c9 = c1 *= c2;
//Comoplex c10 = c1 /= c2;
bool ret = (c1 == c2);
if (ret)
{
cout<< "c1 == c2"<< endl;
}
else
{
cout << "c1 != c2" << endl;
}
cout <<"c1="<
【测试结果】
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/7C/99/wKiom1bS99bgyVEMAAAvB3yBqMI439.png" style="float:none;" title="4Y_W1Q{W41GX5URZVQE8KYS.png" alt="wKiom1bS99bgyVEMAAAvB3yBqMI439.png" />
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/7C/98/wKioL1bS-EuS6CvwAAATN9oxEpI526.png" style="float:none;" title="IXSD)JBMNROO]QI`9LTCC`Y.png " alt="wKioL1bS-EuS6CvwAAATN9oxEpI526.png" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/7C/98/wKioL1bS-EvyhsPEAAASflnQOw4180.png" style="float:none;" title="S0TG)VEN%DJC8O%V4J8E9G0.png" alt="wKioL1bS-EvyhsPEAAASflnQOw4180.png" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/7C/99/wKiom1bS99eScQadAAAPKbc6qYY164.png" style="float:none;" title="(8QIG95P)5FMRE41[TC_1CQ.png" alt="wKiom1bS99eScQadAAAPKbc6qYY164.png" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/7C/99/wKiom1bS99eS2N7RAAAQKVwxOzg783.png" style="float:none;" title="5XXEQITLW51S~974)XABHD8.png" alt="wKiom1bS99eS2N7RAAAQKVwxOzg783.png" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/7C/98/wKioL1bS-EyySuYMAAAUg8OMCqU854.png" style="float:none;" title="F)YG$)_O$4)4[8W~GJMUBTR.png" alt="wKioL1bS-EyySuYMAAAUg8OMCqU854.png" />
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/7C/98/wKioL1bS-EzBFL9FAAAQS7ekacs782.png" style="float:none;" title="265)(J3HP[DA06~5Z{28UV6.png" alt="wKioL1bS-EzBFL9FAAAQS7ekacs782.png" />
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M02/7C/99/wKiom1bS99iiBKhcAAAPNdaRSAw106.png" style="float:none;" title="ZE[`@MBF%F51N~6@]SZ(QVQ.png " alt="wKiom1bS99iiBKhcAAAPNdaRSAw106.png" />
本文出自 “Pzd流川枫” 博客,请务必保留此出处http://xujiafan.blog.51cto.com/10778767/1745839