C++實(shí)踐分?jǐn)?shù)類中運(yùn)算符重載的方法參考
【項(xiàng)目-分?jǐn)?shù)類中的運(yùn)算符重載】
(1)實(shí)現(xiàn)分?jǐn)?shù)類中的運(yùn)算符重載,在分?jǐn)?shù)類中可以完成分?jǐn)?shù)的加減乘除(運(yùn)算后再化簡)、比較(6種關(guān)系)的運(yùn)算。
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
//構(gòu)造函數(shù)及運(yùn)算符重載的函數(shù)聲明
};
//重載函數(shù)的實(shí)現(xiàn)及用于測(cè)試的main()函數(shù)
(2)在(1)的基礎(chǔ)上,實(shí)現(xiàn)分?jǐn)?shù)類中的對(duì)象和整型數(shù)的四則運(yùn)算。分?jǐn)?shù)類中的對(duì)象可以和整型數(shù)進(jìn)行四則運(yùn)算,且運(yùn)算符合交換律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同樣,可以完成i+a, 45+a, a*27, 5/a等各種運(yùn)算。
(3)定義分?jǐn)?shù)的一目運(yùn)算+和-,分別代表分?jǐn)?shù)取正和求反,將“按位取反運(yùn)算符”~重載為分?jǐn)?shù)的求倒數(shù)運(yùn)算。
(4)定義分?jǐn)?shù)類中<<和>>運(yùn)算符重載,實(shí)現(xiàn)分?jǐn)?shù)的輸入輸出,改造原程序中對(duì)運(yùn)算結(jié)果顯示方式,使程序讀起來更自然。
【參考解答】
#include <iostream>
#include <Cmath>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=1):nume(nu),deno(de) {}
void simplify();
//輸入輸出的重載
friend istream &operator>>(istream &in,CFraction &x);
friend ostream &operator<<(ostream &out,CFraction x);
CFraction operator+(const CFraction &c); //兩個(gè)分?jǐn)?shù)相加,結(jié)果要化簡
CFraction operator-(const CFraction &c); //兩個(gè)分?jǐn)?shù)相減,結(jié)果要化簡
CFraction operator*(const CFraction &c); //兩個(gè)分?jǐn)?shù)相乘,結(jié)果要化簡
CFraction operator/(const CFraction &c); //兩個(gè)分?jǐn)?shù)相除,結(jié)果要化簡
CFraction operator+(); //取正一目運(yùn)算
CFraction operator-(); //取反一目運(yùn)算
CFraction operator~(); //取倒數(shù)一目運(yùn)算
bool operator>(const CFraction &c);
bool operator<(const CFraction &c);
bool operator==(const CFraction &c);
bool operator!=(const CFraction &c);
bool operator>=(const CFraction &c);
bool operator<=(const CFraction &c);
};
// 分?jǐn)?shù)化簡
void CFraction::simplify()
{
int m,n,r;
n=fabs(deno);
m=fabs(nume);
while(r=m%n) // 求m,n的最大公約數(shù)
{
m=n;
n=r;
}
deno/=n; // 化簡
nume/=n;
if (deno<0) // 將分母轉(zhuǎn)化為正數(shù)
{
deno=-deno;
nume=-nume;
}
}
// 重載輸入運(yùn)算符>>
istream &operator>>(istream &in,CFraction &x)
{
char ch;
while(1)
{
cin>>x.nume>>ch>>x.deno;
if (x.deno==0)
cerr<<"分母為0, 請(qǐng)重新輸入\n";
else if(ch!='/')
cerr<<"格式錯(cuò)誤(形如m/n)! 請(qǐng)重新輸入\n";
else
break;
}
return cin;
}
// 重載輸出運(yùn)算符<<
ostream &operator<<(ostream &out,CFraction x)
{
cout<<x.nume<<'/'<<x.deno;
return cout;
}
// 分?jǐn)?shù)相加
CFraction CFraction::operator+(const CFraction &c)
{
CFraction t;
t.nume=nume*c.deno+c.nume*deno;
t.deno=deno*c.deno;
t.simplify();
return t;
}
// 分?jǐn)?shù)相減
CFraction CFraction:: operator-(const CFraction &c)
{
CFraction t;
t.nume=nume*c.deno-c.nume*deno;
t.deno=deno*c.deno;
t.simplify();
return t;
}
// 分?jǐn)?shù)相乘
CFraction CFraction:: operator*(const CFraction &c)
{
CFraction t;
t.nume=nume*c.nume;
t.deno=deno*c.deno;
t.simplify();
return t;
}
// 分?jǐn)?shù)相除
CFraction CFraction:: operator/(const CFraction &c)
{
CFraction t;
if (!c.nume) return *this; //除法無效(除數(shù)為)時(shí),這種情況需要考慮,但這種處理仍不算合理
t.nume=nume*c.deno;
t.deno=deno*c.nume;
t.simplify();
return t;
}
// 分?jǐn)?shù)取正號(hào)
CFraction CFraction:: operator+()
{
return *this;
}
// 分?jǐn)?shù)取負(fù)號(hào)
CFraction CFraction:: operator-()
{
CFraction x;
x.nume=-nume;
x.deno=deno;
return x;
}
// 分?jǐn)?shù)取倒數(shù)
CFraction CFraction:: operator~()
{
CFraction x;
x.nume=deno;
x.deno=nume; //未對(duì)原分子為0的情況進(jìn)行處理
if(x.deno<0) //保證負(fù)分?jǐn)?shù)的負(fù)號(hào)在分子上
{
x.deno=-x.deno;
x.nume=-x.nume;
}
return x;
}
// 分?jǐn)?shù)比較大小
bool CFraction::operator>(const CFraction &c)
{
int this_nume,c_nume,common_deno;
this_nume=nume*c.deno; // 計(jì)算分?jǐn)?shù)通分后的分子,同分母為deno*c.deno
c_nume=c.nume*deno;
common_deno=deno*c.deno;
if ((this_nume-c_nume)*common_deno>0) return true;
return false;
}
// 分?jǐn)?shù)比較大小
bool CFraction::operator<(const CFraction &c)
{
int this_nume,c_nume,common_deno;
this_nume=nume*c.deno;
c_nume=c.nume*deno;
common_deno=deno*c.deno;
if ((this_nume-c_nume)*common_deno<0) return true;
return false;
}
// 分?jǐn)?shù)比較大小
bool CFraction::operator==(const CFraction &c)
{
if (*this!=c) return false;
return true;
}
// 分?jǐn)?shù)比較大小
bool CFraction::operator!=(const CFraction &c)
{
if (*this>c || *this<c) return true;
return false;
}
// 分?jǐn)?shù)比較大小
bool CFraction::operator>=(const CFraction &c)
{
if (*this<c) return false;
return true;
}
// 分?jǐn)?shù)比較大小
bool CFraction::operator<=(const CFraction &c)
{
if (*this>c) return false;
return true;
}
int main()
{
CFraction x,y,s;
cout<<"輸入x: ";
cin>>x;
cout<<"輸入y: ";
cin>>y;
s=+x+y;
cout<<"+x+y="<<s<<endl;
s=x-y;
cout<<"x-y="<<s<<endl;
s=x*y;
cout<<"x*y="<<s<<endl;
s=x/y;
cout<<"x/y="<<s<<endl;
cout<<"-x="<<-x<<endl;
cout<<"+x="<<+x<<endl;
cout<<"x的倒數(shù): "<<~x<<endl;
cout<<x;
if (x>y) cout<<"大于";
if (x<y) cout<<"小于";
if (x==y) cout<<"等于";
cout<<y<<endl;
return 0;
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
詳解C++中遞增運(yùn)算符重載的實(shí)現(xiàn)
本文主要詳解運(yùn)算符重載里的遞增運(yùn)算符重載;遞增和遞減原理是一樣的,這里就只分享遞增的重載;提到遞增遞減,我們都知道又前置和后置兩種方法, 那今天就詳解一下前置遞增和后置遞增的細(xì)節(jié),拿捏遞增運(yùn)算符重載2022-06-06
C++實(shí)現(xiàn)線性表鏈?zhǔn)酱鎯?chǔ)(單鏈)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)線性表鏈?zhǔn)酱鎯?chǔ),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
C++中delete和delete[]的區(qū)別詳細(xì)介紹
一直對(duì)C++中的delete和delete[]的區(qū)別不甚了解,今天遇到了,上網(wǎng)查了一下,得出了結(jié)論,拿出來和大家分享一下2012-11-11
C++ opencv實(shí)現(xiàn)的把藍(lán)底照片轉(zhuǎn)化為白底照片功能完整示例
這篇文章主要介紹了C++ opencv實(shí)現(xiàn)的把藍(lán)底照片轉(zhuǎn)化為白底照片功能,結(jié)合完整實(shí)例形式詳細(xì)分析了C++使用opencv模塊進(jìn)行圖片轉(zhuǎn)換操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-12-12
基于Matlab實(shí)現(xiàn)離散系統(tǒng)分岔圖的繪制
這篇文章主要介紹了如何利用Matlab實(shí)現(xiàn)離散分岔圖的繪制,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Matlab有一定的幫助,需要的可以參考一下2022-04-04
使用OpenCV實(shí)現(xiàn)檢測(cè)和追蹤車輛
這篇文章主要為大家詳細(xì)介紹了使用OpenCV實(shí)現(xiàn)檢測(cè)和追蹤車輛,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01

