c++ 之 复数类的实现

xiaoxiao2021-02-28  58

//实现复数类的基本成员函数 //实现复数之间比较大小 //实现复数的四则运算 /* 复数加法: 复数z = a + bi(a, b为实数) 当b = 0时, z为实数, 可以比较大小; 当b不为零时, z为虚数, (a = 0时为纯虚数), 不能比较大小.*/ /* 复数减法: 设z1=a+bi,z2=c+di是任意两个复数, 则它们的差是 (a+bi)-(c+di)=(a-c)+(b-d)i. 两个复数的差依然是复数,它的实部是原来两个复数实部的差,它的虚部是原来两个虚部的差 */ //复数乘法:设z1 = a + bi,z2 = c + di(a、b、c、d∈R)是任意两个复数, //那么它们的积(a + bi)(c + di) = (ac - bd) + (bc + ad)i. //复数除法: //(a + bi) / (c + di) = (ac + bd) / (c ^ 2 + d ^ 2) + (bc - ad) / (c ^ 2 + d ^ 2)i #include<iostream> using namespace std; class Complex { public: //四个默认成员函数 //构造函数 Complex(double real = 0.0, double image = 0.0) :_real(real), _image( image) { } //拷贝构造函数 Complex(const Complex& c) { _real = c._real; _image = c._image; } //析构函数 ~Complex() { } //赋值操作符的重载 Complex& operator=(Complex& c) { if (this!=&c) { this->_real = c._real; this->_image = c._image; } return *this; } //复数比较大小 bool operator==(const Complex& c) { if (this->_image != 0 || c._image != 0) { cout << "虚数不能比较大小" << endl; return false; } return (this->_image == c._image) && (this->_real==c._real); } bool operator!=(const Complex& c) { if (this->_image != 0 || c._image != 0) { cout << "虚数不能比较大小" << endl; return false; } return !(this->_image == c._image) && (this->_real == c._real); } bool operator>(const Complex& c) { if (this->_image != 0 || c._image != 0) { cout << "虚数不能比较大小" << endl; return false; } else { if (_real > c._real) return true; else return false; } } bool operator>=(const Complex& c) { if (this->_image != 0 || c._image != 0) { cout << "虚数不能比较大小" << endl; return false; } return (*this == c || *this > c); } bool operator<(const Complex& c) { if (this->_image != 0 || c._image != 0) { cout << "虚数不能比较大小" << endl; return false; } return !(*this >= c); } bool operator<=(const Complex& c) { if (this->_image != 0 || c._image != 0) { cout << "虚数不能比较大小" << endl; return false; } return !(*this>c); } //复数的四则运算 Complex operator+(const Complex& c) { Complex sum; sum._real = _real + c._real; sum._image = _image + c._image; return sum; } Complex& operator+=(const Complex& c) { _real = this->_real + c._real; _image = this->_image + c._image; return *this; } Complex operator-(const Complex& c) { Complex sub; sub._real = _real - c._real; sub._image = _image - c._image; return sub; } Complex& operator-=(const Complex& c) { _real = this->_real - c._real; _image = this->_image - c._image; return *this; } Complex operator*(const Complex& c) { Complex mul; mul._real = (this->_real * c._real) - (this->_image *c._image); mul._image = (this->_real * c._real) +(this->_image *c._image); return mul; } Complex operator/(const Complex& c) { Complex div; div._real = ((this->_real*c._real) + (this->_image*c._image)) / (c._real*c._real + c._image*c._image); div._image = ((this->_image*c._real) - (this->_real*c._image)) / (c._real*c._real + c._image*c._image); return div; } //前置++ Complex& operator++() { this->_real++; this->_image++; return *this; } //后置++ Complex operator++(int) { Complex tmp(*this); this->_real++; this->_image++; return tmp; } void Display() { cout << _real << "+" << _image << "i" << endl; } private: double _real; double _image; }; void test() { Complex c1(2, 1); c1.Display(); Complex c2(3,1); c2.Display(); bool ret = (c1 <c2); cout << ret << endl; Complex c3 = c1 - c2; c3.Display(); Complex c4 = c1 + c2; c4.Display(); c1-=c2; c1.Display(); Complex c5 = c1 /c2; c5.Display(); c1++; c1.Display(); ++c1; c1.Display(); } int main() { test(); system("pause"); return 0; }

结果如下:

转载请注明原文地址: https://www.6miu.com/read-66000.html

最新回复(0)