设计一个类,用自己的成员函数重载运算符,使对整型的运算符=、+、-、*、/ 适用于分数运算。要求: (1)输出结果是最简分数(可以是带分数); (2)分母为1,只输出分子。 (3)用友元函数重载运算符,使对整型的运算符=、+、-、*、/ 适用于分数运算。
时间: 2023-07-10 16:12:17 浏览: 47
好的,以下是我设计的类Fraction:
```cpp
#include<iostream>
using namespace std;
class Fraction {
private:
int numerator; // 分子
int denominator; // 分母
int gcd(int a, int b); // 求最大公约数
public:
Fraction(int n = 0, int d = 1); // 构造函数
Fraction(const Fraction& f); // 拷贝构造函数
~Fraction(); // 析构函数
Fraction& operator=(const Fraction& f); // 赋值运算符重载
Fraction operator+(const Fraction& f); // 加法运算符重载
Fraction operator-(const Fraction& f); // 减法运算符重载
Fraction operator*(const Fraction& f); // 乘法运算符重载
Fraction operator/(const Fraction& f); // 除法运算符重载
friend istream& operator>>(istream& in, Fraction& f); // 输入运算符重载
friend ostream& operator<<(ostream& out, Fraction& f); // 输出运算符重载
};
Fraction::Fraction(int n, int d) {
if (d == 0) {
cout << "Error: Denominator can not be 0!" << endl;
exit(1);
}
if (n == 0) {
numerator = 0;
denominator = 1;
}
else {
int g = gcd(abs(n), abs(d));
numerator = n / g;
denominator = d / g;
if (denominator < 0) { // 保证分母为正数
numerator = -numerator;
denominator = -denominator;
}
}
}
Fraction::Fraction(const Fraction& f) {
numerator = f.numerator;
denominator = f.denominator;
}
Fraction::~Fraction() {}
int Fraction::gcd(int a, int b) {
if (b == 0) {
return a;
}
else {
return gcd(b, a % b);
}
}
Fraction& Fraction::operator=(const Fraction& f) {
numerator = f.numerator;
denominator = f.denominator;
return *this;
}
Fraction Fraction::operator+(const Fraction& f) {
int n = numerator * f.denominator + f.numerator * denominator;
int d = denominator * f.denominator;
return Fraction(n, d);
}
Fraction Fraction::operator-(const Fraction& f) {
int n = numerator * f.denominator - f.numerator * denominator;
int d = denominator * f.denominator;
return Fraction(n, d);
}
Fraction Fraction::operator*(const Fraction& f) {
int n = numerator * f.numerator;
int d = denominator * f.denominator;
return Fraction(n, d);
}
Fraction Fraction::operator/(const Fraction& f) {
if (f.numerator == 0) {
cout << "Error: Denominator can not be 0!" << endl;
exit(1);
}
int n = numerator * f.denominator;
int d = denominator * f.numerator;
return Fraction(n, d);
}
istream& operator>>(istream& in, Fraction& f) {
char ch;
in >> f.numerator >> ch >> f.denominator;
if (f.denominator == 0) {
cout << "Error: Denominator can not be 0!" << endl;
exit(1);
}
Fraction tmp(f.numerator, f.denominator);
f = tmp;
return in;
}
ostream& operator<<(ostream& out, Fraction& f) {
if (f.denominator == 1) { // 分母为1,只输出分子
out << f.numerator;
}
else if (abs(f.numerator) > f.denominator) { // 输出带分数
int integer = f.numerator / f.denominator;
int n = abs(f.numerator) - abs(integer * f.denominator);
int g = Fraction::gcd(n, f.denominator);
if (integer < 0) {
out << "-";
}
out << abs(integer) << " " << n / g << "/" << f.denominator / g;
}
else { // 输出真分数
int g = Fraction::gcd(f.numerator, f.denominator);
if (f.numerator < 0) {
out << "-";
}
out << abs(f.numerator) / g << "/" << abs(f.denominator) / g;
}
return out;
}
```
这个类实现了基本的分数运算,包括构造函数、拷贝构造函数、析构函数、赋值运算符重载、加法运算符重载、减法运算符重载、乘法运算符重载、除法运算符重载、输入运算符重载、输出运算符重载。
其中,gcd函数用于求最大公约数,输入运算符重载和输出运算符重载可以根据不同情况输出最简分数或带分数。
使用示例:
```cpp
int main() {
Fraction f1(2, 3), f2(3, 4), f3;
f3 = f1 + f2;
cout << f1 << " + " << f2 << " = " << f3 << endl;
f3 = f1 - f2;
cout << f1 << " - " << f2 << " = " << f3 << endl;
f3 = f1 * f2;
cout << f1 << " * " << f2 << " = " << f3 << endl;
f3 = f1 / f2;
cout << f1 << " / " << f2 << " = " << f3 << endl;
Fraction f4;
cin >> f4;
cout << "f4 = " << f4 << endl;
return 0;
}
```
输出结果:
```
2/3 + 3/4 = 17/12
2/3 - 3/4 = -1/12
2/3 * 3/4 = 1/2
2/3 / 3/4 = 8/9
5/6
f4 = 5/6
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)