c++实现分数类fraction
时间: 2023-10-15 17:04:31 浏览: 226
下面是一个简单的 C++ 分数类 `fraction` 的实现,它包含了分数的四则运算和比较运算。
```cpp
#include <iostream>
using namespace std;
class fraction {
private:
int num; // 分子
int den; // 分母
public:
fraction(int n = 0, int d = 1) {
if (d == 0) {
cout << "分母不能为0!" << endl;
exit(1);
}
if (d < 0) {
n = -n;
d = -d;
}
num = n;
den = d;
reduce();
}
int getNum() const { return num; }
int getDen() const { return den; }
fraction operator-() const { return fraction(-num, 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 bool operator==(const fraction& f1, const fraction& f2);
friend bool operator!=(const fraction& f1, const fraction& f2);
friend bool operator<(const fraction& f1, const fraction& f2);
friend bool operator>(const fraction& f1, const fraction& f2);
friend bool operator<=(const fraction& f1, const fraction& f2);
friend bool operator>=(const fraction& f1, const fraction& f2);
friend istream& operator>>(istream& in, fraction& f);
friend ostream& operator<<(ostream& out, const fraction& f);
private:
void reduce() {
int gcd = getGCD(num, den);
num /= gcd;
den /= gcd;
}
int getGCD(int a, int b) {
if (b == 0) return a;
return getGCD(b, a % b);
}
};
fraction operator+(const fraction& f1, const fraction& f2) {
return fraction(f1.getNum() * f2.getDen() + f2.getNum() * f1.getDen(),
f1.getDen() * f2.getDen());
}
fraction operator-(const fraction& f1, const fraction& f2) {
return f1 + (-f2);
}
fraction operator*(const fraction& f1, const fraction& f2) {
return fraction(f1.getNum() * f2.getNum(), f1.getDen() * f2.getDen());
}
fraction operator/(const fraction& f1, const fraction& f2) {
return fraction(f1.getNum() * f2.getDen(), f1.getDen() * f2.getNum());
}
bool operator==(const fraction& f1, const fraction& f2) {
return f1.getNum() == f2.getNum() && f1.getDen() == f2.getDen();
}
bool operator!=(const fraction& f1, const fraction& f2) {
return !(f1 == f2);
}
bool operator<(const fraction& f1, const fraction& f2) {
return f1.getNum() * f2.getDen() < f2.getNum() * f1.getDen();
}
bool operator>(const fraction& f1, const fraction& f2) {
return f2 < f1;
}
bool operator<=(const fraction& f1, const fraction& f2) {
return !(f2 < f1);
}
bool operator>=(const fraction& f1, const fraction& f2) {
return !(f1 < f2);
}
istream& operator>>(istream& in, fraction& f) {
char slash;
in >> f.num >> slash >> f.den;
f.reduce();
return in;
}
ostream& operator<<(ostream& out, const fraction& f) {
out << f.getNum() << '/' << f.getDen();
return out;
}
int main() {
fraction f1(1, 2), f2(3, 4), f3;
cout << "f1 = " << f1 << endl;
cout << "f2 = " << f2 << endl;
cout << "f3 = " << f3 << endl;
cout << "请输入一个分数:";
cin >> f3;
cout << "f3 = " << f3 << endl;
cout << "f1 + f2 = " << f1 + f2 << endl;
cout << "f1 - f2 = " << f1 - f2 << endl;
cout << "f1 * f2 = " << f1 * f2 << endl;
cout << "f1 / f2 = " << f1 / f2 << endl;
cout << "f1 == f2 ? " << (f1 == f2) << endl;
cout << "f1 != f2 ? " << (f1 != f2) << endl;
cout << "f1 < f2 ? " << (f1 < f2) << endl;
cout << "f1 > f2 ? " << (f1 > f2) << endl;
cout << "f1 <= f2 ? " << (f1 <= f2) << endl;
cout << "f1 >= f2 ? " << (f1 >= f2) << endl;
return 0;
}
```
上述实现中,分数类 `fraction` 的成员变量包括分子 `num` 和分母 `den`,它们在构造函数中被初始化。构造函数中还进行了分母为零的判断和分数的约分处理。`fraction` 类重载了一些运算符,包括一元负号运算符、二元加减乘除运算符和比较运算符。此外,还重载了输入输出流运算符,方便用户输入和输出分数。在主函数中,演示了如何使用这些运算符进行分数的运算和比较。
阅读全文