博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++练习 | 运算符重载练习
阅读量:4646 次
发布时间:2019-06-09

本文共 4373 字,大约阅读时间需要 14 分钟。

#include 
#include
#include
#include
using namespace std;int gcd(int x,int y){ x=abs(x); y=abs(y); if(x
0&&r.m<0) { r.z=0-r.z; r.m=0-r.m; } if(r.z<0&&r.m<0) { r.z=abs(r.z); r.m=abs(r.m); } return r; } friend Rational operator+(const Rational &r1, const Rational &r2) { Rational t; t.m=r1.m*r2.m; t.z=r1.z*r2.m+r2.z*r1.m; return t; } friend Rational operator-(const Rational &r1, const Rational &r2) { Rational t; t.m=r1.m*r2.m; t.z=r1.z*r2.m-r2.z*r1.m; return t; } friend Rational operator*(const Rational &r1, const Rational &r2) { Rational t; t.m=r1.m*r2.m; t.z=r1.z*r2.z; return t; } friend Rational operator/(const Rational &r1, const Rational &r2) { Rational t; t.m=r1.m*r2.z; t.z=r1.z*r2.m; return t; } Rational & operator+=(const Rational &r) { Rational t; t.m=this->m*r.m; t.z=this->z*r.m+r.z*this->m; this->z=t.z; this->m=t.m; return *this; } Rational & operator-=(const Rational &r) { Rational t; t.m=this->m*r.m; t.z=this->z*r.m-r.z*this->m; this->m=t.m; this->z=t.z; return *this; } Rational & operator*=(const Rational &r) { this->m=this->m*r.m; this->z=this->z*r.z; return *this; } Rational & operator/=(const Rational &r) { Rational t; t.m=this->m*r.z; t.z=this->z*r.m; this->m=t.m; this->z=t.z; return *this; } friend bool operator==(const Rational &s1, const Rational &s2)//判断两个有理数是否相等 { int m1,m2,z1,z2,t1,t2; t1=gcd(s1.z,s1.m); t2=gcd(s2.z,s2.m); m1=s1.m/t1; m2=s2.m/t2; z1=s1.z/t1; z2=s1.z/t2; if(m1==m2&&z1==z2) return 1; else return 0; } friend bool operator!=(const Rational &s1, const Rational &s2)//判断两个有理数是否不等 { int m1,m2,z1,z2,t1,t2; t1=gcd(s1.z,s1.m); t2=gcd(s2.z,s2.m); m1=s1.m/t1; m2=s2.m/t2; z1=s1.z/t1; z2=s1.z/t2; if(m1==m2&&z1==z2) return 0; else return 1; } friend ostream & operator<<(ostream &t1, const Rational &t2) { t1<
<<"/"<
>(istream &t1, Rational &t2) { t1>>t2.z>>t2.m; return t1; }};int main(){ Rational r1,r2,r3; while(cin>>r1>>r2) { cout << "r1 = " << yuefen(r1) << "\n" << "r2 = " << yuefen(r2) << endl; r3 = r1 + r2; cout << "r1+r2 = " << yuefen(r3) << endl; r3 = r1 - r2; cout << "r1-r2 = " << yuefen(r3) << endl; r3 = r1 * r2; cout << "r1*r2 = " << yuefen(r3) << endl; r3 = r1 / r2; cout << "r1/r2 = " << yuefen(r3) << endl; cout << (r1 == r2) << " " << (r1 != r2) << endl; cout << yuefen(r1 += r2) << endl; cout << yuefen(r1 -= r2) << endl; cout << yuefen(r1 *= r2) << endl; cout << yuefen(r1 /= r2) << endl; } return 0; }

 

#include 
#include
#include
#include
using namespace std;class CheckedPtr{public: CheckedPtr(int * b, int * e):beg(b), end(e), curr(b){ } CheckedPtr & operator ++()// prefix ++ { this->curr+=1; return *this; } CheckedPtr & operator --() // prefix -- { this->curr-=1; return *this; } CheckedPtr operator ++(int)// postfix ++,()内int用于c区分前置后置 { CheckedPtr t=*this; this->curr+=1; return t; } CheckedPtr operator --(int)// postfix -- { CheckedPtr t=*this; this->curr-=1; return t; } int * GetBeg() { return beg; } int * GetEnd() { return end; } int * GetCurr() { return curr; }private: int * beg; // pointer to beginning of the array int * end; // one past the end of the array int * curr; // current position within the array};int main(){ int array[10] = {
1,2,3,4,5,6,7,8,9,10}; CheckedPtr cp(array, array+10); for(;cp.GetCurr()
cp.GetBeg();cp--) cout<<*cp.GetCurr()<<" "; cout<<*cp.GetCurr()<

 

转载于:https://www.cnblogs.com/tsj816523/p/10669124.html

你可能感兴趣的文章
【转】Impala和Hive的关系
查看>>
IDEA操作git
查看>>
有向图算法之拓扑排序
查看>>
windows 下安装elasticsearch
查看>>
C语言学习12:带参数的main函数,无指定的函数形参,调用库函数处理无指定的函数形参,...
查看>>
禁止某程序联网
查看>>
[LOJ6191][CodeM]配对游戏(概率期望DP)
查看>>
mysql中utf8和utf8mb4区别
查看>>
谈谈源码管理那点事儿(一)——源码管理十诫(转)
查看>>
拒绝switch,程序加速之函数指针数组
查看>>
[你必须知道的.NET]第二十五回:认识元数据和IL(中)
查看>>
.NET中的三种Timer的区别和用法
查看>>
python第三方包安装方法(两种方法)
查看>>
10 —— node —— 获取文件在前台遍历
查看>>
MySQL 索引知识整理(创建高性能的索引)
查看>>
C++动态链接库方法调用
查看>>
全屏滚动页面的原理
查看>>
SICP习题 1.22(素数)
查看>>
字符串格式化,自动化属性赋值
查看>>
mysql主从复制
查看>>