Rational(int nn=1,int mm=1);
时间: 2024-05-30 21:15:47 浏览: 14
This is a constructor function for a class named Rational, which creates an instance of the class with two integer values nn and mm. The purpose of this constructor is to initialize the numerator and denominator of a rational number. If no arguments are provided, the default values of 1 are used for both the numerator and denominator.
相关问题
描述有理数的 Rational 类如下,请补充类的其他成员使其能够执行各种运算。 class Rational{ int m; // 分母 int n; // 分子 void simple( ); //约分 public: Rational(int nn=1,int mm=1);//构造 …… }; 1)重载算术运算符“+”、“-”、“*”、“/”,使之能够适用于各种有理数的四则运算。 2)重载比较运算符“>”、“ <=” 和“==”,使之能够比较两个有理数。 3)重载运算符“<<”,使其能以规范的方式输出分数,如 1/2,-1/3,分母不能为 0。
class Rational{
private:
int m; // 分母
int n; // 分子
void simple( ); //约分
public:
Rational(int nn=1,int mm=1);//构造函数
Rational operator+(const Rational& r) const; //重载算术运算符“+”
Rational operator-(const Rational& r) const; //重载算术运算符“-”
Rational operator*(const Rational& r) const; //重载算术运算符“*”
Rational operator/(const Rational& r) const; //重载算术运算符“/”
bool operator>(const Rational& r) const; //重载比较运算符“>”
bool operator<=(const Rational& r) const; //重载比较运算符“<=”
bool operator==(const Rational& r) const; //重载比较运算符“==”
friend std::ostream& operator<<(std::ostream& os, const Rational& r); //重载运算符“<<”
};
Rational::Rational(int nn, int mm) : n(nn), m(mm) {}
void Rational::simple() {
int gcd = std::gcd(n, m);
n /= gcd;
m /= gcd;
}
Rational Rational::operator+(const Rational& r) const {
Rational res(n * r.m + m * r.n, m * r.m);
res.simple();
return res;
}
Rational Rational::operator-(const Rational& r) const {
Rational res(n * r.m - m * r.n, m * r.m);
res.simple();
return res;
}
Rational Rational::operator*(const Rational& r) const {
Rational res(n * r.n, m * r.m);
res.simple();
return res;
}
Rational Rational::operator/(const Rational& r) const {
Rational res(n * r.m, m * r.n);
res.simple();
return res;
}
bool Rational::operator>(const Rational& r) const {
return n * r.m > m * r.n;
}
bool Rational::operator<=(const Rational& r) const {
return !(*this > r);
}
bool Rational::operator==(const Rational& r) const {
return n * r.m == m * r.n;
}
std::ostream& operator<<(std::ostream& os, const Rational& r) {
if (r.m == 0) {
os << "分母不能为0";
} else if (r.n == 0) {
os << 0;
} else {
os << r.n << "/" << r.m;
}
return os;
}
定义一个有理数类(num为分母、den为分子),并重载比较运算符 < ,比较两个有理数的大小。class Rational { public: Rational(int num=0, int den=1); int operator<(Rational r); private: long num; long den; } Rational a(2,3), b(3,4)
下面是定义有理数类并重载比较运算符 < 的代码:
```c++
#include <iostream>
using namespace std;
class Rational {
public:
Rational(int num=0, int den=1);
bool operator<(const Rational& r) const;
private:
long num;
long den;
};
Rational::Rational(int num, int den) {
if (den == 0) {
cout << "Error: denominator can not be zero!" << endl;
exit(1);
}
int sign = 1;
if (num < 0) { sign = -sign; num = -num; }
if (den < 0) { sign = -sign; den = -den; }
long d = gcd(num, den);
this->num = sign * (num / d);
this->den = den / d;
}
bool Rational::operator<(const Rational& r) const {
return num * r.den < den * r.num;
}
int main() {
Rational a(2, 3), b(3, 4);
if (a < b) {
cout << "a < b" << endl;
} else {
cout << "a >= b" << endl;
}
return 0;
}
```
其中,gcd 函数用于求两个整数的最大公因数:
```c++
long gcd(long a, long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
```
注意,在比较运算符 < 中,我们使用 const 关键字来表示该函数不会修改类的成员变量。此外,我们还需要通过 & 来表示参数是一个常引用,以避免复制对象。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)