编程:设计一个类,用自己的成员函数重载运算符,使对整型的运算符=、+、-、*、/适用于分数运算。要求: (1)输出结果是最简分数(可以是带分数); (2)分母为1,只输出分子。 (3)用友元函数重载运算符,使对整型的运 算符=、+、-、*、/适用于分数运算。
时间: 2023-07-10 08:12:19 浏览: 97
好的,以下是实现上述要求的代码:
```c++
#include <iostream>
using namespace std;
int gcd(int a, int b) { // 求最大公约数
return b == 0 ? a : gcd(b, a % b);
}
class Fraction {
private:
int numerator; // 分子
int denominator; // 分母
public:
Fraction(int num = 0, int den = 1) : numerator(num), denominator(den) {
simplify(); // 构造时进行约分
}
void simplify() { // 约分
int g = gcd(numerator, denominator);
numerator /= g;
denominator /= g;
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
}
void print() const { // 输出分数(或带分数)
if (denominator == 1) cout << numerator << endl;
else if (numerator % denominator == 0) cout << numerator / denominator << endl;
else if (numerator / denominator == 0) cout << numerator << "/" << denominator << endl;
else cout << numerator / denominator << " " << abs(numerator % denominator) << "/" << denominator << endl;
}
Fraction operator+ (const Fraction& f) const { // 重载加法运算符
int num = numerator * f.denominator + f.numerator * denominator;
int den = denominator * f.denominator;
Fraction res(num, den);
return res;
}
Fraction operator- (const Fraction& f) const { // 重载减法运算符
int num = numerator * f.denominator - f.numerator * denominator;
int den = denominator * f.denominator;
Fraction res(num, den);
return res;
}
Fraction operator* (const Fraction& f) const { // 重载乘法运算符
int num = numerator * f.numerator;
int den = denominator * f.denominator;
Fraction res(num, den);
return res;
}
Fraction operator/ (const Fraction& f) const { // 重载除法运算符
int num = numerator * f.denominator;
int den = denominator * f.numerator;
Fraction res(num, den);
return res;
}
Fraction& operator= (const Fraction& f) { // 重载赋值运算符
numerator = f.numerator;
denominator = f.denominator;
simplify();
return *this;
}
friend Fraction operator+ (int n, const Fraction& f) { // 重载友元加法运算符
Fraction res(n * f.denominator + f.numerator, f.denominator);
res.simplify();
return res;
}
friend Fraction operator- (int n, const Fraction& f) { // 重载友元减法运算符
Fraction res(n * f.denominator - f.numerator, f.denominator);
res.simplify();
return res;
}
friend Fraction operator* (int n, const Fraction& f) { // 重载友元乘法运算符
Fraction res(n * f.numerator, f.denominator);
res.simplify();
return res;
}
friend Fraction operator/ (int n, const Fraction& f) { // 重载友元除法运算符
Fraction res(n * f.denominator, f.numerator);
res.simplify();
return res;
}
};
int main() {
Fraction f1(1, 2);
Fraction f2(2, 3);
Fraction f3;
f3 = f1 + f2;
f3.print(); // 输出:7/6
f3 = f1 - f2;
f3.print(); // 输出:-1/6
f3 = f1 * f2;
f3.print(); // 输出:1/3
f3 = f1 / f2;
f3.print(); // 输出:3/4
f3 = 1 + f1;
f3.print(); // 输出:3/2
f3 = 1 - f2;
f3.print(); // 输出:1/3
f3 = 2 * f1;
f3.print(); // 输出:1
f3 = 2 / f2;
f3.print(); // 输出:3
return 0;
}
```
以上代码中,我们封装了一个分数类 `Fraction`,并重载了运算符 `+`、`-`、`*`、`/` 和 `=`。此外,我们还用友元函数重载了运算符 `+`、`-`、`*`、`/`,使之可以支持整型和分数之间的运算。在 `Fraction` 类中,我们定义了 `simplify` 函数,用于对分数进行约分;定义了 `print` 函数,用于输出最简分数(或带分数);定义了重载运算符的函数,实现了分数的加、减、乘、除和赋值运算。在友元函数中,我们将整型转化为分数进行计算,并最终输出最简分数(或带分数)。
阅读全文