53-C++ CH08 01
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&);
(zrf==ssh) is:0; (zrf<ssh) is:1
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; }
53-C++ CH08 01的更多相关文章
- Dotnet文件格式解析
0x0.序 解析过程并没有介绍对pe结构的相关解析过程,网上此类相关资料很多可自行查阅,本文只介绍了网上资料较少的从pe结构的可选头中的数据目录表中获取dotnet目录的rva和size,到完全解析d ...
- 十几张表的join(千万级/百万级表) 7hours-->5mins
================START============================== 来了一个mail说是job跑得很慢,调查下原因 先来看下sql: SELECT h.order_ ...
- Neutron 理解 (7): Neutron 是如何实现负载均衡器虚拟化的 [LBaaS V1 in Juno]
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- 如何通过pid快速找出进程的路径
[carlton@oc3408554812 Desktop]$ top top - 09:35:06 up 32 min, 2 users, load average: 1.49, 1.56, 1 ...
- index merge的一次优化
手机微博4040端口SQL优化 现象 某端口常态化延迟,通过使用pt-query-digest发现主要由于一条count(*)语句引发,具体如下: # .5s .58M rss, .84M vsz # ...
- 【转载】linux环境下tcpdump源代码分析
linux环境下tcpdump源代码分析 原文时间 2013-10-11 13:13:02 CSDN博客 原文链接 http://blog.csdn.net/han_dawei/article/d ...
- 【多媒体封装格式详解】---MKV
http://blog.csdn.net/tx3344/article/details/8162656# http://blog.csdn.net/tx3344/article/details/817 ...
- linux环境下tcpdump源代码分析
Linux 环境下tcpdump 源代码分析 韩大卫@吉林师范大学 tcpdump.c 是tcpdump 工具的main.c, 本文旨对tcpdump的框架有简单了解,只展示linux平台使用的一部分 ...
- WCF简单教程
WCF是DotNet体系中很重要的一项技术,但是组内很多组员通过书籍自学的时候 感觉涉及面太广.配置文件太复杂,新名词太多.抓不到头绪,有感于此,决定进行一次组内技术培训,顺便把培训讲义整理到blog ...
随机推荐
- CentOS 6.5 下MySql主从、主主配置
参考网站: http://blog.csdn.net/faye0412/article/details/6280761 http://blog.csdn.net/kk185800961/article ...
- Python学习问题记录
1.在windows的cmd中使用open方法打开文件时,报如下错误: (unicode error) 'unicodeescape' codec can't decode bytes in posi ...
- NoClassDefFoundError: org/apache/juli/logging/LogFactory
将 apache-tomcat-7.0.8\lib 下的 tomcat-util.jar添加到 注:不是 直接在 configure build path 中添加 jar
- Makefile编写 五 隐含规则
隐含规则———— 在我们使用Makefile时,有一些我们会经常使用,而且使用频率非常高的东西,比如,我们编译C/C++的源程序为中间目标文件(Unix下是[.o]文件,Windows下是[.obj] ...
- STL 练习
makefile ------------------------------------- %.o : %.cpp g++ -g -c $< -o $@ all: t t2 rmXX % : ...
- 将xml转为array
/** * 将xml转为array * @param string $xml * @throws Exception */ public function FromXml($xml) { if (!$ ...
- questions information
1. 面向对象 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 三大特性: 封装: -- 将内容封装到对象中 -- 将方法疯 ...
- mysql 存储过程简单学习
转载自:http://blog.chinaunix.net/uid-23302288-id-3785111.html ■存储过程Stored Procedure 存储过程就是保存一系列SQL命令的集合 ...
- canvas绘制圆弧
canvas绘制圆弧 方法 anticlockwise为true表示逆时针,默认为顺时针 角度都传的是弧度(弧度 = (Math.PI/180)*角度) arc(x, y, radius, start ...
- [z]【Bash命令行处理】[详解]
(转自:http://www.linuxsir.org/bbs/thread99465.html) 我看很多兄弟写脚本或命令时出现错误的主要原因,是因为不了解bash的命令行处理.我在这里总结了一下, ...