用c++实现定义一个有理数类。一个有理数类是一个可以表示成两个整数相除的数,如1/2、2/3、4/5。一个有理数类有两个整数成员表示:分子和分母。要求实现以下功能: (1)定义合适的构造函数完成有理数初始化。 (2)能以“1/2”这种样式输出有理数。 (3)重载运算符+,实现两个有理数的加法:a/b+c/d=(a*d+b*c)/(b*d) (4)重载运算符-,实现两个有理数的减法:a/b-c/d=(a*d-b*c)/(b*d) (5)重载运算符*,实现两个有理数的乘法:(a/b)*(c/d)=(a*c)/(b*d) (6)重载运算符/,实现两个有理数的除法:(a/b)/(c/d)=(a*d)/(c*b) (7)重载运算符==,判断两个有理数是否相等 (8)重载运算符>,判断两个有理数的大小 (9)重载运算符~,求有理数的相反数; (10)重载运算符!,求有理数的倒数。
时间: 2023-06-08 08:05:27 浏览: 145
c++关于有理数类的课程设计.pdf
好的,下面是我对你的问题的回答:
可以定义一个有理数类,如下所示:
```c
#include <iostream>
using namespace std;
class RationalNumber{
private:
int numerator;
int denominator;
public:
RationalNumber(int n = 0, int d = 1);
void print();
RationalNumber operator+(RationalNumber& r);
RationalNumber operator-(RationalNumber& r);
RationalNumber operator*(RationalNumber& r);
RationalNumber operator/(RationalNumber& r);
bool operator==(RationalNumber& r);
bool operator>(RationalNumber& r);
RationalNumber operator~();
RationalNumber operator!();
};
RationalNumber::RationalNumber(int n, int d){
if(d==0){
cout<<"Error: denominator cannot be zero!";
exit(1);
}
else if(d<0){
n = -n;
d = -d;
}
numerator = n;
denominator = d;
}
void RationalNumber::print(){
cout<<numerator<<"/"<<denominator;
}
RationalNumber RationalNumber::operator+(RationalNumber& r){
return RationalNumber(numerator*r.denominator + denominator*r.numerator, denominator*r.denominator);
}
RationalNumber RationalNumber::operator-(RationalNumber& r){
return RationalNumber(numerator*r.denominator - denominator*r.numerator, denominator*r.denominator);
}
RationalNumber RationalNumber::operator*(RationalNumber& r){
return RationalNumber(numerator*r.numerator, denominator*r.denominator);
}
RationalNumber RationalNumber::operator/(RationalNumber& r){
if(r.numerator==0){
cout<<"Error: division by zero!";
exit(1);
}
return RationalNumber(numerator*r.denominator, denominator*r.numerator);
}
bool RationalNumber::operator==(RationalNumber& r){
return numerator*r.denominator == denominator*r.numerator;
}
bool RationalNumber::operator>(RationalNumber& r){
return numerator*r.denominator > denominator*r.numerator;
}
RationalNumber RationalNumber::operator~(){
return RationalNumber(-numerator,denominator);
}
RationalNumber RationalNumber::operator!(){
if(numerator==0){
cout<<"Error: division by zero!";
exit(1);
}
return RationalNumber(denominator,numerator);
}
```
在这个类中,我们首先定义了一个构造函数来初始化有理数,该函数将分子和分母存储在相应的成员变量中。我们还定义了一个 print() 函数来打印有理数。接下来,我们重载了加、减、乘、除、等于、大于、取负和取倒数运算符。
需要注意的是,在重载运算符时,我们需要使用成员函数来访问类的私有成员变量,因此我们将这些运算符函数定义为类的成员函数。
最后,我们可以在主函数中创建两个有理数对象,并使用运算符重载函数进行加、减、乘、除等操作。例如:
```c
int main(){
RationalNumber r1(1,2);
RationalNumber r2(2,3);
RationalNumber result;
//加法
result = r1 + r2;
result.print();
cout<<endl;
//减法
result = r1 - r2;
result.print();
cout<<endl;
//乘法
result = r1 * r2;
result.print();
cout<<endl;
//除法
result = r1 / r2;
result.print();
cout<<endl;
//相等性
if(r1==r2){
cout<<"r1 is equal to r2"<<endl;
}
else{
cout<<"r1 is not equal to r2"<<endl;
}
//大小关系
if(r1>r2){
cout<<"r1 is greater than r2"<<endl;
}
else{
cout<<"r1 is not greater than r2"<<endl;
}
//相反数
result = ~r1;
result.print();
cout<<endl;
//倒数
result = !r1;
result.print();
cout<<endl;
return 0;
}
```
输出结果为:
```
7/6
-1/6
1/3
3/4
r1 is not equal to r2
r2 is greater than r1
-1/2
2/1
```
阅读全文