设计一个Rational类,进行带分数的运算。要求:
(1)包含两个整数成员变量表示分子和分母。
(2)包含一个对所声明对象初始化的构造函数。不提供参数时,构造函数应提供默认值。分数存放成简化形式,例如分数“2/4”应在对象中存放成分子1和分母2的形式。
(3)采用成员函数的方式重载加法、减法运算符,采用友元函数的方式重载乘法和除法运算符,实现如下功能:
- a)两个Rational值相加,结果保存成简化形式并输出。
- b)两个Rational值相减,结果保存成简化形式并输出。
- c)两个Rational值相乘,结果保存成简化形式并输出。
- d)两个Rational值相除,结果保存成简化形式并输出。
- e)重载++运算符,实现对两个Rational值的自增操作,结果保存成简化形式并输出(1/2++的结果为3/2)。
- f)声明一个类对象Rational R1,R2;重载赋值运算符”=”,将两个Rational值分子和分母颠倒位置后,赋值给R1和R2,并简化输出。(2/3 变为 3/2,如果分母是1,直接输出整数形式。)
- g)重载输出运算符,按a/b形式打印Rational值,其中a为分子,b为分母。
输入为两行,每行代表一个分数,每行有两个int类型的数字,分别代表分数的分子和分母
输出:按照步骤(3)操作后的分数值,分子和分母用“/”分隔,每个操作一行。
样例输入:
1 2
5 6
样例输出:
4/3
-1/3
5/12
3/5
3/2 11/6
2 6/5
#include <iostream>
#include <algorithm>
using namespace std;
class Rational {
public:
friend Rational operator*(Rational& lhs, Rational& rhs); // 声明友元
friend Rational operator/(Rational& lhs, Rational& rhs); // 声明友元
friend ostream& operator<<(ostream& output, Rational& r); // 声明友元
int gcd(int a, int b) { // 辗转相除求gcd
if (b == 0) return a;
return gcd(b, a%b);
}
Rational(int numerator = 0, int denominator = 1) { // 构造函数
int g = gcd(abs(numerator), abs(denominator)); // 求最大公约数
num = numerator / g;
den = denominator / g;
if (den < 0) { // 统一分母为正数
num *= -1;
den *= -1;
}
}
Rational& operator+=(Rational& rhs) { // 加法运算符重载
int lcm = den * rhs.den / gcd(den, rhs.den);
num = num * (lcm / den) + rhs.num * (lcm / rhs.den);
den = lcm;
int g = gcd(abs(num), abs(den));
num /= g;
den /= g;
if (den < 0) {
num *= -1;
den *= -1;
}
return *this;
}
Rational& operator-=(Rational& rhs) { // 减法运算符重载
int lcm = den * rhs.den / gcd(den, rhs.den);
num = num * (lcm / den) - rhs.num * (lcm / rhs.den);
den = lcm;
int g = gcd(abs(num), abs(den));
num /= g;
den /= g;
if (den < 0) {
num *= -1;
den *= -1;
}
return *this;
}
Rational& operator++() { // 前置++运算符重载
num += den;
int g = gcd(abs(num), abs(den));
num /= g;
den /= g;
if (den < 0) {
num *= -1;
den *= -1;
}
return *this;
}
Rational operator=(Rational& rhs) { // 赋值运算符重载
num = rhs.num;
den = rhs.den;
return *this;
}
private:
int num, den;
};
Rational operator+(Rational& lhs, Rational& rhs) { // 加法运算符重载
Rational ret = lhs;
ret += rhs;
return ret;
}
Rational operator-(Rational& lhs, Rational& rhs) { // 减法运算符重载
Rational ret = lhs;
ret -= rhs;
return ret;
}
Rational operator*(Rational& lhs, Rational& rhs) { // 乘法运算符重载
int num = lhs.num * rhs.num, den = lhs.den * rhs.den;
int g = lhs.gcd(abs(num), abs(den));
num /= g;
den /= g;
if (den < 0) {
num *= -1;
den *= -1;
}
return Rational(num, den);
}
Rational operator/(Rational& lhs, Rational& rhs) { // 除法运算符重载
int num = lhs.num * rhs.den, den = lhs.den * rhs.num;
int g = lhs.gcd(abs(num), abs(den));
num /= g;
den /= g;
if (den < 0) {
num *= -1;
den *= -1;
}
return Rational(num, den);
}
ostream& operator<<(ostream& output, Rational& r) { // 输出运算符重载
if (r.den == 1) output << r.num;
else output << r.num << '/' << r.den;
return output;
}
int main() {
int n1, d1, n2, d2;
cin >> n1 >> d1 >> n2 >> d2;
Rational r1(n1, d1), r2(n2, d2);
Rational r3=r1+r2;
Rational r4=r1-r2;
Rational r5=r1*r2;
Rational r6=r1/r2;
cout << r3 << endl;
cout << r4 << endl;
cout << r5 << endl;
cout << r6 << endl;
cout << ++r1 << " " << ++r2 << endl;
Rational r7 = Rational(d1, n1); // 注意这里转换分子和分母的位置
Rational r8 = Rational(d2, n2);
cout << r7 << " " << r8 << endl;
return 0;
}