设计并实现一个有理数类Rational,要求:用两个整数的比描述有理数;能够进行加减除等算术运算;以分数形式输出有理数;提供double类型到有理数的类型转换;合理的初始化,注意检测分母和除数为0的错误,默认分子为0,分母为1
时间: 2023-12-03 09:41:55 浏览: 206
以下是Rational类的实现代码:
```c++
#include <iostream>
using namespace std;
class Rational {
private:
int numerator; // 分子
int denominator; // 分母
public:
Rational(int num = 0, int den = 1) {
if (den == 0) {
cout << "分母不能为0!" << endl;
numerator = 0;
denominator = 1;
} else {
numerator = num;
denominator = den;
reduce();
}
}
Rational(const Rational& r) {
numerator = r.numerator;
denominator = r.denominator;
}
Rational& operator=(const Rational& r) {
if (this != &r) {
numerator = r.numerator;
denominator = r.denominator;
}
return *this;
}
~Rational() {}
int getNumerator() const { return numerator; }
int getDenominator() const { return denominator; }
void setNumerator(int num) { numerator = num; }
void setDenominator(int den) {
if (den == 0) {
cout << "分母不能为0!" << endl;
} else {
denominator = den;
reduce();
}
}
void reduce() {
if (numerator == 0 || denominator == 1) {
return;
}
int gcd = getGcd(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
if (denominator < 0) { // 分母为负数时,将分子和分母都变为负数
numerator = -numerator;
denominator = -denominator;
}
}
int getGcd(int a, int b) {
return b == 0 ? a : getGcd(b, a % b);
}
Rational operator+ (const Rational& r) const {
int num = numerator * r.denominator + r.numerator * denominator;
int den = denominator * r.denominator;
return Rational(num, den);
}
Rational operator- (const Rational& r) const {
int num = numerator * r.denominator - r.numerator * denominator;
int den = denominator * r.denominator;
return Rational(num, den);
}
Rational operator* (const Rational& r) const {
int num = numerator * r.numerator;
int den = denominator * r.denominator;
return Rational(num, den);
}
Rational operator/ (const Rational& r) const {
if (r.numerator == 0) {
cout << "除数不能为0!" << endl;
return Rational();
}
int num = numerator * r.denominator;
int den = denominator * r.numerator;
return Rational(num, den);
}
bool operator== (const Rational& r) const {
return numerator == r.numerator && denominator == r.denominator;
}
bool operator!= (const Rational& r) const {
return !(*this == r);
}
bool operator> (const Rational& r) const {
int num = numerator * r.denominator - r.numerator * denominator;
return num > 0;
}
bool operator>= (const Rational& r) const {
return (*this > r) || (*this == r);
}
bool operator< (const Rational& r) const {
return !(*this >= r);
}
bool operator<= (const Rational& r) const {
return !(*this > r);
}
operator double() const {
return (double)numerator / denominator;
}
void print() const {
cout << numerator << "/" << denominator << endl;
}
};
int main() {
Rational r1(2, 3);
Rational r2(1, 4);
Rational r3 = r1 + r2;
r3.print(); // 输出 11/12
Rational r4 = r1 - r2;
r4.print(); // 输出 5/12
Rational r5 = r1 * r2;
r5.print(); // 输出 1/6
Rational r6 = r1 / r2;
r6.print(); // 输出 8/3
if (r1 == r2) {
cout << "r1 == r2" << endl;
} else {
cout << "r1 != r2" << endl;
}
if (r1 > r2) {
cout << "r1 > r2" << endl;
} else {
cout << "r1 <= r2" << endl;
}
double d = (double)r1;
cout << d << endl; // 输出 0.666667
return 0;
}
```
运行结果如下:
```
11/12
5/12
1/6
8/3
r1 != r2
r1 > r2
0.666667
```
阅读全文