早教吧作业答案频道 -->其他-->
运算符重载构造一个分数类rationalNumber,该类中包括分子和分母两个成员数据,并具有下述功能:建立构造函数,它能防止分母为零,当分数不是最简形式时进行约分,并避免分母为负数重
题目详情
运算符重载
构造一个分数类rationalNumber,该类中包括分子和分母两个成员数据,并具有下述功能:
建立构造函数,它能防止分母为零,当分数不是最简形式时进行约分,并避免分母为负数
重载加法、减法、乘法以及除法运算符。
重载关系运算符:>、
构造一个分数类rationalNumber,该类中包括分子和分母两个成员数据,并具有下述功能:
建立构造函数,它能防止分母为零,当分数不是最简形式时进行约分,并避免分母为负数
重载加法、减法、乘法以及除法运算符。
重载关系运算符:>、
▼优质解答
答案和解析
#include
using namespace std;
class rationalNumber{
private:
int a,b;
public:
rationalNumber(){}
rationalNumber(int m,int n){
if(m>0){
a=m;
b=n;
}
}
void display(){
cout< }
friend rationalNumber operator +(rationalNumber r1,rationalNumber r2);
friend rationalNumber operator -(rationalNumber r1,rationalNumber r2);
friend rationalNumber operator *(rationalNumber r1,rationalNumber r2);
friend rationalNumber operator /(rationalNumber r1,rationalNumber r2);
friend rationalNumber operator >(rationalNumber r1,rationalNumber r2);
friend rationalNumber operator friend rationalNumber operator ==(rationalNumber r1,rationalNumber r2);
};
rationalNumber operator +(rationalNumber r1,rationalNumber r2){
rationalNumber r;
r.a=r1.a*r2.a;
r.b=(r1.b*r2.a)+(r2.b*r1.a);
cout< }
rationalNumber operator -(rationalNumber r1,rationalNumber r2){
rationalNumber r;
r.a=r1.a*r2.a;
r.b=(r1.b*r2.a)-(r2.b*r1.a);
cout< }
rationalNumber operator *(rationalNumber r1,rationalNumber r2){
rationalNumber r;
r.a=r1.a*r2.a;
r.b=r1.b*r2.b;
cout< }
rationalNumber operator /(rationalNumber r1,rationalNumber r2){
rationalNumber r;
r.a=r1.a*r2.b;
r.b=r1.b*r2.a;
cout< }
rationalNumber operator >(rationalNumber r1,rationalNumber r2){
if((r1.b*r2.a)>(r1.a*r2.b)){
cout< }
else{
cout< }
}
rationalNumber operator if((r1.b*r2.a) cout< }
else{
cout< }
}
rationalNumber operator ==(rationalNumber r1,rationalNumber r2){
if((r1.b*r2.a)==(r1.a*r2.b)){
cout< }
}
void main()
{
int a,b;
cout< cin>>a;
cout< cin>>b;
rationalNumber r1(a,b);
r1.display();
int aa,bb;
cout< cin>>aa;
cout< cin>>bb;
rationalNumber r2(aa,bb);
r2.display();
operator +(r1,r2);
operator -(r1,r2);
operator *(r1,r2);
operator /(r1,r2);
operator >(r1,r2);
operator operator ==(r1,r2);
}
using namespace std;
class rationalNumber{
private:
int a,b;
public:
rationalNumber(){}
rationalNumber(int m,int n){
if(m>0){
a=m;
b=n;
}
}
void display(){
cout< }
friend rationalNumber operator +(rationalNumber r1,rationalNumber r2);
friend rationalNumber operator -(rationalNumber r1,rationalNumber r2);
friend rationalNumber operator *(rationalNumber r1,rationalNumber r2);
friend rationalNumber operator /(rationalNumber r1,rationalNumber r2);
friend rationalNumber operator >(rationalNumber r1,rationalNumber r2);
friend rationalNumber operator friend rationalNumber operator ==(rationalNumber r1,rationalNumber r2);
};
rationalNumber operator +(rationalNumber r1,rationalNumber r2){
rationalNumber r;
r.a=r1.a*r2.a;
r.b=(r1.b*r2.a)+(r2.b*r1.a);
cout<
rationalNumber operator -(rationalNumber r1,rationalNumber r2){
rationalNumber r;
r.a=r1.a*r2.a;
r.b=(r1.b*r2.a)-(r2.b*r1.a);
cout<
rationalNumber operator *(rationalNumber r1,rationalNumber r2){
rationalNumber r;
r.a=r1.a*r2.a;
r.b=r1.b*r2.b;
cout<
rationalNumber operator /(rationalNumber r1,rationalNumber r2){
rationalNumber r;
r.a=r1.a*r2.b;
r.b=r1.b*r2.a;
cout<
rationalNumber operator >(rationalNumber r1,rationalNumber r2){
if((r1.b*r2.a)>(r1.a*r2.b)){
cout< }
else{
cout< }
}
rationalNumber operator if((r1.b*r2.a) cout< }
else{
cout< }
}
rationalNumber operator ==(rationalNumber r1,rationalNumber r2){
if((r1.b*r2.a)==(r1.a*r2.b)){
cout< }
}
void main()
{
int a,b;
cout< cin>>a;
cout< cin>>b;
rationalNumber r1(a,b);
r1.display();
int aa,bb;
cout< cin>>aa;
cout< cin>>bb;
rationalNumber r2(aa,bb);
r2.display();
operator +(r1,r2);
operator -(r1,r2);
operator *(r1,r2);
operator /(r1,r2);
operator >(r1,r2);
operator operator ==(r1,r2);
}
看了 运算符重载构造一个分数类ra...的网友还看了以下:
零声母有哪四类?分别举个例子 2020-05-13 …
有两箱同类零件,第一箱有50个,其中10个一等品.有两箱同类零件,第一箱有50个,其中10个一等品 2020-05-17 …
三角函数一题,急救110,长虹贯日,冰天雪地,百思不得其解,谢谢已知角o的终边经过点P(-4a,3 2020-05-20 …
圆锥底面半径为R,母线长3R,M是底面圆上一点,从点M拉一条绳子绕圆锥一圈,再回到点M,求M这根绳 2020-06-06 …
以下关于设备及零部件的保管要求论述,错误的是( )。A.箱体类零件摆放时要水平B.板类,盖类零件最好 2020-06-07 …
邵静敏版现代汉语通论中这样说,i的音变﹝j﹞,即出现在ia,ie,iao,iou,ian,iang 2020-06-26 …
设圆锥的母线长为a,底面半径为r,那么这个扇形的半径(R)为()扇形的弧长(L)为(),因此圆锥的 2020-07-31 …
若矩阵A有一个非零r阶子式,则r(A)≥r..(判断对错) 2020-12-07 …
计算题,试计算这类商品的零售物价类指数1.某地某类零售商品中甲乙丙丁四种代表商品的个体价格指数分别为 2020-12-26 …
判断题:(1)偏心轴类零件和台阶轴类的装夹方法完全相同,对么?(2)数控机床的插补可分为直线插补和. 2021-01-02 …