C++重載運(yùn)算符實(shí)現(xiàn)分?jǐn)?shù)加減乘除
本文實(shí)例為大家分享了C++重載運(yùn)算符實(shí)現(xiàn)分?jǐn)?shù)加減乘除的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)結(jié)果如下圖所示:
代碼如下所示:
#include <iostream> using namespace std; class Rational { public: Rational operator+(Rational rhs); Rational operator-(Rational rhs); Rational operator*(Rational rhs); Rational operator/(Rational rhs); Rational(int num, int denom); private: void normalize(); //負(fù)責(zé)對分?jǐn)?shù)做化簡工作 int numerator;//分子 int denominator;//分母 friend std::ostream& operator<<(ostream& os, Rational f); }; Rational::Rational(int num, int denom) { numerator = num; denominator = denom; normalize(); } //只允許分子為負(fù)數(shù),若分母為負(fù)數(shù)則把負(fù)數(shù)挪到分子部分 //利用歐幾里得算法將分?jǐn)?shù)簡化 void Rational::normalize() { if (denominator < 0) { numerator = -numerator; denominator = -denominator; } //歐幾里得算法 int a = abs(numerator); int b = abs(denominator); //求出最大公約數(shù),a //eg: a=12,b=9. //t=3 a=9 b=3 //t=0 a=3 b=0 while (b > 0) { int t = a % b;//t為余數(shù) a = b; //a取除數(shù) b = t; //b取余數(shù) } //分子、分母分別處以最大公約數(shù) numerator /= a; denominator /= a; } //重載運(yùn)算符函數(shù)是類的成員函數(shù) //分?jǐn)?shù)是先通分再加減 // a*d+b*c / b*d Rational Rational::operator+(Rational rhs) { int a = numerator; int b = denominator; int c = rhs.numerator; int d = rhs.denominator; return Rational(a*d+b*c,b*d); } Rational Rational::operator-(Rational rhs) { int a = numerator; int b = denominator; int c = rhs.numerator; int d = rhs.denominator; return Rational(a*d - b*c, b*d); /* **這里有個(gè)偷懶的寫法 rhs.numerator = -rhs.numerator; return operator+(rhs); */ } Rational Rational::operator*(Rational rhs) { int a = numerator; int b = denominator; int c = rhs.numerator; int d = rhs.denominator; return Rational(a*c, b*d); } Rational Rational::operator/(Rational rhs) { int t = rhs.denominator; rhs.denominator = rhs.numerator; rhs.numerator = t; return operator*(rhs); } //該函數(shù)是友元函數(shù),因此不用加空間域名 ostream& operator<<(ostream& os, Rational f); int main() { Rational r1(4, 18); Rational r2(14, 18); cout << r1 << " + " << r2 << " = " << (r1 + r2) << endl; cout << r1 << " - " << r2 << " = " << (r1 - r2) << endl; cout << r1 << " * " << r2 << " = " << (r1 * r2) << endl; cout << r1 << " / " << r2 << " = " << (r1 / r2) << endl; system("pause"); return 0; } ostream& operator<<(ostream& os, Rational f) { os << f.numerator << "/" << f.denominator; return os; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++中的構(gòu)造函數(shù)與析造函數(shù)詳解
這篇文章主要介紹了C++中的構(gòu)造函數(shù)與析造函數(shù)詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06C語言位段(位域)機(jī)制結(jié)構(gòu)體的特殊實(shí)現(xiàn)及解析
這篇文章主要為大家介紹了C語言位段位域機(jī)制結(jié)構(gòu)體的特殊實(shí)現(xiàn)講解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-02-02深入理解memmove()與memcpy()的區(qū)別以及實(shí)現(xiàn)方法
本篇文章是對memmove()與memcpy()的區(qū)別以及實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C++實(shí)現(xiàn)通訊錄管理系統(tǒng)項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)通訊錄管理系統(tǒng)項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06C語言線性表順序存儲結(jié)構(gòu)實(shí)例詳解
這篇文章主要介紹了C語言線性表順序存儲結(jié)構(gòu)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06學(xué)生成績管理系統(tǒng)C語言代碼實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了C語言代碼實(shí)現(xiàn)學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01C++棧實(shí)現(xiàn)逆波蘭式的應(yīng)用
逆波蘭式指的是操作符在其所控制的操作數(shù)后面的表達(dá)式。本文主要介紹了C++棧實(shí)現(xiàn)逆波蘭式的應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11