欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實踐分?jǐn)?shù)類中運算符重載的方法參考

 更新時間:2019年02月19日 11:50:22   作者:迂者-賀利堅  
今天小編就為大家分享一篇關(guān)于C++實踐分?jǐn)?shù)類中運算符重載的方法參考,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

【項目-分?jǐn)?shù)類中的運算符重載】

(1)實現(xiàn)分?jǐn)?shù)類中的運算符重載,在分?jǐn)?shù)類中可以完成分?jǐn)?shù)的加減乘除(運算后再化簡)、比較(6種關(guān)系)的運算。

class CFraction
{
private:
  int nume; // 分子
  int deno; // 分母
public:
  //構(gòu)造函數(shù)及運算符重載的函數(shù)聲明
};
//重載函數(shù)的實現(xiàn)及用于測試的main()函數(shù)

(2)在(1)的基礎(chǔ)上,實現(xiàn)分?jǐn)?shù)類中的對象和整型數(shù)的四則運算。分?jǐn)?shù)類中的對象可以和整型數(shù)進(jìn)行四則運算,且運算符合交換律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同樣,可以完成i+a, 45+a, a*27, 5/a等各種運算。

(3)定義分?jǐn)?shù)的一目運算+和-,分別代表分?jǐn)?shù)取正和求反,將“按位取反運算符”~重載為分?jǐn)?shù)的求倒數(shù)運算。

(4)定義分?jǐn)?shù)類中<<和>>運算符重載,實現(xiàn)分?jǐn)?shù)的輸入輸出,改造原程序中對運算結(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); //兩個分?jǐn)?shù)相加,結(jié)果要化簡
  CFraction operator-(const CFraction &c); //兩個分?jǐn)?shù)相減,結(jié)果要化簡
  CFraction operator*(const CFraction &c); //兩個分?jǐn)?shù)相乘,結(jié)果要化簡
  CFraction operator/(const CFraction &c); //兩個分?jǐn)?shù)相除,結(jié)果要化簡
  CFraction operator+(); //取正一目運算
  CFraction operator-(); //取反一目運算
  CFraction operator~(); //取倒數(shù)一目運算
  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;
  }
}
// 重載輸入運算符>>
istream &operator>>(istream &in,CFraction &x)
{
  char ch;
  while(1)
  {
    cin>>x.nume>>ch>>x.deno;
    if (x.deno==0)
      cerr<<"分母為0, 請重新輸入\n";
    else if(ch!='/')
      cerr<<"格式錯誤(形如m/n)! 請重新輸入\n";
    else
      break;
  }
  return cin;
}
// 重載輸出運算符<<
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ù)為)時,這種情況需要考慮,但這種處理仍不算合理
  t.nume=nume*c.deno;
  t.deno=deno*c.nume;
  t.simplify();
  return t;
}
// 分?jǐn)?shù)取正號
CFraction CFraction:: operator+()
{
  return *this;
}
// 分?jǐn)?shù)取負(fù)號
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;  //未對原分子為0的情況進(jìn)行處理
  if(x.deno<0)  //保證負(fù)分?jǐn)?shù)的負(fù)號在分子上
  {
    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ǐ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)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • C++中delete和delete[]的區(qū)別詳細(xì)介紹

    C++中delete和delete[]的區(qū)別詳細(xì)介紹

    一直對C++中的delete和delete[]的區(qū)別不甚了解,今天遇到了,上網(wǎng)查了一下,得出了結(jié)論,拿出來和大家分享一下
    2012-11-11
  • C++vector自定義大小方式

    C++vector自定義大小方式

    這篇文章主要介紹了C++vector自定義大小方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Qt下監(jiān)測內(nèi)存泄漏的方法

    Qt下監(jiān)測內(nèi)存泄漏的方法

    在寫Qt應(yīng)用程序時,由于是采用C++語言,經(jīng)常會碰到一個令人棘手的問題,那就是內(nèi)存泄漏,本文主要介紹了Qt下監(jiān)測內(nèi)存泄漏的方法,感興趣的可以了解一下
    2021-12-12
  • C語言在頭文件中定義const變量詳解

    C語言在頭文件中定義const變量詳解

    這篇文章主要介紹了C語言在頭文件中定義const變量詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • C++ opencv實現(xiàn)的把藍(lán)底照片轉(zhuǎn)化為白底照片功能完整示例

    C++ opencv實現(xiàn)的把藍(lán)底照片轉(zhuǎn)化為白底照片功能完整示例

    這篇文章主要介紹了C++ opencv實現(xiàn)的把藍(lán)底照片轉(zhuǎn)化為白底照片功能,結(jié)合完整實例形式詳細(xì)分析了C++使用opencv模塊進(jìn)行圖片轉(zhuǎn)換操作的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2019-12-12
  • C語言菜鳥基礎(chǔ)教程之Hello World

    C語言菜鳥基礎(chǔ)教程之Hello World

    C語言是一門通用計算機編程語言,應(yīng)用廣泛。C語言的設(shè)計目標(biāo)是提供一種能以簡易的方式編譯、處理低級存儲器、產(chǎn)生少量的機器碼以及不需要任何運行環(huán)境支持便能運行的編程語言。
    2017-10-10
  • 基于Matlab實現(xiàn)離散系統(tǒng)分岔圖的繪制

    基于Matlab實現(xiàn)離散系統(tǒng)分岔圖的繪制

    這篇文章主要介紹了如何利用Matlab實現(xiàn)離散分岔圖的繪制,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Matlab有一定的幫助,需要的可以參考一下
    2022-04-04
  • 使用OpenCV實現(xiàn)檢測和追蹤車輛

    使用OpenCV實現(xiàn)檢測和追蹤車輛

    這篇文章主要為大家詳細(xì)介紹了使用OpenCV實現(xiàn)檢測和追蹤車輛,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 最新評論