#include <iostream>
#include <cmath>
#include <cstring>
#include <iomanip>
using namespace std; int gcd(int x,int y)
{
x=abs(x);
y=abs(y);
if(x<y)
{
int t=x;
x=y;
y=t;
}
if(x%y==)
return y;
else
return gcd(x%y,y);
} class Rational
{
private:
int z; //分子
int m; //分母
public:
Rational(int a=, int b=)//构造有理数分数,分子默认为0,分母默认为1
{
z=a;
m=b;
}
friend Rational& yuefen(Rational& r)//约分函数对分数化简
{
int c;
c=gcd(r.z,r.m);
r.z=r.z/c;
r.m=r.m/c;
if(r.z>&&r.m<)
{
r.z=-r.z;
r.m=-r.m;
}
if(r.z<&&r.m<)
{
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 ;
else
return ;
}
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 ;
else
return ;
}
friend ostream & operator<<(ostream &t1, const Rational &t2)
{
t1<<t2.z<<"/"<<t2.m;
return t1;
}
friend istream & operator>>(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 ; }
#include <iostream>
#include <cmath>
#include <cstring>
#include <iomanip>
using namespace std; class CheckedPtr
{
public:
CheckedPtr(int * b, int * e):beg(b), end(e), curr(b){ }
CheckedPtr & operator ++()// prefix ++
{
this->curr+=;
return *this;
}
CheckedPtr & operator --() // prefix --
{
this->curr-=;
return *this;
}
CheckedPtr operator ++(int)// postfix ++,()内int用于c区分前置后置
{
CheckedPtr t=*this;
this->curr+=;
return t;
}
CheckedPtr operator --(int)// postfix --
{
CheckedPtr t=*this;
this->curr-=;
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[] = {,,,,,,,,,};
CheckedPtr cp(array, array+);
for(;cp.GetCurr()<cp.GetEnd();cp++)
cout<<*cp.GetCurr()<<" ";
cout<<endl;
for(--cp;cp.GetCurr()>cp.GetBeg();cp--)
cout<<*cp.GetCurr()<<" ";
cout<<*cp.GetCurr()<<endl;
return ;
}

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. Esri大数据分析引擎GeoAnalytics Server部署经历

    系统架构 Base WebGIS 4Cores 16GB Spatiotemporal Data Store 32GB SSD Disk 足够大的空间 GA Server 4Cores 16GB 足够 ...

  2. 使用WICleanup清理Windows Installer 冗余文件

    使用WICleanup清理Windows Installer 冗余文件 | 浏览:816 | 更新:2015-11-02 10:43 | 标签:Win7 Win10 1 2 3 4 5 6 7 分步阅 ...

  3. Visual Studio 2015 + Update 1

    Visual Studio 2015是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码管控工具.集成开发环境(IDE)等等.所写的目标代码适用于微软支持的所有 ...

  4. volley2--volley的使用和架构

    图片: 下面只是笼统的介绍,大家可以对比自己的想法,看看自己是不是有什么考虑不周的(如果是你实现这样一个框架的话) 1,Request的设计,我们在得到response之后,我们可能根据项目需求希望有 ...

  5. android OrmLite

    最近在使用ormlite框架进行数据库的操作,下面简单的写个demo来学习下 1.下载jar包 这里使用的是ormlite-core-5.0.jar 和 ormlite-android-5.0.jar ...

  6. vue3.0环境最新安装步骤

    安装最新的node.js版本: https://nodejs.org/dist/v8.11.3/node-v8.11.3-x64.msi 安装vue:  npm install -g @vue/cli ...

  7. CPU硬件辅助虚拟化技术

    目前主要有Intel的VT-x和AMD的AMD-V这两种技术.其核心思想都是通过引入新的指令和运行模式,使VMM和Guest OS分别运行在不同模式(ROOT模式和非ROOT模式)下,且Guest O ...

  8. Python实例---基于页面的后台管理[简单版]

    后台管理菜单 + 母板[css/content/js] 向后台提交数据[2种]:       1.  模态对话框(数据少操作,且Js复杂):        form表单 :优点:简单,前端提交后后台处 ...

  9. haproxy开启日志功能

    haproxy在默认情况不会记录日志,除了在haproxy.conf中的global段指定日志的输出外,还需要配置系统日志的配置文件.下面以centos6.4为例,haproxy使用系统自带的rpm报 ...

  10. Entity Framework:代码优先

    一.代码优先Code First EF6支持Oracle ODT 12C Release 3 (net4.5) DataModel(类)-->生成数据库DB 或 存在的数据库DB-->生成 ...