#include <iostream>
using namespace std;
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational {
public:
Rational(int = 0, int = 1);
Rational operator+(const Rational &) const;
Rational operator-(const Rational &) const;
Rational operator*(const Rational &) const;
Rational operator/(const Rational &) const;
bool operator==(const Rational &) const;
bool operator!=(const Rational &) const;
bool operator<(const Rational &) const;
bool operator<=(const Rational &) const;
bool operator>(const Rational &) const;
bool operator>=(const Rational &) const;
void printRational();
private:
int numerator;
int denominator;
void reduction();
};
#endif
Rational::Rational(int n, int d) {
numerator = n;
denominator = d;
reduction();
}
Rational Rational::operator+(const Rational &a) const {
Rational t;
t.numerator = a.numerator * denominator;
t.numerator += a.denominator * numerator;
t.denominator = a.denominator * denominator;
t.reduction();
return t;
}
Rational Rational::operator-(const Rational &s) const {
Rational t;
t.numerator = s.denominator * numerator;
t.numerator -= denominator * s.numerator;
t.denominator = s.denominator * denominator;
t.reduction();
return t;
}
Rational Rational::operator*(const Rational &m) const {
Rational t;
t.numerator = m.numerator * numerator;
t.denominator = m.denominator * denominator;
t.reduction();
return t;
}
Rational Rational::operator/(const Rational &v) const {
Rational t;
t.numerator = v.denominator * numerator;
t.denominator = denominator * v.numerator;
t.reduction();
return t;
}
bool Rational::operator==(const Rational &a) const {
if (numerator == a.numerator && denominator == a.denominator)
return true;
else
return false;
}
bool Rational::operator!=(const Rational &b) const {
if (numerator != b.numerator || denominator != b.denominator)
return true;
else
return false;
}
bool Rational::operator<(const Rational &c) const {
if (numerator / denominator < c.numerator / c.denominator)
return true;
else
return false;
}
bool Rational::operator<=(const Rational &d) const {
if (numerator / denominator <= d.numerator / d.denominator)
return true;
else
return false;
}
bool Rational::operator>(const Rational &e) const {
if (numerator / denominator > e.numerator / e.denominator)
return true;
else
return false;
}
bool Rational::operator>=(const Rational &f) const {
if (numerator / denominator >= f.numerator / f.denominator)
return true;
else
return false;
}
void Rational::printRational() {
if (denominator == 0)
cout << "\nDIVIDE BY ZERO ERROR!!!" << '\n';
else if (numerator == 0)
cout << 0;
else
cout << numerator << '/' << denominator;
}
void Rational::reduction() {
int largest;
largest = numerator > denominator ? numerator : denominator;
int gcd = 0;
for (int loop = 2; loop <= largest; loop++)
if (numerator % loop == 0 && denominator % loop == 0)
gcd = loop;
if (gcd != 0) {
numerator /= gcd;
denominator /= gcd;
}
}
int main()
{
Rational c(7,3), d(1, 3), x;
c.printRational();
cout << " + ";
d.printRational();
cout << " = ";
x = c + d;
x.printRational();
cout << '\n';
c.printRational();
cout << " - ";
d.printRational();
cout << " = ";
x = c - d;
x.printRational();
cout << '\n';
c.printRational();
cout << " * ";
d.printRational();
cout << " = ";
x = c * d;
x.printRational();
cout << '\n';
c.printRational();
cout << " / ";
d.printRational();
cout << " = ";
x = c / d;
x.printRational();
cout << '\n';
c.printRational();
cout << " is:\n";
cout << ((c > d) ? " > " : " <= ");
d.printRational();
cout << " according to the overloaded > operator\n";
cout << ((c < d) ? " < " : " >= ");
d.printRational();
cout << " according to the overloaded < operator\n";
cout << ((c >= d) ? " >= " : " < ");
d.printRational();
cout << " according to the overloaded >= operator\n";
cout << ((c <= d) ? " <= " : " > ");
d.printRational();
cout << " according to the overloaded <= operator\n";
cout << ((c == d) ? " == " : " != ");
d.printRational();
cout << " according to the overloaded == operator\n";
cout << ((c != d) ? " != " : " == ");
d.printRational();
cout << " according to the overloaded != operator" << endl;
system("pause");
}
rn.zip_RationalNumber
版权申诉
154 浏览量
2022-09-20
10:28:04
上传
评论
收藏 1KB ZIP 举报
APei
- 粉丝: 63
- 资源: 1万+
最新资源
- 本科毕业设计基于C# wpf人脸识别的考勤系统的设计与实现源码.zip
- 基于Ruoyi+uniapp实现学生考勤系统 学生考勤源码+项目说明.zip
- feae6bc968ca68a099455d8b8a8dea35
- 基于Pytorch训练CIRAR10上分类算法.zip
- Pytorch-pytorch深度学习教程之Tensorboard.zip
- 基于C++和Python开发yolov8-face作为人脸检测器dlib作为人脸识别器的人脸考勤系统源码+项目说明.zip
- Pytorch-pytorch深度学习教程之变分自动编码器.zip
- Pytorch-pytorch深度学习教程之神经风格迁移.zip
- Pytorch-pytorch深度学习教程之深度残差网络.zip
- Pytorch-pytorch深度学习教程之循环神经网络.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈