建立一个分数类。分数类的数据成员包括分子和分母,操作包括约分、通分、比较、加、减、乘、除、求倒数、显示和输入,并将其集合类和分数类的集合类型或分数类型参数改为引用,并添加复制构造函数。
时间: 2023-04-09 21:04:20 浏览: 135
好的,以下是建立一个分数类的代码:
```cpp
#include <iostream>
using namespace std;
class Fraction {
private:
int numerator; // 分子
int denominator; // 分母
public:
Fraction(int num = 0, int den = 1) : numerator(num), denominator(den) {}
// 约分
void reduce() {
int gcd = __gcd(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
}
// 通分
void commonDenominator(Fraction& other) {
int lcm = denominator * other.denominator / __gcd(denominator, other.denominator);
numerator *= lcm / denominator;
denominator = lcm;
other.numerator *= lcm / other.denominator;
other.denominator = lcm;
}
// 比较
bool operator<(const Fraction& other) const {
return numerator * other.denominator < other.numerator * denominator;
}
bool operator==(const Fraction& other) const {
return numerator * other.denominator == other.numerator * denominator;
}
// 加
Fraction operator+(const Fraction& other) const {
Fraction res(numerator, denominator);
res.commonDenominator(const_cast<Fraction&>(other));
res.numerator += other.numerator;
res.reduce();
return res;
}
// 减
Fraction operator-(const Fraction& other) const {
Fraction res(numerator, denominator);
res.commonDenominator(const_cast<Fraction&>(other));
res.numerator -= other.numerator;
res.reduce();
return res;
}
// 乘
Fraction operator*(const Fraction& other) const {
Fraction res(numerator * other.numerator, denominator * other.denominator);
res.reduce();
return res;
}
// 除
Fraction operator/(const Fraction& other) const {
Fraction res(numerator * other.denominator, denominator * other.numerator);
res.reduce();
return res;
}
// 求倒数
Fraction reciprocal() const {
return Fraction(denominator, numerator);
}
// 显示
void display() const {
cout << numerator << "/" << denominator << endl;
}
// 输入
void input() {
cin >> numerator >> denominator;
reduce();
}
};
// 复制构造函数
Fraction::Fraction(const Fraction& other) {
numerator = other.numerator;
denominator = other.denominator;
}
// 集合类
template <typename T>
class Set {
private:
T* data;
int size;
public:
Set(int s = 0) : size(s) {
data = new T[size];
}
Set(const Set& other) : size(other.size) {
data = new T[size];
for (int i = 0; i < size; i++) {
data[i] = other.data[i];
}
}
~Set() {
delete[] data;
}
Set& operator=(const Set& other) {
if (this != &other) {
delete[] data;
size = other.size;
data = new T[size];
for (int i = 0; i < size; i++) {
data[i] = other.data[i];
}
}
return *this;
}
void insert(const T& val) {
for (int i = 0; i < size; i++) {
if (data[i] == val) {
return;
}
}
T* temp = new T[size + 1];
for (int i = 0; i < size; i++) {
temp[i] = data[i];
}
temp[size++] = val;
delete[] data;
data = temp;
}
void remove(const T& val) {
for (int i = 0; i < size; i++) {
if (data[i] == val) {
T* temp = new T[size - 1];
for (int j = 0; j < i; j++) {
temp[j] = data[j];
}
for (int j = i + 1; j < size; j++) {
temp[j - 1] = data[j];
}
size--;
delete[] data;
data = temp;
return;
}
}
}
void display() const {
for (int i = 0; i < size; i++) {
cout << data[i] << " ";
}
cout << endl;
}
};
int main() {
Fraction f1(2, 3), f2(3, 4), f3(4, 5);
f1.display();
f2.display();
f3.display();
Fraction f4 = f1 + f2;
f4.display();
Fraction f5 = f1 - f2;
f5.display();
Fraction f6 = f1 * f2;
f6.display();
Fraction f7 = f1 / f2;
f7.display();
Fraction f8 = f1.reciprocal();
f8.display();
Set<Fraction> s1(2);
s1.insert(f1);
s1.insert(f2);
s1.insert(f3);
s1.display();
s1.remove(f2);
s1.display();
Set<Fraction>& s2 = s1;
s2.insert(f4);
s2.display();
Set<Fraction> s3 = s1;
s3.display();
return 0;
}
```
希望这个代码能够满足你的需求!
阅读全文