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