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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ 重载数学运算符

發布時間:2025/3/12 c/c++ 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ 重载数学运算符 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

四則運算符(+、-、、/、+=、-=、=、/=)和關系運算符(>、<、<=、>=、==、!=)都是數學運算符,它們在實際開發中非常常見,被重載的幾率也很高,并且有著相似的重載格式。

復數能夠進行完整的四則運算,但不能進行完整的關系運算:我們只能判斷兩個復數是否相等,但不能比較它們的大小,所以不能對 >、<、<=、>= 進行重載。下面是具體的代碼:

#include <iostream> #include <cmath> using namespace std;//復數類 class Complex{public: //構造函數Complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ }public: //運算符重載//以全局函數的形式重載friend Complex operator+(const Complex &c1, const Complex &c2);friend Complex operator-(const Complex &c1, const Complex &c2);friend Complex operator*(const Complex &c1, const Complex &c2);friend Complex operator/(const Complex &c1, const Complex &c2);friend bool operator==(const Complex &c1, const Complex &c2);friend bool operator!=(const Complex &c1, const Complex &c2);//以成員函數的形式重載Complex & operator+=(const Complex &c);Complex & operator-=(const Complex &c);Complex & operator*=(const Complex &c);Complex & operator/=(const Complex &c);public: //成員函數double real() const{ return m_real; }double imag() const{ return m_imag; }private:double m_real; //實部double m_imag; //虛部 };//重載+運算符 Complex operator+(const Complex &c1, const Complex &c2){Complex c;c.m_real = c1.m_real + c2.m_real;c.m_imag = c1.m_imag + c2.m_imag;return c; }//重載-運算符 Complex operator-(const Complex &c1, const Complex &c2){Complex c;c.m_real = c1.m_real - c2.m_real;c.m_imag = c1.m_imag - c2.m_imag;return c; }//重載*運算符 (a+bi) * (c+di) = (ac-bd) + (bc+ad)i Complex operator*(const Complex &c1, const Complex &c2){Complex c;c.m_real = c1.m_real * c2.m_real - c1.m_imag * c2.m_imag;c.m_imag = c1.m_imag * c2.m_real + c1.m_real * c2.m_imag;return c; }//重載/運算符 (a+bi) / (c+di) = [(ac+bd) / (c2+d2)] + [(bc-ad) / (c2+d2)]i Complex operator/(const Complex &c1, const Complex &c2){Complex c;c.m_real = (c1.m_real*c2.m_real + c1.m_imag*c2.m_imag) / (pow(c2.m_real, 2) + pow(c2.m_imag, 2));c.m_imag = (c1.m_imag*c2.m_real - c1.m_real*c2.m_imag) / (pow(c2.m_real, 2) + pow(c2.m_imag, 2));return c; }//重載==運算符 bool operator==(const Complex &c1, const Complex &c2){if( c1.m_real == c2.m_real && c1.m_imag == c2.m_imag ){return true;}else{return false;} }//重載!=運算符 bool operator!=(const Complex &c1, const Complex &c2){if( c1.m_real != c2.m_real || c1.m_imag != c2.m_imag ){return true;}else{return false;} }//重載+=運算符 Complex & Complex::operator+=(const Complex &c){this->m_real += c.m_real;this->m_imag += c.m_imag;return *this; }//重載-=運算符 Complex & Complex::operator-=(const Complex &c){this->m_real -= c.m_real;this->m_imag -= c.m_imag;return *this; }//重載*=運算符 Complex & Complex::operator*=(const Complex &c){this->m_real = this->m_real * c.m_real - this->m_imag * c.m_imag;this->m_imag = this->m_imag * c.m_real + this->m_real * c.m_imag;return *this; }//重載/=運算符 Complex & Complex::operator/=(const Complex &c){this->m_real = (this->m_real*c.m_real + this->m_imag*c.m_imag) / (pow(c.m_real, 2) + pow(c.m_imag, 2));this->m_imag = (this->m_imag*c.m_real - this->m_real*c.m_imag) / (pow(c.m_real, 2) + pow(c.m_imag, 2));return *this; }int main(){Complex c1(25, 35);Complex c2(10, 20);Complex c3(1, 2);Complex c4(4, 9);Complex c5(34, 6);Complex c6(80, 90);Complex c7 = c1 + c2;Complex c8 = c1 - c2;Complex c9 = c1 * c2;Complex c10 = c1 / c2;cout<<"c7 = "<<c7.real()<<" + "<<c7.imag()<<"i"<<endl;cout<<"c8 = "<<c8.real()<<" + "<<c8.imag()<<"i"<<endl;cout<<"c9 = "<<c9.real()<<" + "<<c9.imag()<<"i"<<endl;cout<<"c10 = "<<c10.real()<<" + "<<c10.imag()<<"i"<<endl;c3 += c1;c4 -= c2;c5 *= c2;c6 /= c2;cout<<"c3 = "<<c3.real()<<" + "<<c3.imag()<<"i"<<endl;cout<<"c4 = "<<c4.real()<<" + "<<c4.imag()<<"i"<<endl;cout<<"c5 = "<<c5.real()<<" + "<<c5.imag()<<"i"<<endl;cout<<"c6 = "<<c6.real()<<" + "<<c6.imag()<<"i"<<endl;if(c1 == c2){cout<<"c1 == c2"<<endl;}if(c1 != c2){cout<<"c1 != c2"<<endl;}return 0; }

運行結果:

c7 = 35 + 55i c8 = 15 + 15i c9 = -450 + 850i c10 = 1.9 + -0.3i c3 = 26 + 37i c4 = -6 + -11i c5 = 220 + 4460i c6 = 5.2 + 1.592i c1 != c2

注意:我們以全局函數的形式重載了 +、-、、/、==、!=,以成員函數的形式重載了 +=、-=、=、/=,而且應該堅持這樣做,不能一股腦都寫作成員函數或者全局函數。

總結

以上是生活随笔為你收集整理的C++ 重载数学运算符的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。