1. #include <iostream>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. int gcd(int x,int y)
  8. {
  9. x=abs(x);
  10. y=abs(y);
  11. if(x<y)
  12. {
  13. int t=x;
  14. x=y;
  15. y=t;
  16. }
  17. if(x%y==)
  18. return y;
  19. else
  20. return gcd(x%y,y);
  21. }
  22.  
  23. class Rational
  24. {
  25. private:
  26. int z; //分子
  27. int m; //分母
  28. public:
  29. Rational(int a=, int b=)//构造有理数分数,分子默认为0,分母默认为1
  30. {
  31. z=a;
  32. m=b;
  33. }
  34. friend Rational& yuefen(Rational& r)//约分函数对分数化简
  35. {
  36. int c;
  37. c=gcd(r.z,r.m);
  38. r.z=r.z/c;
  39. r.m=r.m/c;
  40. if(r.z>&&r.m<)
  41. {
  42. r.z=-r.z;
  43. r.m=-r.m;
  44. }
  45. if(r.z<&&r.m<)
  46. {
  47. r.z=abs(r.z);
  48. r.m=abs(r.m);
  49. }
  50. return r;
  51. }
  52. friend Rational operator+(const Rational &r1, const Rational &r2)
  53. {
  54. Rational t;
  55. t.m=r1.m*r2.m;
  56. t.z=r1.z*r2.m+r2.z*r1.m;
  57. return t;
  58. }
  59. friend Rational operator-(const Rational &r1, const Rational &r2)
  60. {
  61. Rational t;
  62. t.m=r1.m*r2.m;
  63. t.z=r1.z*r2.m-r2.z*r1.m;
  64. return t;
  65. }
  66. friend Rational operator*(const Rational &r1, const Rational &r2)
  67. {
  68. Rational t;
  69. t.m=r1.m*r2.m;
  70. t.z=r1.z*r2.z;
  71. return t;
  72. }
  73. friend Rational operator/(const Rational &r1, const Rational &r2)
  74. {
  75. Rational t;
  76. t.m=r1.m*r2.z;
  77. t.z=r1.z*r2.m;
  78. return t;
  79. }
  80. Rational & operator+=(const Rational &r)
  81. {
  82. Rational t;
  83. t.m=this->m*r.m;
  84. t.z=this->z*r.m+r.z*this->m;
  85. this->z=t.z;
  86. this->m=t.m;
  87. return *this;
  88. }
  89. Rational & operator-=(const Rational &r)
  90. {
  91. Rational t;
  92. t.m=this->m*r.m;
  93. t.z=this->z*r.m-r.z*this->m;
  94. this->m=t.m;
  95. this->z=t.z;
  96. return *this;
  97. }
  98. Rational & operator*=(const Rational &r)
  99. {
  100. this->m=this->m*r.m;
  101. this->z=this->z*r.z;
  102. return *this;
  103. }
  104. Rational & operator/=(const Rational &r)
  105. {
  106. Rational t;
  107. t.m=this->m*r.z;
  108. t.z=this->z*r.m;
  109. this->m=t.m;
  110. this->z=t.z;
  111. return *this;
  112. }
  113. friend bool operator==(const Rational &s1, const Rational &s2)//判断两个有理数是否相等
  114. {
  115. int m1,m2,z1,z2,t1,t2;
  116. t1=gcd(s1.z,s1.m);
  117. t2=gcd(s2.z,s2.m);
  118. m1=s1.m/t1;
  119. m2=s2.m/t2;
  120. z1=s1.z/t1;
  121. z2=s1.z/t2;
  122. if(m1==m2&&z1==z2)
  123. return ;
  124. else
  125. return ;
  126. }
  127. friend bool operator!=(const Rational &s1, const Rational &s2)//判断两个有理数是否不等
  128. {
  129. int m1,m2,z1,z2,t1,t2;
  130. t1=gcd(s1.z,s1.m);
  131. t2=gcd(s2.z,s2.m);
  132. m1=s1.m/t1;
  133. m2=s2.m/t2;
  134. z1=s1.z/t1;
  135. z2=s1.z/t2;
  136. if(m1==m2&&z1==z2)
  137. return ;
  138. else
  139. return ;
  140. }
  141. friend ostream & operator<<(ostream &t1, const Rational &t2)
  142. {
  143. t1<<t2.z<<"/"<<t2.m;
  144. return t1;
  145. }
  146. friend istream & operator>>(istream &t1, Rational &t2)
  147. {
  148. t1>>t2.z>>t2.m;
  149. return t1;
  150. }
  151. };
  152.  
  153. int main()
  154. {
  155. Rational r1,r2,r3;
  156. while(cin>>r1>>r2)
  157. {
  158. cout << "r1 = " << yuefen(r1) << "\n" << "r2 = " << yuefen(r2) << endl;
  159. r3 = r1 + r2;
  160. cout << "r1+r2 = " << yuefen(r3) << endl;
  161. r3 = r1 - r2;
  162. cout << "r1-r2 = " << yuefen(r3) << endl;
  163. r3 = r1 * r2;
  164. cout << "r1*r2 = " << yuefen(r3) << endl;
  165. r3 = r1 / r2;
  166. cout << "r1/r2 = " << yuefen(r3) << endl;
  167. cout << (r1 == r2) << " " << (r1 != r2) << endl;
  168. cout << yuefen(r1 += r2) << endl;
  169. cout << yuefen(r1 -= r2) << endl;
  170. cout << yuefen(r1 *= r2) << endl;
  171. cout << yuefen(r1 /= r2) << endl;
  172. }
  173. return ;
  174.  
  175. }
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. class CheckedPtr
  8. {
  9. public:
  10. CheckedPtr(int * b, int * e):beg(b), end(e), curr(b){ }
  11. CheckedPtr & operator ++()// prefix ++
  12. {
  13. this->curr+=;
  14. return *this;
  15. }
  16. CheckedPtr & operator --() // prefix --
  17. {
  18. this->curr-=;
  19. return *this;
  20. }
  21. CheckedPtr operator ++(int)// postfix ++,()内int用于c区分前置后置
  22. {
  23. CheckedPtr t=*this;
  24. this->curr+=;
  25. return t;
  26. }
  27. CheckedPtr operator --(int)// postfix --
  28. {
  29. CheckedPtr t=*this;
  30. this->curr-=;
  31. return t;
  32. }
  33. int * GetBeg()
  34. {
  35. return beg;
  36. }
  37. int * GetEnd()
  38. {
  39. return end;
  40. }
  41. int * GetCurr()
  42. {
  43. return curr;
  44. }
  45. private:
  46. int * beg; // pointer to beginning of the array
  47. int * end; // one past the end of the array
  48. int * curr; // current position within the array
  49. };
  50.  
  51. int main()
  52. {
  53. int array[] = {,,,,,,,,,};
  54. CheckedPtr cp(array, array+);
  55. for(;cp.GetCurr()<cp.GetEnd();cp++)
  56. cout<<*cp.GetCurr()<<" ";
  57. cout<<endl;
  58. for(--cp;cp.GetCurr()>cp.GetBeg();cp--)
  59. cout<<*cp.GetCurr()<<" ";
  60. cout<<*cp.GetCurr()<<endl;
  61. return ;
  62. }

C++练习 | 运算符重载练习的更多相关文章

  1. C++ 运算符重载时,将运算符两边对象交换问题.

    在C++进行运算符重载时, 一般来讲,运算符两边的对象的顺序是不能交换的. 比如下面的例子: #include <iostream> using namespace std; class ...

  2. C#高级编程笔记2016年10月12日 运算符重载

    1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...

  3. C++运算符重载

    C++运算符重载 基本知识 重载的运算符是具有特殊名字的函数,他们的名字由关键字operator和其后要定义的运算符号共同组成. 运算符可以重载为成员函数和非成员函数.当一个重载的运算符是成员函数时, ...

  4. 标准C++之运算符重载和虚表指针

    1 -> *运算符重载 //autoptr.cpp     #include<iostream> #include<string> using namespace std ...

  5. python运算符重载

    python运算符重载就是在解释器使用对象内置操作前,拦截该操作,使用自己写的重载方法. 重载方法:__init__为构造函数,__sub__为减法表达式 class Number: def __in ...

  6. PoEduo - C++阶段班【Po学校】-Lesson03-5_运算符重载- 第7天

    PoEduo - Lesson03-5_运算符重载- 第7天 复习前面的知识点 空类会自动生成哪些默认函数 6个默认函数    1  构造  2  析构   3  赋值  4 拷贝构造  5 oper ...

  7. 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换

    [源码下载] 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自 ...

  8. 我的c++学习(8)运算符重载和友元

    运算符的重载,实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该运算符时就调用此函数来行使运算符功能.这个函数叫做运算符重载函数(常为类的成员函数). 方法与解释 ◆ 1.定义运 ...

  9. c/c++面试题(6)运算符重载详解

    1.操作符函数: 在特定条件下,编译器有能力把一个由操作数和操作符共同组成的表达式,解释为对 一个全局或成员函数的调用,该全局或成员函数被称为操作符函数.该全局或成员函数 被称为操作符函数.通过定义操 ...

  10. 实验12:Problem H: 整型数组运算符重载

    Home Web Board ProblemSet Standing Status Statistics   Problem H: 整型数组运算符重载 Problem H: 整型数组运算符重载 Tim ...

随机推荐

  1. Android解析ClassLoader(一)Java中的ClassLoader

    Android解析ClassLoader(一)Java中的ClassLoader

  2. 使用mybatis plus自动生成controller、service、dao、mapper、entity代码

    官网:http://mp.baomidou.com(这个项目不仅仅可以用于代码生成,还有分页等其他功能,是对mybatis的一层封装) 要求:基于sql自动生成domain.controller.se ...

  3. 京东原来你运用的这玩意,不错,我也要!! ContainerDNS

    转自社区 ContainerDNS 本文介绍的 DNS 命名为 ContainerDNS,作为京东商城软件定义数据中心的关键基础服务之一,具有以下特点: 分布式,高可用 自动发现服务域名 后端探活 易 ...

  4. VS2013个版本密钥(亲测可用)

    Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY(密钥) ...

  5. Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)

    前言:本文是我撰写的关于搭建“Nginx + PHP(FastCGI)”Web服务器的第6篇文章.本系列文章作为国内最早详细介绍 Nginx + PHP 安装.配置.使用的资料之一,为推动 Nginx ...

  6. 【运维】linux命令查看端口占用情况,杀死进程,后台启动进程

    1.查看端口占用情况:> lsof -i:port COMMAND    PID    USER    FD    TYPE   DEVICE    SIZE/OFF    NODE  NAME ...

  7. 荣禄[róng lù]

    荣禄[róng lù] 百科名片 荣禄 荣禄(1836年4月6日-1903年4月11日)清末大臣,晚清政治家.字仲华,号略园,瓜尔佳氏,满洲正白旗人,出身于世代军官家庭,以荫生晋工部员外郎,后任内务府 ...

  8. Linux命令--权限管理

    chmod命令 Linux/Unix 的文件调用权限分为三级 : 文件拥有者.群组.其他.利用 chmod 可以藉以控制文件如何被他人所调用. 使用权限 : 所有使用者 语法 chmod [-cfvR ...

  9. Java面试基本知识

    Java基本知识 基本知识 服务器:Tomcat 支持Servlet jsp JBoss 开源应用服务器 Apache:最广泛的http服务器,只支持静态网页 String是长度不可变,用+=的时候会 ...

  10. Odoo日历视图

    转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9280604.html 一:日历视图定义 根元素为<calendar>. 主要的属性有:    co ...