所以在这个项目中,我必须创建一个合理化程序,帮助将数字简化为简化格式。我最挣扎的程序部分是具有长参数的运算符。我正在尝试写出适当的条件,以便 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 对象作为参数,另一个带有 long。