算法训练 C++ CH08 01  
时间限制:1.0s   内存限制:256.0MB
    
问题描述
  已知一个有理数类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&);
测试
  测试时主程序会输入四个整数a, b, c, d,表示两个分数a/b和c/d。要求输出最简分数以及两个分数相等和大小的比较结果。
样例输入
1 7 26 25
样例输出
zrf is:1/7; ssh is:26/25
(zrf==ssh) is:0; (zrf<ssh) is:1
 
思路:就是补充四个重载函数,纯c++内容,只需提交一下代码:
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();//化为最简分数
};
//补充函数:
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;
}
//公有成员函数:
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)
{ } //私有成员函数:
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; }

  

最新文章

  1. [Android Pro] Scroller使用分析
  2. TScrollBox的用法 滚动事件
  3. tps (事务处理系统)
  4. Memcached驱动(C#)
  5. Android文件Apk下载变ZIP压缩包解决方案
  6. 如何防止ASP.NET网站遭受CSRF的攻击
  7. WCF 托管在IIS中遇到Http的错误
  8. zeptoJS:如何像jQuery一样,让滚动变得优雅?
  9. Android 学习之 开源项目PullToRefresh的使用
  10. [Jobdu] 题目1517:链表中倒数第k个结点
  11. java多维数组
  12. js优化原则
  13. jmeter问题处理随笔1 - CSV取值数据异常处理(包含&quot;号,&quot;,&quot;号的情况)
  14. PHP MySQL Order By 关键词
  15. 为什么ArrayList、LinkedList线程不安全,Vector线程安全
  16. 主机配置nginx后如何获取访问者IP
  17. winform 可拖动无边框窗体解决办法
  18. BZOJ4227 : 城市
  19. spinlock一边连逻辑一边连控制器
  20. hdu 4950 打怪

热门文章

  1. QT4.8.6静态编译
  2. vue的动画组件(transition)
  3. Ubuntu secuerCRT连接失败,The remote system refused the connection.
  4. 在Mac和win7上分别安装了docker后,发现原来的vagrant都启动不了了
  5. BOM的编制与管理
  6. JAVA-Unit04: SQL(高级查询)
  7. 用命令行cmd 编译小程序
  8. 模拟admin组件自己开发stark组件之创建篇
  9. git 一些用法
  10. Halcon中循环读取文件的实现以及数字与字符的转换