C++实现 复数类
#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)iclass?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;}elsereturn?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;
}
轉載于:https://blog.51cto.com/iynu17/1735825
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: Ethernet IP TCP UDP
- 下一篇: ZOJ 3829 贪心 思维题