【问题描述】
定义一个复数类,并重载运算符,以实现复数的加减乘除,相等与否,并显示其结果。
【代码实现】
// code.c #include <iostream> 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" <<endl; return os; }
【测试代码】
//测试 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="<<c1; cout <<"c2="<<c2; cout <<"c3="<<c3; //cout <<"c4="<<c4; //cout <<"c5="<<c5; //cout <<"c6="<<c6; //cout <<"c7="<<c7; //cout <<"c8="<<c8; //cout <<"c9="<<c9; //cout <<"c10="<<c10; system("pause"); return 0; }
【测试结果】
时间: 2024-11-08 23:24:00