在C++中,分数不是预先定义的,需建立一个分数类,使之具有下述功能:能防止分母为“0”,当分数不是最简形式时进行约分以及避免分母为负数。用重载运算符完成加法、减法、乘法以及除法等四则运算。并判断两分数相不相等
时间: 2024-01-24 14:17:02 浏览: 36
好的,我可以帮你实现一个简单的分数类,具有上述功能。这里提供一个参考实现:
```cpp
#include <iostream>
#include <algorithm>
using namespace std;
// 分数类定义
class Fraction {
private:
int numerator; // 分子
int denominator; // 分母
public:
// 构造函数
Fraction(int num = 0, int den = 1) {
set(num, den);
}
// 设置分数
void set(int num, int den) {
if (den == 0) {
cout << "Error: denominator can't be zero!" << endl;
exit(1);
}
numerator = num;
denominator = den;
simplify(); // 约分
}
// 获取分子
int getNumerator() const {
return numerator;
}
// 获取分母
int getDenominator() const {
return denominator;
}
// 加法
Fraction operator+(const Fraction& other) const {
int num = numerator * other.denominator + other.numerator * denominator;
int den = denominator * other.denominator;
return Fraction(num, den);
}
// 减法
Fraction operator-(const Fraction& other) const {
int num = numerator * other.denominator - other.numerator * denominator;
int den = denominator * other.denominator;
return Fraction(num, den);
}
// 乘法
Fraction operator*(const Fraction& other) const {
int num = numerator * other.numerator;
int den = denominator * other.denominator;
return Fraction(num, den);
}
// 除法
Fraction operator/(const Fraction& other) const {
if (other.numerator == 0) {
cout << "Error: division by zero!" << endl;
exit(1);
}
int num = numerator * other.denominator;
int den = denominator * other.numerator;
return Fraction(num, den);
}
// 判断相等
bool operator==(const Fraction& other) const {
return numerator == other.numerator && denominator == other.denominator;
}
// 约分
void simplify() {
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
int gcd = __gcd(abs(numerator), denominator);
numerator /= gcd;
denominator /= gcd;
}
// 输出分数
friend ostream& operator<<(ostream& os, const Fraction& f) {
os << f.numerator;
if (f.denominator != 1) {
os << '/' << f.denominator;
}
return os;
}
};
int main() {
Fraction f1(2, 3);
Fraction f2(1, 2);
Fraction f3 = f1 + f2;
Fraction f4 = f1 - f2;
Fraction f5 = f1 * f2;
Fraction f6 = f1 / f2;
cout << f1 << " + " << f2 << " = " << f3 << endl;
cout << f1 << " - " << f2 << " = " << f4 << endl;
cout << f1 << " * " << f2 << " = " << f5 << endl;
cout << f1 << " / " << f2 << " = " << f6 << endl;
cout << f1 << " == " << f2 << " ? " << (f1 == f2) << endl;
cout << f1 << " == " << f3 << " ? " << (f1 == f3) << endl;
return 0;
}
```
以上代码定义了一个 `Fraction` 类,包含分子和分母两个私有成员变量,以及构造函数、设置函数、四则运算重载、约分函数、判断相等运算重载和输出运算重载等公有成员函数。其中,约分使用了 C++11 中的 `__gcd()` 函数来计算最大公约数。
你可以通过调用类中的函数来实现各种分数操作,例如:
```cpp
Fraction f1(2, 3);
Fraction f2(1, 2);
Fraction f3 = f1 + f2;
cout << f1 << " + " << f2 << " = " << f3 << endl;
```
以上代码将分数对象 `f1` 和 `f2` 相加,结果存储在 `f3` 中,并输出结果。
你可以根据自己的需求修改代码,例如添加其他运算符重载、比较运算符重载等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)