Create a class called Rational for performing arithmetic with fractions. Use integer variables to represent the private data of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. For example, the fraction would be stored in the object as 1 in the numerator and 2 in the denominator. In order to compute the reduced form, you need to write a reduction function which uses the Euclidean algorithm to get the greatest common divisor (GCD) of the numerator and denominator first and then divides GCD to get the reduced numerator and denominator. Provide public member functions that perform each of the following tasks:(a) Subtract a Rational number from the other Rational number. The result should be stored in reduced form. (b) Divide a Rational number by the other Rational number. The result should be stored in reduced form. (c) Print Rational numbers in the form a/b, where a is the numerator and b is the denominator. (d) Compare two Rational numbers to make sure which one is smaller or they are equal. (1 for the first number, 2 for the second number and 0 if they are equal) Please also write a main function to prompt the user to input two Rational numbers . Subtract one number from the other from these two numbers using (a) and then print the result using (c). Divide one number from the other from these two numbers using (b) and then print the result using (c). Compare these two Rational numbers using (d) and indicate which one is smaller or they are equal.用c++ 寫出和上面不一樣的版本,要用using namespace std; 的版本,並且要有註解
时间: 2024-02-19 11:58:11 浏览: 147
好的,以下是带有注释的代码:
```cpp
#include <iostream>
using namespace std;
class Rational {
private:
int numerator; // 分子
int denominator; // 分母
// 辗转相除法求最大公约数
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
// 约分函数
void reduction() {
int divisor = gcd(numerator, denominator);
numerator /= divisor;
denominator /= divisor;
}
public:
// 构造函数
Rational(int n = 0, int d = 1) {
numerator = n;
denominator = d;
reduction(); // 初始化时就进行约分
}
// 减法
Rational subtract(Rational other) {
int new_numerator = numerator * other.denominator - denominator * other.numerator;
int new_denominator = denominator * other.denominator;
Rational result(new_numerator, new_denominator);
result.reduction(); // 计算结果时进行约分
return result;
}
// 除法
Rational divide(Rational other) {
int new_numerator = numerator * other.denominator;
int new_denominator = denominator * other.numerator;
Rational result(new_numerator, new_denominator);
result.reduction(); // 计算结果时进行约分
return result;
}
// 打印有理数
void print() {
cout << numerator << "/" << denominator;
}
// 比较两个有理数的大小
int compare(Rational other) {
int num1 = numerator * other.denominator;
int num2 = denominator * other.numerator;
if (num1 < num2) {
return 1; // 第一个有理数比第二个小
} else if (num1 > num2) {
return 2; // 第一个有理数比第二个大
} else {
return 0; // 两个有理数相等
}
}
};
int main() {
int n1, d1, n2, d2;
cout << "Enter the first rational number (numerator/denominator): ";
cin >> n1 >> d1;
cout << "Enter the second rational number (numerator/denominator): ";
cin >> n2 >> d2;
Rational r1(n1, d1);
Rational r2(n2, d2);
cout << "Subtraction result: ";
Rational sub_result = r1.subtract(r2);
sub_result.print();
cout << endl;
cout << "Division result: ";
Rational div_result = r1.divide(r2);
div_result.print();
cout << endl;
int compare_result = r1.compare(r2);
if (compare_result == 1) {
cout << "The first rational number is smaller than the second one." << endl;
} else if (compare_result == 2) {
cout << "The first rational number is greater than the second one." << endl;
} else {
cout << "The two rational numbers are equal." << endl;
}
return 0;
}
```
在这个程序中,我们定义了一个有理数类 `Rational`,它包含一个私有的分子和分母,并且提供了减法、除法、打印和比较等公共成员函数。在类的构造函数中,我们通过调用约分函数 `reduction()` 来实现初始化时的约分。在减法和除法函数中,我们计算结果后也进行了约分。在比较函数中,我们将两个有理数都转化为分子相乘的形式进行比较。
在 `main()` 函数中,我们首先提示用户输入两个有理数,并且通过调用类的构造函数来创建了两个有理数对象 `r1` 和 `r2`。然后分别执行减法和除法操作,并且输出结果。最后比较这两个有理数的大小,并输出比较结果。
阅读全文