日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++实现 复数类

發(fā)布時間:2025/4/16 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++实现 复数类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#define?_CRT_SECURE_NO_WARNINGS?1#include<iostream> using?namespace?std;//實現(xiàn)復(fù)數(shù)類的基本成員函數(shù) //實現(xiàn)復(fù)數(shù)之間比較大小 //實現(xiàn)復(fù)數(shù)的四則運(yùn)算/*?復(fù)數(shù)加法: 復(fù)數(shù)z?=?a?+?bi(a,?b為實數(shù)) 當(dāng)b?=?0時,?z為實數(shù),?可以比較大小; 當(dāng)b不為零時,?z為虛數(shù),?(a?=?0時為純虛數(shù)),?不能比較大小.*//*?復(fù)數(shù)減法:設(shè)z1=a+bi,z2=c+di是任意兩個復(fù)數(shù),則它們的差是?(a+bi)-(c+di)=(a-c)+(b-d)i. 兩個復(fù)數(shù)的差依然是復(fù)數(shù),它的實部是原來兩個復(fù)數(shù)實部的差,它的虛部是原來兩個虛部的差 *///復(fù)數(shù)乘法:設(shè)z1?=?a?+?bi,z2?=?c?+?di(a、b、c、d∈R)是任意兩個復(fù)數(shù),//那么它們的積(a?+?bi)(c?+?di)?=?(ac?-?bd)?+?(bc?+?ad)i.//復(fù)數(shù)除法: //(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(){}//賦值運(yùn)算符重載Complex?&?operator=(Complex&?z){if?(this?!=?&z){this->_real?=?z._real;this->_p_w_picpath?=?z._p_w_picpath;}return?*this;}//復(fù)數(shù)比較大小bool?operator>(const?Complex?&z){if?(this->_p_w_picpath?!=?0?||?z._p_w_picpath?!=?0){cout?<<?"虛數(shù)不能比較大小"?<<?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?<<?"虛數(shù)不能比較大小"?<<?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?<<?"虛數(shù)不能比較大小"?<<?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?<<?"虛數(shù)不能比較大小"?<<?endl;return?false;}return?!(*this?>=?z);}bool?operator<=(const?Complex?&z){if?(this->_p_w_picpath?!=?0?||?z._p_w_picpath?!=?0){cout?<<?"虛數(shù)不能比較大小"?<<?endl;return?false;}return?!(*this>z);}//復(fù)數(shù)的四則運(yùn)算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; }


轉(zhuǎn)載于:https://blog.51cto.com/iynu17/1735825

《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的C++实现 复数类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。