Closed alexey-malov closed 5 years ago
void CRational::Normalize()
{
const int gcd = GCD(m_numerator, m_denominator);
m_numerator /= gcd;
m_denominator /= gcd;
}
CRational const operator+(const int n1, const CRational& n2)
{
return CRational(n1) + n2;
}
CRational const operator+(const CRational& n1, const int n2)
{
return CRational(n2) + n1;
}
[x] Если оператор + объявлен для двух рациональных чисел, то компилятор может сам догадаться, как сложить целое с рациональным и рациональное с целым, вызвав предварительно конструктор рационального из целого
[x] То же самое касается операторов сравнения
SCENARIO("check boolean !=")
{
WHEN("check inequality of 1/2 and 1/2")
{
CRational n1(1, 2);
CRational n2(1, 2);
THEN("it returns false")
CRational::CRational(const int numerator, const int denominator)
: m_numerator(numerator)
, m_denominator(denominator)
{
if (m_denominator == 0)
{
throw std::overflow_error("Divide by zero exception");
}
Normalize();
}
adjuct
- нет такого словаstd::pair<int, CRational> CRational::ToCompoundFraction() const
{
const int intPart = (int)(floor(abs(ToDouble())));
const int remainder = intPart * GetDenominator();
WHEN("convert negative retional number with intPart")
{
CRational n(-9, 5);
THEN("get intPart and negative normal rational number")
{
REQUIRE(n.ToCompoundFraction() == make_pair<int, CRational>(1, CRational(-4, 5)));
}
}