Refactoring

- Remove `this->`
- Avoid possible divide by 0
- Invert ternary operator in lcm to be more readable
This commit is contained in:
Tobias Berger 2021-09-24 11:50:07 +02:00
parent d45bbab05b
commit 2a10c5480a

View file

@ -13,11 +13,10 @@ namespace FractionNS {
p %= q;
}
}
int lcm(int p, int q) {
int gcd = FractionNS::gcd(p, q);
return gcd != 0 ? (p / gcd * q) : 0;
return gcd == 0 ? 0 : (p / gcd * q);
}
void Fraction::reduce()
@ -42,30 +41,37 @@ namespace FractionNS {
Fraction::Fraction(int numerator, int denominator)
{
if(denominator == 0) throw divide_by_zero_error();
this->m_numerator = numerator;
this->m_denominator = denominator;
m_numerator = numerator;
m_denominator = denominator;
reduce();
}
Fraction::Fraction(const int n) {
this->m_numerator = n;
this->m_denominator = 1;
m_numerator = n;
m_denominator = 1;
}
Fraction::Fraction(const Fraction& n) {
this->m_numerator = n.getNumerator();
this->m_denominator = n.getDenominator();
m_numerator = n.getNumerator();
m_denominator = n.getDenominator();
}
int Fraction::getNumerator() const { return m_numerator; }
int Fraction::getDenominator() const { return m_denominator; }
void Fraction::setNumerator(int numerator) {
auto gcd = FractionNS::gcd(numerator, this->m_denominator);
this->m_numerator = numerator / gcd;
auto gcd = FractionNS::gcd(numerator, m_denominator);
m_numerator = numerator / gcd;
}
void Fraction::setDenominator(int denominator) {
if(denominator == 0) throw divide_by_zero_error();
auto gcd = FractionNS::gcd(this->m_numerator, denominator);
this->m_denominator = denominator / gcd;
auto gcd = FractionNS::gcd(m_numerator, denominator);
if(gcd == 0)
{
m_denominator = 1;
}
else
{
m_denominator = denominator / gcd;
}
}
QString Fraction::display() const {
@ -89,8 +95,8 @@ namespace FractionNS {
return (double(m_numerator) / double(m_denominator)) == n;
}
bool Fraction::operator==(const Fraction& other) const {
return (this->m_numerator == other.getNumerator())
&& (this->m_denominator == other.getDenominator());
return (m_numerator == other.getNumerator())
&& (m_denominator == other.getDenominator());
}
bool Fraction::operator!=(const int n) const {
@ -103,8 +109,8 @@ namespace FractionNS {
return (double(m_numerator) / double(m_denominator)) != n;
}
bool Fraction::operator!=(const Fraction& other) const {
return (this->m_numerator != other.getNumerator())
|| (this->m_denominator != other.getDenominator());
return (m_numerator != other.getNumerator())
|| (m_denominator != other.getDenominator());
}
bool Fraction::operator<(const int n) const {
@ -181,7 +187,7 @@ namespace FractionNS {
}
Fraction& Fraction::operator+=(const int n) {
this->m_numerator = m_numerator + (n * m_denominator);
m_numerator += n * m_denominator;
reduce();
return *this;
}
@ -195,8 +201,8 @@ namespace FractionNS {
auto ownNumeratorPart = m_numerator * (lcm / m_denominator);
auto otherNumeratorPart = other.getNumerator() * (lcm / other.getDenominator());
this->m_numerator = ownNumeratorPart + otherNumeratorPart;
this->m_denominator = lcm;
m_numerator = ownNumeratorPart + otherNumeratorPart;
m_denominator = lcm;
reduce();
return *this;