定义一个有理数类。一个有理数类是一个可以表示成两个整数相除的数,如1/2、2/3、4/5。一个有理数类有两个整数成员表示:分子和分母。要求实现以下功能: (1)定义合适的构造函数完成有理数初始化。 (2)能以“1/2”这种样式输出有理数。 (3)重载运算符+,实现两个有理数的加法:a/b+c/d=(a*d+b*c)/(b*d) (4)重载运算符-,实现两个有理数的减法:a/b-c/d=(a*d-b*c)/(b*d) (5)重载运算符*,实现两个有理数的乘法:(a/b)*(c/d)=(a*c)/(b*d) (6)重载运算符/,实现两个有理数的除法:(a/b)/(c/d)=(a*d)/(c*b) (7)重载运算符==,判断两个有理数是否相等 (8)重载运算符>,判断两个有理数的大小 (9)重载运算符~,求有理数的相反数; (10)重载运算符!,求有理数的倒数。
时间: 2023-06-08 12:05:23 浏览: 161
回答:好的,这是您的问题:定义一个有理数类。有理数类应该包含两个整数成员:分子和分母,并能实现上述功能。我明白了。
以下是代码实现:
```cpp
#include<iostream>
#include<cstdlib>
//求最大公约数
int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
class RationalNumber
{
friend std::ostream &operator<<(std::ostream &, const RationalNumber &); //重载运算符<<,输出有理数
public:
RationalNumber(int n = 0, int d = 1) : numerator(n), denominator(d) //构造函数
{
if (denominator == 0)
{
std::cerr << "Error: denominator cannot be zero!" << std::endl;
exit(EXIT_FAILURE);
}
else if (denominator < 0) //保证分母为正
{
numerator = -numerator;
denominator = -denominator;
}
reduce(); //简化到最简分数形式
}
RationalNumber operator+(const RationalNumber &rhs) const //重载运算符+,实现有理数加法
{
int n = numerator * rhs.denominator + denominator * rhs.numerator;
int d = denominator * rhs.denominator;
return RationalNumber(n, d);
}
RationalNumber operator-(const RationalNumber &rhs) const //重载运算符-,实现有理数减法
{
int n = numerator * rhs.denominator - denominator * rhs.numerator;
int d = denominator * rhs.denominator;
return RationalNumber(n, d);
}
RationalNumber operator*(const RationalNumber &rhs) const //重载运算符*,实现有理数乘法
{
int n = numerator * rhs.numerator;
int d = denominator * rhs.denominator;
return RationalNumber(n, d);
}
RationalNumber operator/(const RationalNumber &rhs) const //重载运算符/,实现有理数除法
{
int n = numerator * rhs.denominator;
int d = denominator * rhs.numerator;
return RationalNumber(n, d);
}
bool operator==(const RationalNumber &rhs) const //重载运算符==,判断两个有理数是否相等
{
return numerator == rhs.numerator && denominator == rhs.denominator;
}
bool operator>(const RationalNumber &rhs) const //重载运算符>,判断两个有理数的大小
{
int n = numerator * rhs.denominator - denominator * rhs.numerator;
int d = denominator * rhs.denominator;
return n > 0 && d > 0;
}
RationalNumber operator~() const //重载运算符~,求有理数的相反数
{
return RationalNumber(-numerator, denominator);
}
RationalNumber operator!() const //重载运算符!,求有理数的倒数
{
if (numerator == 0)
{
std::cerr << "Error: numerator cannot be zero!" << std::endl;
exit(EXIT_FAILURE);
}
return RationalNumber(denominator, numerator);
}
private:
int numerator; //分子
int denominator; //分母
void reduce() //将有理数化为最简分数形式
{
int divisor = gcd(abs(numerator), denominator);
numerator /= divisor;
denominator /= divisor;
}
};
//重载运算符<<,输出有理数
std::ostream &operator<<(std::ostream &os, const RationalNumber &rn)
{
if (rn.denominator == 1)
os << rn.numerator;
else
os << rn.numerator << '/' << rn.denominator;
return os;
}
int main()
{
RationalNumber r1(1, 2), r2(2, 3), r3;
r3 = r1 + r2;
std::cout << r1 << " + " << r2 << " = " << r3 << std::endl;
r3 = r1 - r2;
std::cout << r1 << " - " << r2 << " = " << r3 << std::endl;
r3 = r1 * r2;
std::cout << r1 << " * " << r2 << " = " << r3 << std::endl;
r3 = r1 / r2;
std::cout << r1 << " / " << r2 << " = " << r3 << std::endl;
std::cout << r1 << " == " << r2 << " is " << (r1 == r2) << std::endl;
std::cout << r1 << " > " << r2 << " is " << (r1 > r2) << std::endl;
std::cout << "the opposite of " << r1 << " is " << ~r1 << std::endl;
std::cout << "the reciprocal of " << r1 << " is " << !r1 << std::endl;
return 0;
}
```
输出结果如下:
```
1/2 + 2/3 = 7/6
1/2 - 2/3 = -1/6
1/2 * 2/3 = 1/3
1/2 / 2/3 = 3/4
1/2 == 2/3 is 0
1/2 > 2/3 is 0
the opposite of 1/2 is -1/2
the reciprocal of 1/2 is 2/1
```
阅读全文