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 2/4 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++5.11寫出,且使用using namespace std;
时间: 2024-02-28 17:54:05 浏览: 113
Global finite-time stabilization of a class of switched nonlinear systems with the powers of positive odd rational numbers
以下是符合要求的C++ 代码:
```
#include <iostream>
using namespace std;
class Rational {
private:
int numerator, denominator;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
public:
Rational(int num = 0, int den = 1) {
int divisor = gcd(num, den);
numerator = num / divisor;
denominator = den / divisor;
}
Rational operator- (Rational other) {
int num = numerator * other.denominator - other.numerator * denominator;
int den = denominator * other.denominator;
return Rational(num, den);
}
Rational operator/ (Rational other) {
int num = numerator * other.denominator;
int den = denominator * other.numerator;
return Rational(num, den);
}
void print() {
cout << numerator << "/" << denominator;
}
int compare(Rational other) {
int num1 = numerator * other.denominator;
int num2 = other.numerator * denominator;
if (num1 < num2) return 1;
if (num1 > num2) return 2;
return 0;
}
};
int main() {
Rational r1, r2;
cout << "Enter the first rational number (numerator denominator): ";
cin >> r1;
cout << "Enter the second rational number (numerator denominator): ";
cin >> r2;
Rational diff = r1 - r2;
cout << "The difference is ";
diff.print();
cout << endl;
Rational quotient = r1 / r2;
cout << "The quotient is ";
quotient.print();
cout << endl;
int result = r1.compare(r2);
if (result == 1) {
cout << "The first number is smaller." << endl;
} else if (result == 2) {
cout << "The second number is smaller." << endl;
} else {
cout << "The two numbers are equal." << endl;
}
return 0;
}
```
请注意,此代码使用了重载运算符-和/来实现两个Rational数的减法和除法。此外,它还定义了一个比较函数来比较两个Rational数的大小。在主函数中,我们首先提示用户输入两个Rational数,然后执行所需的操作并输出结果。
阅读全文