C++定义一个有理数类

定义一个有理数类,利用该类中的数据成员nume来代表分子,数据成员denom来代表分母,这样每一个该类的对象就相当于一个有理数的分式形态。完成该类的初始化程序以及输出程序... 定义一个有理数类,利用该类中的数据成员nume来代表分子,数据成员denom来代表分母,这样每一个该类的对象就相当于一个有理数的分式形态。完成该类的初始化程序以及输出程序,同时重载<,<=,= =,!=这四种比较运算符,用于比较每个对象的大小
(麻烦给出正确源代码)
展开
 我来答
zhlonglt
2009-11-27 · TA获得超过130个赞
知道小有建树答主
回答量:109
采纳率:0%
帮助的人:0
展开全部
#include <iostream>
using namespace std;
class Fraction;
ostream& operator<<(ostream& out,Fraction& frac);
Fraction& operator+(const Fraction& frac1,const Fraction& frac2);
Fraction& operator-(const Fraction& frac1,const Fraction& frac2);
Fraction& operator*(const Fraction& frac1,const Fraction& frac2);
Fraction& operator/(const Fraction& frac1,const Fraction& frac2);
int LCF(const int& D1,const int& D2); //最小公分母
class Fraction
{
private:
int _Numerator; //分子
int _Denominator; //分母
public:
Fraction(){}
Fraction(const int& Numerator,const int& Denominator);
friend Fraction& operator+(const Fraction& frac1,const Fraction& frac2);
friend Fraction& operator-(const Fraction& frac1,const Fraction& frac2);
friend Fraction& operator*(const Fraction& frac1,const Fraction& frac2);
friend Fraction& operator/(const Fraction& frac1,const Fraction& frac2);
bool operator<(const Fraction& frac);
bool operator>(const Fraction& frac);
bool operator==(const Fraction& frac);
bool operator!=(const Fraction& frac);
void simp();
friend ostream& operator<<(ostream& out,Fraction& frac);
};

int main()
{
Fraction f1(12,15);
Fraction f2(3,5);
cout <<"f1: " <<f1;
cout <<endl <<"f2: " <<f2;
cout <<endl <<"f1+f2: ";
cout <<f1+f2;
cout <<endl <<"f1-f2: ";
cout <<f1-f2;
cout <<endl <<"f1*f2: ";
cout <<f1*f2;
cout <<endl <<"f1/f2: ";
cout <<f1/f2;
cout <<endl <<"f1+f2-f1+f2: ";
cout <<f1+f2-f1+f2;
cout <<endl <<"f1<f2";
cout <<endl <<(f1<f2);
cout <<endl <<"f1==f2";
cout <<endl <<(f1==f2);
return 0;
}

Fraction::Fraction(const int& Numerator,const int& Denominator)//构造
:_Numerator(Numerator),_Denominator(Denominator)
{
this->simp();
}
void Fraction::simp() //化简
{
int min=this->_Numerator<this->_Denominator?this->_Numerator:this->_Denominator;
for(int i=min;i>1 && i<=min;i--)
{
if(this->_Numerator%i==0 && this->_Denominator%i==0)
{
this->_Numerator/=i;
this->_Denominator/=i;

}
min=this->_Numerator<this->_Denominator?this->_Numerator:this->_Denominator;
}
}

int LCF(const int& D1,const int& D2) //最小公倍数
{
int max=D1>D2?D1:D2;
for(int i=max;i<D1*D2;i++)
if(i%D1==0 && i%D2==0)
return i;
return D1*D2;
}

Fraction& operator+(const Fraction& frac1,const Fraction& frac2)//重载+
{
static Fraction tempadd;
int lcf=LCF(frac1._Denominator,frac2._Denominator);
int l1=lcf/frac1._Denominator;
int l2=lcf/frac2._Denominator;
tempadd._Numerator=frac1._Numerator*l1+frac2._Numerator*l2;
tempadd._Denominator=lcf; //分母
tempadd.simp();//化简
return tempadd;
}
Fraction& operator-(const Fraction& frac1,const Fraction& frac2)//重载-
{
static Fraction tempminus;
int lcf=LCF(frac1._Denominator,frac2._Denominator);
int l1=lcf/frac1._Denominator;
int l2=lcf/frac2._Denominator;
tempminus._Numerator=frac1._Numerator*l1-frac2._Numerator*l2;
tempminus._Denominator=lcf; //分母
tempminus.simp();//化简
return tempminus;
}
Fraction& operator*(const Fraction& frac1,const Fraction& frac2)//乘
{
static Fraction tempmultiply;
tempmultiply._Numerator=frac1._Numerator*frac2._Numerator;
tempmultiply._Denominator=frac1._Denominator*frac2._Denominator;
tempmultiply.simp();
return tempmultiply;
}
Fraction& operator/(const Fraction& frac1,const Fraction& frac2)//除
{
static Fraction tempdivide;
tempdivide._Numerator=frac1._Numerator*frac2._Denominator;
tempdivide._Denominator=frac1._Denominator*frac2._Numerator;
tempdivide.simp();
return tempdivide;
}
ostream& operator<<(ostream& out,Fraction& frac) //重载<<输出分数
{
out <<frac._Numerator <<"/" <<frac._Denominator;
return out;
}
bool Fraction::operator<(const Fraction& frac)
{
int lcf=LCF(this->_Denominator,frac._Denominator);
int l1=lcf/this->_Denominator;
int l2=lcf/frac._Denominator;
return this->_Numerator<frac._Numerator;
}

bool Fraction::operator>(const Fraction& frac)
{
return !(*this<frac);
}
bool Fraction::operator==(const Fraction& frac)
{
int lcf=LCF(this->_Denominator,frac._Denominator);
int l1=lcf/this->_Denominator;
int l2=lcf/frac._Denominator;
return this->_Numerator==frac._Numerator;
}
bool Fraction::operator!=(const Fraction& frac)
{
return !(*this==frac);
}
TableDI
2024-07-18 广告
仅需3步!不写公式自动完成Excel vlookup表格匹配!Excel在线免,vlookup工具,点击16步自动完成表格匹配,无需手写公式,免费使用!... 点击进入详情页
本回答由TableDI提供
hfhkks
2009-11-27 · TA获得超过257个赞
知道答主
回答量:146
采纳率:0%
帮助的人:0
展开全部
class Ration {
public:
Ration(int a = 0, int b = 1) : nume(a), denom(b){}
~Ration(){};
inline bool operator <(const Ration &r);
bool operator <=(const Ration &r);
bool operator ==(const Ration &r);
bool operator !=(const Ration &r);
friend ostream & operator <<(ostream & os, const Ration & r);
private:
int nume;
int denom;
};

// 其余重载类似
bool Ration::operator <(const Ration &r) {
return (double)this->nume/this->denom < (double)r.nume/r.denom;
}

ostream & operator <<(ostream & os, const Ration & r) {
os << (double)r.nume/r.denom;
return os;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式