印刷报价下单网站开发,html博客转wordpress,网站开发包括后台 前台,广西南宁网站制作四则运算符#xff08;、-、、/、、-、、/#xff09;和关系运算符#xff08;、、、、、!#xff09;都是数学运算符#xff0c;它们在实际开发中非常常见#xff0c;被重载的几率也很高#xff0c;并且有着相似的重载格式。
复数能够进行完整的四则运…四则运算符、-、、/、、-、、/和关系运算符、、、、、!都是数学运算符它们在实际开发中非常常见被重载的几率也很高并且有着相似的重载格式。
复数能够进行完整的四则运算但不能进行完整的关系运算我们只能判断两个复数是否相等但不能比较它们的大小所以不能对 、、、 进行重载。下面是具体的代码
#include iostream
#include cmath
using namespace std;//复数类
class Complex{public: //构造函数Complex(double real 0.0, double imag 0.0): m_real(real), m_imag(imag){ }public: //运算符重载//以全局函数的形式重载friend Complex operator(const Complex c1, const Complex c2);friend Complex operator-(const Complex c1, const Complex c2);friend Complex operator*(const Complex c1, const Complex c2);friend Complex operator/(const Complex c1, const Complex c2);friend bool operator(const Complex c1, const Complex c2);friend bool operator!(const Complex c1, const Complex c2);//以成员函数的形式重载Complex operator(const Complex c);Complex operator-(const Complex c);Complex operator*(const Complex c);Complex operator/(const Complex c);public: //成员函数double real() const{ return m_real; }double imag() const{ return m_imag; }private:double m_real; //实部double m_imag; //虚部
};//重载运算符
Complex operator(const Complex c1, const Complex c2){Complex c;c.m_real c1.m_real c2.m_real;c.m_imag c1.m_imag c2.m_imag;return c;
}//重载-运算符
Complex operator-(const Complex c1, const Complex c2){Complex c;c.m_real c1.m_real - c2.m_real;c.m_imag c1.m_imag - c2.m_imag;return c;
}//重载*运算符 (abi) * (cdi) (ac-bd) (bcad)i
Complex operator*(const Complex c1, const Complex c2){Complex c;c.m_real c1.m_real * c2.m_real - c1.m_imag * c2.m_imag;c.m_imag c1.m_imag * c2.m_real c1.m_real * c2.m_imag;return c;
}//重载/运算符 (abi) / (cdi) [(acbd) / (c²d²)] [(bc-ad) / (c²d²)]i
Complex operator/(const Complex c1, const Complex c2){Complex c;c.m_real (c1.m_real*c2.m_real c1.m_imag*c2.m_imag) / (pow(c2.m_real, 2) pow(c2.m_imag, 2));c.m_imag (c1.m_imag*c2.m_real - c1.m_real*c2.m_imag) / (pow(c2.m_real, 2) pow(c2.m_imag, 2));return c;
}//重载运算符
bool operator(const Complex c1, const Complex c2){if( c1.m_real c2.m_real c1.m_imag c2.m_imag ){return true;}else{return false;}
}//重载!运算符
bool operator!(const Complex c1, const Complex c2){if( c1.m_real ! c2.m_real || c1.m_imag ! c2.m_imag ){return true;}else{return false;}
}//重载运算符
Complex Complex::operator(const Complex c){this-m_real c.m_real;this-m_imag c.m_imag;return *this;
}//重载-运算符
Complex Complex::operator-(const Complex c){this-m_real - c.m_real;this-m_imag - c.m_imag;return *this;
}//重载*运算符
Complex Complex::operator*(const Complex c){this-m_real this-m_real * c.m_real - this-m_imag * c.m_imag;this-m_imag this-m_imag * c.m_real this-m_real * c.m_imag;return *this;
}//重载/运算符
Complex Complex::operator/(const Complex c){this-m_real (this-m_real*c.m_real this-m_imag*c.m_imag) / (pow(c.m_real, 2) pow(c.m_imag, 2));this-m_imag (this-m_imag*c.m_real - this-m_real*c.m_imag) / (pow(c.m_real, 2) pow(c.m_imag, 2));return *this;
}int main(){Complex c1(25, 35);Complex c2(10, 20);Complex c3(1, 2);Complex c4(4, 9);Complex c5(34, 6);Complex c6(80, 90);Complex c7 c1 c2;Complex c8 c1 - c2;Complex c9 c1 * c2;Complex c10 c1 / c2;coutc7 c7.real() c7.imag()iendl;coutc8 c8.real() c8.imag()iendl;coutc9 c9.real() c9.imag()iendl;coutc10 c10.real() c10.imag()iendl;c3 c1;c4 - c2;c5 * c2;c6 / c2;coutc3 c3.real() c3.imag()iendl;coutc4 c4.real() c4.imag()iendl;coutc5 c5.real() c5.imag()iendl;coutc6 c6.real() c6.imag()iendl;if(c1 c2){coutc1 c2endl;}if(c1 ! c2){coutc1 ! c2endl;}return 0;
}运行结果
c7 35 55i
c8 15 15i
c9 -450 850i
c10 1.9 -0.3i
c3 26 37i
c4 -6 -11i
c5 220 4460i
c6 5.2 1.592i
c1 ! c2注意我们以全局函数的形式重载了 、-、、/、、!以成员函数的形式重载了 、-、、/而且应该坚持这样做不能一股脑都写作成员函数或者全局函数。