(1)建立一个分数类。分数类的数据成员包括分子和分母,操作包括通分、比较、加、减、乘、除、求倒数、显示和输入。分数类定义如下:class Fraction{private: int above;
时间: 2024-02-02 20:03:12 浏览: 101
int below;
public:
Fraction(int a=0, int b=1); // 构造函数,a为分子,b为分母,默认为0/1
void reduction(); // 约分
int gcd(int a, int b); // 求最大公约数
Fraction operator+(const Fraction& f); // 加法运算符重载
Fraction operator-(const Fraction& f); // 减法运算符重载
Fraction operator*(const Fraction& f); // 乘法运算符重载
Fraction operator/(const Fraction& f); // 除法运算符重载
bool operator>(const Fraction& f); // 大于运算符重载
bool operator<(const Fraction& f); // 小于运算符重载
bool operator==(const Fraction& f); // 等于运算符重载
Fraction reciprocal(); // 求倒数
void display(); // 显示分数
void input(); // 输入分数
};
Fraction::Fraction(int a, int b) : above(a), below(b) {}
void Fraction::reduction() {
int g = gcd(above, below);
above /= g;
below /= g;
}
int Fraction::gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
Fraction Fraction::operator+(const Fraction& f) {
Fraction res(above * f.below + f.above * below, below * f.below);
res.reduction();
return res;
}
Fraction Fraction::operator-(const Fraction& f) {
Fraction res(above * f.below - f.above * below, below * f.below);
res.reduction();
return res;
}
Fraction Fraction::operator*(const Fraction& f) {
Fraction res(above * f.above, below * f.below);
res.reduction();
return res;
}
Fraction Fraction::operator/(const Fraction& f) {
Fraction res(above * f.below, below * f.above);
res.reduction();
return res;
}
bool Fraction::operator>(const Fraction& f) {
return above * f.below > below * f.above;
}
bool Fraction::operator<(const Fraction& f) {
return above * f.below < below * f.above;
}
bool Fraction::operator==(const Fraction& f) {
return above * f.below == below * f.above;
}
Fraction Fraction::reciprocal() {
Fraction res(below, above);
return res;
}
void Fraction::display() {
cout << above << "/" << below << endl;
}
void Fraction::input() {
cout << "请输入分子和分母,用空格隔开:";
cin >> above >> below;
reduction();
}
使用示例:
int main() {
Fraction f1(1, 2);
Fraction f2(3, 4);
Fraction f3 = f1 + f2;
f3.display();
Fraction f4 = f1 - f2;
f4.display();
Fraction f5 = f1 * f2;
f5.display();
Fraction f6 = f1 / f2;
f6.display();
bool b1 = f1 > f2;
bool b2 = f1 < f2;
bool b3 = f1 == f2;
cout << b1 << " " << b2 << " " << b3 << endl;
Fraction f7 = f1.reciprocal();
f7.display();
Fraction f8;
f8.input();
f8.display();
return 0;
}
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)