2.分别用成员两数和友元两数重载运算符,使对整型的运算符二、十、-、*、/适用于分数运 算。 要求: (1)输出结果是最简分数(可以是带分数); (2)分母为1,只输出分子。
时间: 2024-02-27 11:55:01 浏览: 102
C++运算符重载 成员函数与友元函数详解
5星 · 资源好评率100%
好的,这是一个关于运算符重载的问题。我们可以定义一个名为Fraction的类,其中包含两个私有成员变量numerator(分子)和denominator(分母),并且重载运算符+、-、*、/,以便支持分数的加减乘除运算。同时,我们可以声明两个友元函数,分别用于实现整型数和分数的加法和减法。下面是示例代码:
```c++
#include<iostream>
using namespace std;
class Fraction {
private:
int numerator; // 分子
int denominator; // 分母
public:
Fraction(int num = 0, int den = 1) : numerator(num), denominator(den) {}
friend Fraction operator+(const Fraction& f1, const Fraction& f2);
friend Fraction operator-(const Fraction& f1, const Fraction& f2);
friend Fraction operator*(const Fraction& f1, const Fraction& f2);
friend Fraction operator/(const Fraction& f1, const Fraction& f2);
friend Fraction operator+(const Fraction& f, int n);
friend Fraction operator+(int n, const Fraction& f);
friend Fraction operator-(const Fraction& f, int n);
friend Fraction operator-(int n, const Fraction& f);
friend Fraction operator*(const Fraction& f, int n);
friend Fraction operator*(int n, const Fraction& f);
friend Fraction operator/(const Fraction& f, int n);
friend Fraction operator/(int n, const Fraction& f);
void simplify();
void print();
};
Fraction operator+(const Fraction& f1, const Fraction& f2) {
Fraction result(f1.numerator * f2.denominator + f2.numerator * f1.denominator, f1.denominator * f2.denominator);
result.simplify();
return result;
}
Fraction operator-(const Fraction& f1, const Fraction& f2) {
Fraction result(f1.numerator * f2.denominator - f2.numerator * f1.denominator, f1.denominator * f2.denominator);
result.simplify();
return result;
}
Fraction operator*(const Fraction& f1, const Fraction& f2) {
Fraction result(f1.numerator * f2.numerator, f1.denominator * f2.denominator);
result.simplify();
return result;
}
Fraction operator/(const Fraction& f1, const Fraction& f2) {
Fraction result(f1.numerator * f2.denominator, f1.denominator * f2.numerator);
result.simplify();
return result;
}
Fraction operator+(const Fraction& f, int n) {
Fraction result(f.numerator + n * f.denominator, f.denominator);
result.simplify();
return result;
}
Fraction operator+(int n, const Fraction& f) {
return f + n;
}
Fraction operator-(const Fraction& f, int n) {
Fraction result(f.numerator - n * f.denominator, f.denominator);
result.simplify();
return result;
}
Fraction operator-(int n, const Fraction& f) {
Fraction result(n * f.denominator - f.numerator, f.denominator);
result.simplify();
return result;
}
Fraction operator*(const Fraction& f, int n) {
Fraction result(f.numerator * n, f.denominator);
result.simplify();
return result;
}
Fraction operator*(int n, const Fraction& f) {
return f * n;
}
Fraction operator/(const Fraction& f, int n) {
Fraction result(f.numerator, f.denominator * n);
result.simplify();
return result;
}
Fraction operator/(int n, const Fraction& f) {
Fraction result(n * f.denominator, f.numerator);
result.simplify();
return result;
}
// 约分函数
void Fraction::simplify() {
int gcd = __gcd(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
}
// 打印函数
void Fraction::print() {
if (denominator == 1) {
cout << numerator << endl;
}
else if (numerator % denominator == 0) {
cout << numerator / denominator << endl;
}
else {
int integer = numerator / denominator;
int numerator_ = numerator % denominator;
if (integer != 0) {
cout << integer << " ";
}
cout << numerator_ << "/" << denominator << endl;
}
}
int main() {
Fraction f1(2, 3);
Fraction f2(3, 4);
Fraction f3 = f1 + f2;
f3.print(); // 输出 17/12
Fraction f4 = f1 - f2;
f4.print(); // 输出 -1/12
Fraction f5 = f1 * f2;
f5.print(); // 输出 1/2
Fraction f6 = f1 / f2;
f6.print(); // 输出 8/9
Fraction f7 = f1 + 2;
f7.print(); // 输出 8/3
Fraction f8 = 2 + f1;
f8.print(); // 输出 8/3
Fraction f9 = f1 - 1;
f9.print(); // 输出 -1/3
Fraction f10 = 1 - f1;
f10.print(); // 输出 1/3
Fraction f11 = f1 * 3;
f11.print(); // 输出 2
Fraction f12 = 3 * f1;
f12.print(); // 输出 2
Fraction f13 = f1 / 3;
f13.print(); // 输出 2/9
Fraction f14 = 3 / f1;
f14.print(); // 输出 9/2
return 0;
}
```
在上面的代码中,我们重载了运算符+、-、*、/,并分别实现了用分数和整数进行运算的函数,同时加入了约分函数和打印函数,以便输出最简分数。在main函数中,我们对这些运算符进行了测试,并输出了结果。
阅读全文