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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

53-C++ CH08 01

發(fā)布時(shí)間:2025/3/15 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 53-C++ CH08 01 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http://lx.lanqiao.cn/problem.page?gpid=T407 算法訓(xùn)練 C++ CH08 01 ? 時(shí)間限制:1.0s ? 內(nèi)存限制:256.0MB 問題描述 已知一個(gè)有理數(shù)類Zrf_Ratio,實(shí)現(xiàn)如下的操作符重載形式:
  friend std::ostream& operator<<(std::ostream&, const zrf_Ratio&);//輸出最簡分?jǐn)?shù)
  friend std::istream& operator>>(std::istream&, zrf_Ratio&);
  friend bool operator==(const zrf_Ratio&, const zrf_Ratio&);
  friend bool operator<(const zrf_Ratio&, const zrf_Ratio&); 測試 測試時(shí)主程序會(huì)輸入四個(gè)整數(shù)a, b, c, d,表示兩個(gè)分?jǐn)?shù)a/b和c/d。要求輸出最簡分?jǐn)?shù)以及兩個(gè)分?jǐn)?shù)相等和大小的比較結(jié)果。 樣例輸入 1 7 26 25 樣例輸出 zrf is:1/7; ssh is:26/25
(zrf==ssh) is:0; (zrf<ssh) is:1 思路:就是補(bǔ)充四個(gè)重載函數(shù),純c++內(nèi)容,只需提交一下代碼: ostream& operator<<(ostream& os, const zrf_Ratio& zrf_Ratio){os << zrf_Ratio.num << "/" << zrf_Ratio.den;return os; }istream& operator>>(istream& in, zrf_Ratio& zrf_Ratio){in >> zrf_Ratio.num >> zrf_Ratio.den;return in; } bool operator==(const zrf_Ratio& z1, const zrf_Ratio& z2){if(z1.num == z2.num && z1.den == z2.den)return true;return false; } bool operator<(const zrf_Ratio& z1, const zrf_Ratio& z2){if(z1.num * z2.den < z2.num * z1.den)return true;return false; }

  完整的程序:

#include <iostream> #include <cassert> using namespace std; class zrf_Ratio {friend std::ostream& operator<<(std::ostream&, const zrf_Ratio&);friend std::istream& operator>>(std::istream&, zrf_Ratio&);friend bool operator==(const zrf_Ratio&, const zrf_Ratio&);friend bool operator<(const zrf_Ratio&, const zrf_Ratio&); public:zrf_Ratio(int=0,int=1);zrf_Ratio(const zrf_Ratio&);private:int num;int den;void reduce();//化為最簡分?jǐn)?shù) }; //補(bǔ)充函數(shù): ostream& operator<<(ostream& os, const zrf_Ratio& zrf_Ratio){os << zrf_Ratio.num << "/" << zrf_Ratio.den;return os; }istream& operator>>(istream& in, zrf_Ratio& zrf_Ratio){in >> zrf_Ratio.num >> zrf_Ratio.den;return in; } bool operator==(const zrf_Ratio& z1, const zrf_Ratio& z2){if(z1.num == z2.num && z1.den == z2.den)return true;return false; } bool operator<(const zrf_Ratio& z1, const zrf_Ratio& z2){if(z1.num * z2.den < z2.num * z1.den)return true;return false; } //公有成員函數(shù): zrf_Ratio::zrf_Ratio(int num, int den) : num(num), den(den) {reduce(); }zrf_Ratio::zrf_Ratio(const zrf_Ratio& r) : num(r.num), den(r.den) {}//私有成員函數(shù): void swap(int &m, int &n) { int t; t=m; m=n; n=t; }int zrf_Gcd(int m, int n) {if (m<n)swap(m,n);assert(n>=0);while (n>0) {int r=m%n; m = n; n = r;}return m; }void zrf_Ratio::reduce() {if (num == 0 || den == 0) {num = 0; den = 1; return; }if (den < 0){den *= -1; num *= -1;}if (num == 1)return;int sgn = (num<0?-1:1);int g = zrf_Gcd(sgn*num,den);num /= g; den /= g; }int main() {int a = 0, b = 0, c = 0, d = 0;cin >> a >> b >> c >> d;zrf_Ratio zrf(a, b),ssh(c, d);std::cout<<"zrf is:"<<zrf<<"; ssh is:"<<ssh<<'\n' ;std::cout<<"(zrf==ssh) is:"<<(zrf==ssh)<<"; (zrf<ssh) is:"<<(zrf<ssh) <<endl;return 0;}

  

轉(zhuǎn)載于:https://www.cnblogs.com/zhumengdexiaobai/p/8457832.html

總結(jié)

以上是生活随笔為你收集整理的53-C++ CH08 01的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。