#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; //实现复数类的基本成员函数 //实现复数之间比较大小 //实现复数的四则运算 /* 复数加法: 复数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 class Complex { public: Complex(double real=0.0 , double p_w_picpath=0.0 ) :_real(real) , _p_w_picpath(p_w_picpath) {} Complex(Complex & z) { _real = z._real; _p_w_picpath = z._p_w_picpath; } void Display() { cout << "z="<<_real << "+" << _p_w_picpath << "i" << endl; } ~Complex() {} //赋值运算符重载 Complex & operator=(Complex& z) { if (this != &z) { this->_real = z._real; this->_p_w_picpath = z._p_w_picpath; } return *this; } //复数比较大小 bool operator>(const Complex &z) { if (this->_p_w_picpath != 0 || z._p_w_picpath != 0) { cout << "虚数不能比较大小" << endl; return false; } else { if (_real > z._real) { return true; } else return false; } } bool operator==(const Complex &z) { if (this->_p_w_picpath != 0 || z._p_w_picpath != 0) { cout << "虚数不能比较大小" << endl; return false; } return (this->_p_w_picpath == z._p_w_picpath) && (this->_real == z._real); } bool operator>=(const Complex &z) { if (this->_p_w_picpath != 0 || z._p_w_picpath != 0) { cout << "虚数不能比较大小" << endl; return false; } return (*this == z ||* this > z); } bool operator<(const Complex &z) { if (this->_p_w_picpath != 0 || z._p_w_picpath != 0) { cout << "虚数不能比较大小" << endl; return false; } return !(*this >= z); } bool operator<=(const Complex &z) { if (this->_p_w_picpath != 0 || z._p_w_picpath != 0) { cout << "虚数不能比较大小" << endl; return false; } return !(*this>z); } //复数的四则运算 Complex& operator+=(const Complex &z) { _real = this->_real + z._real; _p_w_picpath = this->_p_w_picpath + z._p_w_picpath; return *this; } Complex operator+(const Complex& z) { Complex sum; sum._real =_real + z._real; sum._p_w_picpath =_p_w_picpath + z._p_w_picpath; return sum; } Complex& operator-=(const Complex &z) { _real = this->_real - z._real; _p_w_picpath = this->_p_w_picpath - z._p_w_picpath; return *this; } Complex operator-(const Complex& z) { Complex sub; sub._real = _real - z._real; sub._p_w_picpath = _p_w_picpath - z._p_w_picpath; return sub; } Complex operator*(const Complex& z) { Complex mul; mul._real = (this->_real*z._real) - (this->_p_w_picpath*z._p_w_picpath); mul._p_w_picpath = (this->_real*z._real) + (this->_p_w_picpath*z._p_w_picpath); return mul; } Complex operator/(const Complex& z) { Complex div; div._real = ((this->_real*z._real) + (this->_p_w_picpath*z._p_w_picpath))/(z._real*z._real+z._p_w_picpath*z._p_w_picpath); div._p_w_picpath = ((this->_p_w_picpath*z._real) - (this->_real*z._p_w_picpath)) / (z._real*z._real + z._p_w_picpath*z._p_w_picpath); return div; } Complex operator++() { ++_real; ++_p_w_picpath; return *this; } Complex operator++(int) { Complex tmp = *this; _real++; _p_w_picpath++; return tmp; } private: double _real; double _p_w_picpath; }; void Test() { Complex z1(2, 1); z1.Display(); //Complex z2(3,1); //z2.Display(); //bool ret = (z1 <z2); //cout << ret << endl; //Complex z3 = z1 - z2; //z3.Display(); //Complex z3 = z1 + z2; //z3.Display(); //z1-=z2; //z1.Display(); //Complex z3 = z1 / z2; //z3.Display(); z1++; z1.Display(); ++z1; z1.Display(); } int main() { Test(); system("pause"); return 0; }
C++实现 复数类
原创
©著作权归作者所有:来自51CTO博客作者言安阳的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【ArkTS】鸿蒙开发 比较两个数的大小 用户界面输入两个数并输出较大数
【HarmonyOS】 鸿蒙开发 (比较两个值大小,输出较大值)在DevEco Studio中开发,用户界面输入两个值,比较两个值大小并输出较大值。主要是练习了对TextInput属性的应用。
鸿蒙开发 TextInput属性 用户界面输入 两个值比较大小 ArkTS -
PTA—比较大小(C语言)
d",&a,&b,&c); if(a>c) { t=a;
c语言 算法 #include 输出格式