当前位置:网站首页>西农大 C plus
西农大 C plus
2022-07-20 18:17:00 【numb and dying】
问题 K: oop实习-11.运算符重载
题目描述
定义有理数类(分母不为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的<、<=、>、>=的结果
提示
#include <iostream>
using namespace std;
class Rational{
friend Rational operator+(Rational& p1, Rational& p2);
friend Rational operator-(Rational& p1, Rational& p2);
friend Rational operator*(Rational& p1, Rational& p2);
friend Rational operator/(Rational& p1, Rational& p2);
friend bool operator<(Rational& p1, Rational& p2);
friend bool operator<=(Rational& p1, Rational& p2);
friend bool operator>(Rational& p1, Rational& p2);
friend bool operator>=(Rational& p1, Rational& p2);
friend ostream& operator <<(ostream& cout, Rational& p);
friend istream& operator >>(istream& cin, Rational& p);
private:
int iUp;
int 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 l, int r) {
int temp = l % r;
while (temp != 0) {
l = r;
r = temp;
temp = l % r;
}
return r;
}
public:
Rational() {
iUp = 1;
iDown = 1;
}
Rational(int a,int b) {
iUp = a;
iDown = b;
Reduce();
}
//相反数
Rational operator-() {
Rational temp;
temp.iUp = -iUp;
temp.iDown = iDown;
return temp;
}
//赋值
Rational& operator=(const Rational& p) {
iUp = p.iUp;
iDown = p.iDown;
Reduce();
return *this;
}
//前置++
Rational& operator++() {
iUp += iDown;
Reduce();
return *this;
}
//前置--
Rational& operator--() {
iUp -= iDown;
return *this;
}
//后置++
Rational operator++(int) {
Rational temp = *this;
iUp += iDown;
return temp;
}
//后置--
Rational operator--(int) {
Rational temp = *this;
iUp -= iDown;
return temp;
}
};
//加法+
Rational operator+(Rational& p1, Rational& p2) {
Rational temp;
temp.iDown = p1.iDown * p2.iDown;
temp.iUp = p1.iUp * p2.iDown + p2.iUp * p1.iDown;
temp.Reduce();
return temp;
}
//减法-
Rational operator-(Rational& p1, Rational& p2) {
Rational temp;
temp.iDown = p1.iDown * p2.iDown;
temp.iUp = p1.iUp * p2.iDown - p2.iUp * p1.iDown;
temp.Reduce();
return temp;
}
//乘法*
Rational operator*(Rational& p1, Rational& p2) {
Rational temp;
temp.iUp = p1.iUp * p2.iUp;
temp.iDown = p1.iDown * p2.iDown;
temp.Reduce();
return temp;
}
//除法/
Rational operator/(Rational& p1, Rational& p2) {
Rational temp;
temp.iUp = p1.iUp * p2.iDown;
temp.iDown = p1.iDown * p2.iUp;
temp.Reduce();
return temp;
}
//<号
bool operator<(Rational& p1, Rational& p2) {
int temp;
temp = p1.iDown * p2.iDown;
p1.iDown = temp;
p1.iUp += p2.iDown;
p2.iDown = temp;
p2.iUp += p1.iDown;
p1.Reduce();
p2.Reduce();
if (p1.iUp < p2.iUp) {
return true;
}
return false;
}
bool operator<=(Rational& p1, Rational& p2) {
int temp;
temp = p1.iDown * p2.iDown;
p1.iDown = temp;
p1.iUp += p2.iDown;
p2.iDown = temp;
p2.iUp += p1.iDown;
p1.Reduce();
p2.Reduce();
if (p1.iUp <= p2.iUp) {
return true;
}
return false;
}
bool operator>(Rational& p1, Rational& p2) {
int temp;
temp = p1.iDown * p2.iDown;
p1.iDown = temp;
p1.iUp += p2.iDown;
p2.iDown = temp;
p2.iUp += p1.iDown;
p1.Reduce();
p2.Reduce();
if (p1.iUp > p2.iUp) {
return true;
}
return false;
}
bool operator>=(Rational& p1, Rational& p2) {
int temp;
temp = p1.iDown * p2.iDown;
p1.iDown = temp;
p1.iUp += p2.iDown;
p2.iDown = temp;
p2.iUp += p1.iDown;
p1.Reduce();
p2.Reduce();
if (p1.iUp >= p2.iUp) {
return true;
}
return false;
}
ostream& operator <<(ostream& cout, Rational& p) {
int temp;
if (p.iUp % p.iDown == 0) {
cout << p.iUp / p.iDown <<endl;
}else {
cout << p.iUp << "/" << p.iDown << endl;
}
return cout;
}
istream& operator >>(istream& cin, Rational& p) {
cin >> p.iUp >> p.iDown;
//cout << p.iUp << "/" << p.iDown << endl;
return cin;
}
int main()
{
Rational a;
Rational b;
cin >> a;
cin >> b;
Rational c;
c = a + b;
cout << "a+b: " << c ;
c = a - b;
cout << "a-b: " << c;
c = a * b;
cout << "a*b: " << c;
c = a / b;
cout << "a/b: " << c;
c = -a;
cout << "-a: " << c ;
cout << "++a: " << ++a ;
cout << "--a: " << --a ;
c = a++;
cout << "a++: " << c ;
c = a--;
cout << "a--: " << c ;
bool d;
d = (a < b);
cout << "a<b: " << boolalpha << d << endl;
d = (a <= b);
cout << "a<=b: " << boolalpha << d<< endl;
d = (a > b);
cout << "a>b: " << boolalpha << d << endl;
d = (a >= b);
cout << "a>=b: " << boolalpha << d << endl;
}
边栏推荐
猜你喜欢
随机推荐
Guangdong Xinghao sterile injection has passed the certification of the European drug administration and obtained the "quality pass" in the international market
华为云数据治理生产线DataArts,让“数据‘慧’说话”
通过专线/VPN网关/智能接入网关的自建数据库传输之间的差异是什么?
Use the add () method in calendar to add and subtract time, obtain the time range, and read the dynamic data dictionary.
1111111
电商项目中订单系统到底该怎么设计才好?(至尊典藏版)
How does AI predict the COVID-19? A summary of Georgia Institute of technology's latest "data centric epidemic prediction"
选择ECS上的自建数据库这种接入方式,适合那种类型的数据库迁移?
如何实现FinClip微信授权登录的三种方案
2022清华暑校笔记之L1_NLP和Bigmodel基础
小红书商城整店商品API接口(店铺所有商品接口)
使用MySQL, JSON 这张牌你一定要善用
【Matplotlib 画图】
使用无公网IP: Port的数据库时候需要注意什么问题?
Pr视频剪辑师如何选笔记本?华硕灵耀Pro16 2022带你玩转内容创作
使用 GCC 避免堆栈粉碎攻击
CH单库数据迁移到读写分离模式
17 polymorphism
选择云企业网CEN接入自建数据库,需要怎么选择和链接?
不公平分发