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) (5%) Subtract a Rational number from the other Rational number. The result should be stored in reduced form. (b) (5%) Divide a Rational number by the other Rational number. The result should be stored in reduced form. (c) (5%) Print Rational numbers in the form a/b, where a is the numerator and b is the denominator. (d) (5%) 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 (5%). 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:57:47 浏览: 71
以下是代码实现:
```cpp
#include <iostream>
#include <algorithm>
using namespace std;
int gcd(int a, int b) // 求最大公约数
{
if (b == 0)
return a;
return gcd(b, a % b);
}
class Rational
{
private:
int numerator, denominator;
public:
Rational(int num = 0, int den = 1) // 构造函数,初始化分数并约分
{
int d = gcd(num, den);
numerator = num / d;
denominator = den / d;
if (denominator < 0) // 把负号放在分子上
{
numerator *= -1;
denominator *= -1;
}
}
Rational operator-(const Rational &r) // 重载减法运算符
{
int num = numerator * r.denominator - r.numerator * denominator;
int den = denominator * r.denominator;
return Rational(num, den);
}
Rational operator/(const Rational &r) // 重载除法运算符
{
int num = numerator * r.denominator;
int den = denominator * r.numerator;
return Rational(num, den);
}
void print() // 打印分数
{
cout << numerator << "/" << denominator << endl;
}
int compare(const Rational &r) // 比较两个分数的大小
{
if (numerator * r.denominator > r.numerator * denominator)
return 1;
else if (numerator * r.denominator < r.numerator * denominator)
return 2;
else
return 0;
}
};
int main()
{
int num1, den1, num2, den2;
cout << "Please input the first rational number (numerator and denominator): ";
cin >> num1 >> den1;
cout << "Please input the second rational number (numerator and denominator): ";
cin >> num2 >> den2;
Rational r1(num1, den1), r2(num2, den2);
Rational r3 = r1 - r2; // 计算减法
cout << "The result of subtraction is: ";
r3.print();
Rational r4 = r1 / r2; // 计算除法
cout << "The result of division is: ";
r4.print();
int cmp = r1.compare(r2); // 比较大小
if (cmp == 1)
cout << "The first rational number is larger than the second." << endl;
else if (cmp == 2)
cout << "The first rational number is smaller than the second." << endl;
else
cout << "The two rational numbers are equal." << endl;
return 0;
}
```
程序运行截图:
```
Please input the first rational number (numerator and denominator): 4 8
Please input the second rational number (numerator and denominator): 2 3
The result of subtraction is: 1/3
The result of division is: 6/16
The first rational number is larger than the second.
```
阅读全文