问题 K: oop实习-11.运算符重载

news/2024/10/25 15:23:54/

题目描述

定义有理数类(分母不为0的分数,分子分母均为整数)Rational,实现相应操作符的重载。

(1)定义私有数据成员:分子int iUp; 分母 int iDown。

(2)定义私有成员函数:void Reduce() 和 int Gcd(int l, int r),分别用于有理数的约简和求两个整数的最大公约数。其中,在约简时需要求取分子与分母的最大公约数。

(3)定义构造函数,在构造函数体内可调用Reduce对有理数进行约简。

(4)将负号-和赋值运算符=重载为公有成员函数,分别用于求有理数的负数和赋值。

(5)将前置++、前置--、后置++、后置--重载为公有成员函数,实现有理数自增1或自减1。

(6)将+、-、*、/重载为友员函数,实现有理数的加减乘除。

(7)将<、<=、>、>=重载为友员函数,实现有理数的大小关系比较。

(8)重载流插入符<<和流提取符>>,分别用于有理数的输出和输入。其中,输出格式为“分子/分母”,若为整数,则直接输出整数。

在main函数中,根据输入的分子和分母定义两个有理数对象a和b。再定义几个有理数对象分别用于表示a和b的加、减、乘、除、前置自增a、前置自减a、后置自增a、后置自减a,并依次各个对象的结果。最后依次用<、<=、>、>=比较a和b的大小关系,并依次输出比较结果(true或false)。 

输入

两个有理数a和b的的分子和分母

输出

有理数a和b的加、减、乘、除以及前置自增a、前置自减a、后置自增a、后置自减a

有理数a和b的<、<=、>、>=的结果

样例输入

4 3 3 2

样例输出

a+b: 17/6

a-b: -1/6

a*b: 2

a/b: 8/9

-a: -4/3

++a: 7/3

 --a: 4/3

a++: 4/3

a--: 7/3

a<b: true

a<=b: true

a>b: false

a>=b: false


#include <iostream>
#include <cmath>
using namespace std;class Rational
{
private:int iUp,iDown;void Reduce(){int t=abs(Gcd(iUp,iDown));iUp=iUp/t;iDown=iDown/t;if(iDown<0) { iDown=-iDown; iUp=-iUp; }}int Gcd(int a,int b){int r=a%b;while(r!=0){a=b; b=r; r=a%b;}return b;}
public:Rational(){}Rational(int a,int b){iUp=a; iDown=b;Reduce();}Rational& operator=(const Rational& a){iUp=a.iUp;iDown=a.iDown;Reduce();return *this;}Rational operator-(){Rational t;t.iUp=-iUp;t.iDown=iDown;return t;}Rational& operator++();Rational operator++(int);Rational& operator--();Rational operator--(int);friend Rational operator+(const Rational&,const Rational&);friend Rational operator-(const Rational&,const Rational&);friend Rational operator*(const Rational&,const Rational&);friend Rational operator/(const Rational&,const Rational&);friend bool operator<(const Rational&,const Rational&);friend bool operator<=(const Rational&,const Rational&);friend bool operator>(const Rational&,const Rational&);friend bool operator>=(const Rational&,const Rational&);friend ostream& operator<<(ostream& out,const Rational&);friend istream& operator>>(istream& in,Rational&);
};Rational& Rational::operator++()
{iUp=iUp+iDown;return *this;
}
Rational Rational::operator++(int)
{Rational temp(iUp,iDown);iUp=iUp+iDown;return temp;
}
Rational& Rational::operator--()
{iUp=iUp-iDown;return *this;
}
Rational Rational::operator--(int)
{Rational temp(iUp,iDown);iUp=iUp-iDown;return temp;
}
Rational operator+(const Rational& a,const Rational& b)
{Rational t;t.iUp = a.iUp*b.iDown + b.iUp*a.iDown;t.iDown = a.iDown * b.iDown;return t;}
Rational operator-(const Rational& a,const Rational& b)
{Rational t;t.iUp = a.iUp*b.iDown - b.iUp*a.iDown;t.iDown = a.iDown * b.iDown;return t;
}
Rational operator*(const Rational& a,const Rational& b)
{Rational t;t.iUp = a.iUp * b.iUp;t.iDown = a.iDown * b.iDown;return t;
}
Rational operator/(const Rational& a,const Rational& b)
{Rational t;t.iUp = a.iUp * b.iDown;t.iDown = a.iDown * b.iUp;return t;
}
bool operator<(const Rational&a,const Rational&b)
{return( a.iUp/(double)a.iDown < b.iUp/(double)b.iDown );
}
bool operator<=(const Rational&a,const Rational&b)
{return( a.iUp/(double)a.iDown <= b.iUp/(double)b.iDown );
}
bool operator>(const Rational&a,const Rational&b)
{return( a.iUp/(double)a.iDown > b.iUp/(double)b.iDown );
}
bool operator>=(const Rational&a,const Rational&b)
{return( a.iUp/(double)a.iDown >= b.iUp/(double)b.iDown );
}
ostream& operator<<(ostream& out,const Rational&a)
{if(a.iUp%a.iDown!=0)out<<a.iUp<<"/"<<a.iDown;else out<<(a.iUp/a.iDown);return out;
}
istream& operator>>(istream& in,Rational&a)
{in>>a.iUp>>a.iDown;return in;
}int main()
{Rational a,b;cin>>a>>b;Rational c;c=a+b;cout<<"a+b: "<<c<<endl;c=a-b;cout<<"a-b: "<<c<<endl;c=a*b;cout<<"a*b: "<<c<<endl;c=a/b;cout<<"a/b: "<<c<<endl;c=-a;cout<<"-a: "<<c<<endl;c=++a;cout<<"++a: "<<c<<endl;c=--a;cout<<"--a: "<<c<<endl;c=a++;cout<<"a++: "<<c<<endl;c=a--;cout<<"a--: "<<c<<endl;bool x;x=a<b;cout<<"a<b: "<<boolalpha<<x<<endl;x=a<=b;cout<<"a<=b: "<<x<<endl;x=a>b;cout<<"a>b: "<<x<<endl;x=a>=b;cout<<"a>=b: "<<x<<endl;return 0;
}

 


http://www.ppmy.cn/news/315263.html

相关文章

特殊矩阵的压缩存储(详细版 通俗易懂 含c语言稀疏矩阵十字链表代码 )

前言 此文章是本人第一篇博客&#xff0c;目的在于巩固过去所学的知识&#xff0c;同时可能会给大家带来一丝丝帮助&#xff0c;但由于没有经验加上本人能力极其有限&#xff0c;文章中可能存在不足之处&#xff0c;还请读者能够指正&#xff08;&#xff65;ω&#xff65;&a…

解决C++运算符重载时=和+(或-、*、/、后置自增自减)无法连用

背景题目&#xff1a; 定义有理数类&#xff08;分母不为0的分数&#xff0c;分子分母均为整数&#xff09;Rational&#xff0c;实现相应操作符的重 载。 &#xff08;1&#xff09;定义私有数据成员&#xff1a;分子int iUp; 分母 int iDown。 &#xff08;2&#xff09;定义…

shell脚本实现俄罗斯方块

脚本内容&#xff1a; #!/bin/bash # Tetris Game# 10.21.2003 xhchen< [email]xhchenwinbond.com.tw[/email]> #APP declarationAPP_NAME"${0##*[\\/]}"APP_VERSION"1.0" #颜色定义cRed1cGreen2cYellow3cBlue4cFuchsia5cCyan6cWhite7colorTable($c…

特殊矩阵的压缩存储(对称矩阵,三角矩阵,对角矩阵,稀疏矩阵的顺序,链序存储,十字链表的建立)

特殊矩阵的压缩存储 压缩存储的定义&#xff1a; 若多个数据元素的值都相同&#xff0c;则只分配一个元素值的存储空间&#xff0c;且 零元素不占存储空间。 能够压缩的一些矩阵&#xff1a; 一些特殊矩阵&#xff0c;如&#xff1a;对称矩阵&#xff0c;对角矩阵&#xff0c;…

稀疏矩阵转十字链表

定义: 十字链表(Orthogonal List)是有向图的另一种链式存储结构。该结构可以看成是将有向图的邻接表和逆邻接表结合起来得到的。用十字链表来存储有向图&#xff0c;可以达到高效的存取效果。同时&#xff0c;代码的可读性也会得到提升。 ● 特点 • 只保存非零值 • 为每一行设…

shell脚本的俄罗斯方块 : )

#!/bin/bash # Tetris Game # 10.21.2003 xhchen<[email]xhchenwinbond .com.tw[/email]> #APP declaration APP_NAME"${0##*[\\/]}" APP_VERSION"1.0" #颜色定义 cRed1 cGreen2 cYellow3 cBlue4 cFuchsia5 cCyan6 cWhite7 colorTable($cRed $cGre…

栈、队列和数组(包括求解迷宫问题)

1.1 琐碎知识点 栈、队列、数组是线性存储结构&#xff0c;它们都是一段连续的内存空间&#xff0c;其中栈和队列是动态的&#xff0c;而数组是静态的。它们的区别在于&#xff1a; 栈&#xff1a;后进先出&#xff0c;只能在栈顶进行插入和删除操作。队列&#xff1a;先进先出…

西农大 C plus

问题 K: oop实习-11.运算符重载 题目描述 定义有理数类&#xff08;分母不为0的分数&#xff0c;分子分母均为整数&#xff09;Rational&#xff0c;实现相应操作符的重载。 &#xff08;1&#xff09;定义私有数据成员&#xff1a;分子int iUp; 分母 int iDown。 &#xff08…