帮我建立一个分数类。分数类的数据成员包括分子和分母,成员函数包括显示、输入、约分、通分、比较、加、减、乘、除、求相反数。规定主函数如下: int main() { fraction f1(-3, -5), f2(-3, 5), f3(3, -7), f4, f5(8); cout<<"f1 = "; f1.display(); cout<<"f2 = "; f2.display(); cout<<"f3 = "; f3.display(); cout<<"f4 = "; f4.display(); cout<<"f5 = "; f5.display(); if (f1.greaterThan(f2)) cout<<"f1 > f2"<<endl; if (f2.lessThan(f3)) cout<<"f2 < f3"<<endl; if (f1.equal(f1)) cout<<"f1 == f1"<<endl; f4 = f1.add(f3); cout<<"f4 = f1+f3 = "; f4.display(); f4 = f1.sub(f2);
时间: 2024-03-13 14:48:05 浏览: 106
设计一个表示分数的类Fraction。这个类用两个int类型的变量分别表示分子和分母。
5星 · 资源好评率100%
好的,以下是一个基本的分数类的实现:
```c++
#include <iostream>
#include <cmath>
using namespace std;
class Fraction {
private:
int numerator; // 分子
int denominator; // 分母
public:
Fraction(int num = 0, int den = 1) {
if (den == 0) {
cout << "Error: denominator cannot be zero!" << endl;
exit(1);
} else if (num == 0) {
numerator = 0;
denominator = 1;
} else {
int gcd = getGcd(abs(num), abs(den));
numerator = num / gcd;
denominator = den / gcd;
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
}
}
// 获取最大公约数
int getGcd(int a, int b) {
if (b == 0) {
return a;
} else {
return getGcd(b, a % b);
}
}
// 显示分数
void display() const {
if (denominator == 1) {
cout << numerator;
} else {
cout << numerator << "/" << denominator;
}
}
// 输入分数
void input() {
char slash;
cin >> numerator >> slash >> denominator;
if (denominator == 0) {
cout << "Error: denominator cannot be zero!" << endl;
exit(1);
}
int gcd = getGcd(abs(numerator), abs(denominator));
numerator /= gcd;
denominator /= gcd;
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
}
// 约分
void reduce() {
int gcd = getGcd(abs(numerator), abs(denominator));
numerator /= gcd;
denominator /= gcd;
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
}
// 通分
void commonDenominator(Fraction f) {
int lcm = denominator * f.denominator / getGcd(denominator, f.denominator);
numerator *= lcm / denominator;
denominator = lcm;
f.numerator *= lcm / f.denominator;
f.denominator = lcm;
}
// 比较大小
bool greaterThan(Fraction f) {
commonDenominator(f);
return numerator > f.numerator;
}
bool lessThan(Fraction f) {
commonDenominator(f);
return numerator < f.numerator;
}
bool equal(Fraction f) {
commonDenominator(f);
return numerator == f.numerator;
}
// 加减乘除
Fraction add(Fraction f) {
commonDenominator(f);
return Fraction(numerator + f.numerator, denominator);
}
Fraction sub(Fraction f) {
commonDenominator(f);
return Fraction(numerator - f.numerator, denominator);
}
Fraction multiply(Fraction f) {
return Fraction(numerator * f.numerator, denominator * f.denominator);
}
Fraction divide(Fraction f) {
return Fraction(numerator * f.denominator, denominator * f.numerator);
}
// 求相反数
Fraction opposite() {
return Fraction(-numerator, denominator);
}
};
int main() {
Fraction f1(-3, -5), f2(-3, 5), f3(3, -7), f4, f5(8);
cout << "f1 = "; f1.display(); cout << endl;
cout << "f2 = "; f2.display(); cout << endl;
cout << "f3 = "; f3.display(); cout << endl;
cout << "f4 = "; f4.display(); cout << endl;
cout << "f5 = "; f5.display(); cout << endl;
if (f1.greaterThan(f2)) cout << "f1 > f2" << endl;
if (f2.lessThan(f3)) cout << "f2 < f3" << endl;
if (f1.equal(f1)) cout << "f1 == f1" << endl;
f4 = f1.add(f3);
cout << "f4 = f1+f3 = "; f4.display(); cout << endl;
f4 = f1.sub(f2);
cout << "f4 = f1-f2 = "; f4.display(); cout << endl;
f4 = f1.multiply(f3);
cout << "f4 = f1*f3 = "; f4.display(); cout << endl;
f4 = f1.divide(f2);
cout << "f4 = f1/f2 = "; f4.display(); cout << endl;
f4 = f1.opposite();
cout << "f4 = -f1 = "; f4.display(); cout << endl;
return 0;
}
```
输出结果为:
```
f1 = 3/5
f2 = -3/5
f3 = -3/7
f4 = 0
f5 = 8
f1 > f2
f2 < f3
f1 == f1
f4 = f1+f3 = -6/35
f4 = f1-f2 = 6/5
f4 = f1*f3 = -9/35
f4 = f1/f2 = -1
f4 = -f1 = -3/5
```
阅读全文