C++实现有理数运算类(具有四则运算、输出等功能)

先上代码,还需完善:

#include 
#include 
#include 
#include 
#include 

using namespace std;

/** \brief 取最小公倍数
 *
 * \param a int
 * \param b int
 * \return int,2个参数的最小公倍数
 */
int GetLcm(int a, int b)
{
    if (a == 0 || b == 0)
    {
        cout << "错误:参数为0" << endl;
        return -1;
    }

    int Big, Small;
    int LcmRet;
    if (a > b)
    {
        Big = a;
        Small =b;
    }
    else
    {
        Big = b;
        Small = a;
    }
    int N = 2;
    //大数的倍数
    int BigMutipl = Big;
    while(BigMutipl%Small != 0)
    {
        BigMutipl = Big * N;
        N++;
    }
    LcmRet = BigMutipl;
    return LcmRet;
}

/** \brief 计算最大公约数
 *
 * \param a int
 * \param b int
 * \return int
 */
int GetGcd(int a, int b)
{
    if (b == 0)
        return a;
    else
        GetGcd(b, a % b);
}

class Rational
{
public:
    Rational();
    Rational(int a, int b);

    string GetRationalValue();
    friend Rational operator +(const Rational, const Rational);
    friend Rational operator -(const Rational, const Rational);
    friend Rational operator *(const Rational, const Rational);
    friend Rational operator /(const Rational, const Rational);
    friend ostream& operator << (ostream & os, Rational x);

    ~Rational();

private:
    int Numerator;      //分子
    int Denominator;  //分母

    string  RationValue;
};

Rational::Rational()
{
    Numerator = 1;
    Denominator = 1;
}

Rational::Rational(int a, int b)
{
    if (b == 0)
        cout << "Error: 分母不能为0" << endl;
    Numerator = a;
    Denominator = b;
    GetRationalValue( );
}

string Rational::GetRationalValue()
{
    char cha[16];
    char chb[16];
    //约分
    int gcd = GetGcd(Numerator, Denominator);
    Numerator/=gcd;
    Denominator/=gcd;

    itoa(Numerator, cha, 10);
    itoa(Denominator, chb, 10);
    RationValue = string(cha ) + "/" +string(  chb);
    return RationValue;
}

Rational::~Rational()
{
}

Rational operator+(Rational x , Rational y)
{
    Rational ResultRa, tempRa;
    //int Ratio;
    //1/2 +3/2
    tempRa.Denominator = x.Denominator;
    tempRa.Numerator = x.Numerator;
    //Latest common multiple
    int Lcm = GetLcm(x.Denominator, y.Denominator);

    tempRa.Numerator = tempRa.Numerator * (Lcm/tempRa.Denominator);
    tempRa.Denominator =  Lcm;

    y.Numerator = y.Numerator *  (Lcm/y.Denominator);
    y.Denominator =  Lcm;

    ResultRa.Numerator = tempRa.Numerator + y.Numerator;
    ResultRa.Denominator = Lcm;

    return ResultRa;
}

Rational operator-(Rational x , Rational y)
{
    Rational ResultRa, tempRa;
    tempRa.Denominator = x.Denominator;
    tempRa.Numerator = x.Numerator;

    tempRa.Denominator =  x.Denominator * y.Denominator;
    tempRa.Numerator = tempRa.Numerator * y.Denominator;

    y.Numerator = y.Numerator * x.Denominator;
    y.Denominator =  y.Denominator * x.Denominator;

    ResultRa.Numerator = tempRa.Numerator  - y.Numerator;
    ResultRa.Denominator = tempRa.Denominator;

    return ResultRa;
}

Rational operator*(Rational x , Rational y)
{
    Rational ResultRa;
    ResultRa.Numerator = x.Numerator * y.Numerator;
    ResultRa.Denominator = x.Denominator * y.Denominator;
    return ResultRa;
}

Rational operator/(Rational x , Rational y)
{
    Rational ResultRa;
    ResultRa.Numerator = x.Numerator * y.Denominator;
    ResultRa.Denominator = x.Denominator * y.Numerator;
    return ResultRa;
}

ostream& operator <<(ostream& os, Rational x)
{
    x.GetRationalValue();
    os<< x.RationValue;
    return  os;
}

int main()
{
    Rational Ra= Rational(4, 2);
    Rational Rb = Rational(2, 3);
    Rational Rc =  Ra + Rb;
    Rational Rd = Ra - Rb;
    Rational Re = Ra * Rb;
    Rational Rf = Ra / Rb;
    cout << Rc.GetRationalValue() << endl;
    cout << "---------------1--------------" << endl;
    cout << Rd.GetRationalValue() << endl;
    cout << "---------------2--------------" << endl;
    cout << Re.GetRationalValue() << endl;
    cout << "---------------3--------------" << endl;
    //cout << Rf.GetRationalValue() <
					

发表评论

电子邮件地址不会被公开。 必填项已用*标注