C++定义一个有理数类
定义一个有理数类,利用该类中的数据成员nume来代表分子,数据成员denom来代表分母,这样每一个该类的对象就相当于一个有理数的分式形态。完成该类的初始化程序以及输出程序...
定义一个有理数类,利用该类中的数据成员nume来代表分子,数据成员denom来代表分母,这样每一个该类的对象就相当于一个有理数的分式形态。完成该类的初始化程序以及输出程序,同时重载<,<=,= =,!=这四种比较运算符,用于比较每个对象的大小
(麻烦给出正确源代码) 展开
(麻烦给出正确源代码) 展开
2个回答
展开全部
#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);
}
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);
}
展开全部
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;
}
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询