Rational Assignment运算符

时间:2017-09-19 21:20:39

标签: c++ rational-number

所以在这个项目中,我必须创建一个合理化程序,帮助将数字简化为简化格式。我最挣扎的程序部分是具有长参数的运算符。我正在尝试写出正确的条件,以便将LongRational正确存储到this-> _num中,  this-> _den但我写的逻辑似乎不对。我的函数参数是否有问题或者无论如何都要纠正这个问题。

Rational& Rational::operator += (const Rational& rational)
{
    this->_num += ((rational._num * _den) + (rational._den * _num));
    this->_den += (rational._den * _den);

    return *this;
}

//Part I am having trouble on. Tried editing but unsure if correct.
Rational& Rational::operator += (long LongRational) 
{

    this->_num += (LongRational * _den) + (_num * LongRational);
    this->_den += (LongRational * _num);

    return *this;
}

Rational& Rational::operator -= (const Rational& rational)
{
    this->_num -= ((rational._den * _num) - (_den * rational._num));
    this->_den -= (rational._den * _den);

    return *this;
}

//Part I am having trouble on. Haven't edited this bit yet,
Rational& Rational::operator -= (long LongRational)
{
    this->_num -= LongRational;
    this->_den -= _den;

    return *this;
}

//注意:他希望有一组两个赋值运算符,其参数将Rational对象作为参数,另一个作为参数。

0 个答案:

没有答案