From e245ab83a16eac463a69abf4c3966759146df999 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Tue, 21 Sep 2021 09:22:46 +0200 Subject: [PATCH] Subtraction, multiplication, light formatting --- FractionTask.pro.user | 7 +++-- fraction.cpp | 37 +++++++++++++++++----- fraction.hpp | 6 ++++ tst_fractiontest.cpp | 73 +++++++++++++++++++++++++++++-------------- 4 files changed, 88 insertions(+), 35 deletions(-) diff --git a/FractionTask.pro.user b/FractionTask.pro.user index aef6d40..c524163 100644 --- a/FractionTask.pro.user +++ b/FractionTask.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -74,14 +74,15 @@ Checked Checked Checked + Checked 0 true -fno-delayed-template-parsing - true - Builtin.BuildSystem + false + {7e16192f-0076-4a4a-bb5d-2bec1fc8c0c7} true true diff --git a/fraction.cpp b/fraction.cpp index 4e6cc11..5279bba 100644 --- a/fraction.cpp +++ b/fraction.cpp @@ -80,9 +80,9 @@ namespace FractionNS { bool Fraction::operator==(const double n) const { return (double(m_numerator) / double(m_denominator)) == n; } - bool Fraction::operator==(const Fraction& n) const { - return (this->m_numerator == n.getNumerator()) - && (this->m_denominator == n.getDenominator()); + bool Fraction::operator==(const Fraction& other) const { + return (this->m_numerator == other.getNumerator()) + && (this->m_denominator == other.getDenominator()); } bool Fraction::operator!=(const int n) const { @@ -112,8 +112,8 @@ namespace FractionNS { return *new Fraction(ownNumeratorPart + otherNumeratorPart, lcm); } - Fraction& Fraction::operator+=(const int other) { - this->m_numerator = m_numerator + (other * m_denominator); + Fraction& Fraction::operator+=(const int n) { + this->m_numerator = m_numerator + (n * m_denominator); reduce(); return *this; } @@ -148,8 +148,8 @@ namespace FractionNS { } Fraction& Fraction::operator-=(const int n) { - auto newNumerator = m_numerator - (n * m_denominator); - return *new Fraction(newNumerator, m_denominator); + m_numerator -= n * m_denominator; + return *this; }; Fraction& Fraction::operator-=(const Fraction& other) { // Sanity check for Clang @@ -160,6 +160,27 @@ namespace FractionNS { auto ownNumeratorPart = m_numerator * (lcm / m_denominator); auto otherNumeratorPart = other.getNumerator() * (lcm / other.getDenominator()); - return *new Fraction(ownNumeratorPart - otherNumeratorPart, lcm); + m_numerator = ownNumeratorPart - otherNumeratorPart; + m_denominator = lcm; + reduce(); + return *this; + } + + Fraction& Fraction::operator*(const int n) const { + return *new Fraction(m_numerator * n, m_denominator); + } + Fraction& Fraction::operator*(const Fraction& other) const { + return *new Fraction(m_numerator * other.getNumerator(), m_denominator * other.getDenominator()); + } + + Fraction& Fraction::operator*=(const int n) { + m_numerator *= n; + return *this; + } + Fraction& Fraction::operator*=(const Fraction& other) { + m_numerator *= other.getNumerator(); + m_denominator *= other.getDenominator(); + reduce(); + return *this; } } diff --git a/fraction.hpp b/fraction.hpp index 520758a..2dee8c4 100644 --- a/fraction.hpp +++ b/fraction.hpp @@ -43,5 +43,11 @@ namespace FractionNS { Fraction& operator-=(const int); Fraction& operator-=(const Fraction&); + + Fraction& operator*(const int) const; + Fraction& operator*(const Fraction&) const; + + Fraction& operator*=(const int); + Fraction& operator*=(const Fraction&); }; } diff --git a/tst_fractiontest.cpp b/tst_fractiontest.cpp index b7f9b5f..ab977a8 100644 --- a/tst_fractiontest.cpp +++ b/tst_fractiontest.cpp @@ -1,12 +1,12 @@ #include + #include #include "fraction.hpp" using namespace FractionNS; -class FractionTest : public QObject -{ +class FractionTest : public QObject { Q_OBJECT public: @@ -20,17 +20,11 @@ private slots: void test_addition(); void test_inequivalence(); void test_substraction(); + void test_multiplication(); }; -FractionTest::FractionTest() -{ - -} - -FractionTest::~FractionTest() -{ - -} +FractionTest::FractionTest() {}; +FractionTest::~FractionTest() {}; void FractionTest::test_constructor() { @@ -40,7 +34,7 @@ void FractionTest::test_constructor() QCOMPARE(f1.getDenominator(), 2); // Reducible fraction - Fraction f2 = Fraction(2,4); + Fraction f2 = Fraction(2, 4); QCOMPARE(f2.getNumerator(), 1); QCOMPARE(f2.getDenominator(), 2); @@ -106,20 +100,21 @@ void FractionTest::test_equivalence() QCOMPARE(f4, f7); QCOMPARE(f5, f6); - QCOMPARE(f4, 2.0f/3.0f); - QCOMPARE(f4, 2.0/3.0); + QCOMPARE(f4, 2.0f / 3.0f); + QCOMPARE(f4, 2.0 / 3.0); - QCOMPARE(f7, 2.0f/3.0f); - QCOMPARE(f7, 2.0/3.0); + QCOMPARE(f7, 2.0f / 3.0f); + QCOMPARE(f7, 2.0 / 3.0); - QCOMPARE(f5, -2.0f/3.0f); - QCOMPARE(f5, -2.0/3.0); + QCOMPARE(f5, -2.0f / 3.0f); + QCOMPARE(f5, -2.0 / 3.0); - QCOMPARE(f6, -2.0f/3.0f); - QCOMPARE(f6, -2.0/3.0); + QCOMPARE(f6, -2.0f / 3.0f); + QCOMPARE(f6, -2.0 / 3.0); } -void FractionTest::test_inequivalence() { +void FractionTest::test_inequivalence() +{ Fraction f1 = Fraction(1, 1); Fraction f2 = Fraction(-1, 1); Fraction f3 = Fraction(2, 3); @@ -146,15 +141,16 @@ void FractionTest::test_addition() QCOMPARE(f2, 0.5f); Fraction f3 = Fraction(-2, 9); - QCOMPARE(f3, -(2.0/9.0)); - QCOMPARE(f3, -(2.0f/9.0f)); + QCOMPARE(f3, -(2.0 / 9.0)); + QCOMPARE(f3, -(2.0f / 9.0f)); f3 += Fraction(2, 9); QCOMPARE(f3, 0); QCOMPARE(f3, 0.0); QCOMPARE(f3, 0.0f); } -void FractionTest::test_substraction() { +void FractionTest::test_substraction() +{ Fraction f1 = Fraction(11) - 10; QCOMPARE(f1, 1); @@ -169,6 +165,35 @@ void FractionTest::test_substraction() { Fraction f4 = Fraction(9, 3); f4 -= 3; QCOMPARE(f4, 0); + + Fraction f5 = Fraction(8, 3); + // (8/3) - ((2/3) - 1) + //=(8/3) + (1/3) + //= 9/3 + //= 3 + f5 -= Fraction(2, 3) - 1; + QCOMPARE(f5, 3); + + Fraction f6 = Fraction(-2, 199); + // (-2/199) + 8 + //=(-2/199) + (1592/199 + //=(1590/199) + f6 -= Fraction(-4) - 4; + QCOMPARE(f6, Fraction(1590, 199)); +} + +void FractionTest::test_multiplication() +{ + Fraction f1 = Fraction(1, 1); + const Fraction f2 = Fraction(2, 1); + QCOMPARE(f1 * 2, f2); + f1 *= 2; + QCOMPARE(f1, f2); + + f1 *= 3; + QCOMPARE(f1, 6); + + QCOMPARE(f1 * Fraction(2, 3), 4); } QTEST_APPLESS_MAIN(FractionTest)