最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

【項(xiàng)目-分?jǐn)?shù)類(lèi)中的運(yùn)算符重載】

(1)實(shí)現(xiàn)分?jǐn)?shù)類(lèi)中的運(yùn)算符重載,在分?jǐn)?shù)類(lèi)中可以完成分?jǐn)?shù)的加減乘除(運(yùn)算后再化簡(jiǎ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ù)類(lèi)中的對(duì)象和整型數(shù)的四則運(yùn)算。分?jǐn)?shù)類(lèi)中的對(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ù)類(lèi)中<<和>>運(yùn)算符重載,實(shí)現(xiàn)分?jǐn)?shù)的輸入輸出,改造原程序中對(duì)運(yùn)算結(jié)果顯示方式,使程序讀起來(lái)更自然。

【參考解答】

#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é)果要化簡(jiǎn)
  CFraction operator-(const CFraction &c); //兩個(gè)分?jǐn)?shù)相減,結(jié)果要化簡(jiǎn)
  CFraction operator*(const CFraction &c); //兩個(gè)分?jǐn)?shù)相乘,結(jié)果要化簡(jiǎn)
  CFraction operator/(const CFraction &c); //兩個(gè)分?jǐn)?shù)相除,結(jié)果要化簡(jiǎn)
  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ù)化簡(jiǎn)
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;   // 化簡(jiǎ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;  //除法無(wú)效(除數(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é)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

最新評(píng)論

德昌县| 长治县| 科技| 桑日县| 桦川县| 汶川县| 仲巴县| 太原市| 兴城市| 博白县| 南丹县| 沛县| 阳新县| 新闻| 凤凰县| 庆安县| 藁城市| 太保市| 民权县| 苍南县| 安图县| 吉林市| 中卫市| 游戏| 阆中市| 海门市| 安国市| 建阳市| 平邑县| 新蔡县| 定陶县| 澎湖县| 聂拉木县| 定日县| 盈江县| 郧西县| 大悟县| 裕民县| 邢台县| 瑞金市| 根河市|