From 53d139bc11a281c774d659fc6469eb6b5263f241 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Fri, 24 Sep 2021 12:29:52 +0200 Subject: [PATCH] Add inc, dec, ++, and -- --- fraction.cpp | 30 ++++++++++++++++++++++++++++++ fraction.hpp | 4 ++-- tst_fractiontest.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/fraction.cpp b/fraction.cpp index c37e45d..cfd48b1 100644 --- a/fraction.cpp +++ b/fraction.cpp @@ -109,6 +109,14 @@ namespace FractionNS { return *this / n; } + // Equivalent to ++x and --x, respectively + Fraction& Fraction::inc() { + return ++(*this); + } + Fraction& Fraction::dec() { + return --(*this); + } + bool Fraction::operator==(const int n) const { return m_denominator == 1 && m_numerator == n; @@ -346,4 +354,26 @@ namespace FractionNS { Fraction& Fraction::operator+() const { return *new Fraction(m_numerator, m_denominator); } + + // Prefix -- + Fraction& Fraction::operator--() { + return *new Fraction(*this -= 1); + } + // Postfix -- + Fraction& Fraction::operator--(const int) { + auto result = new Fraction(*this); + *this -= 1; + return *result; + } + + // Prefix ++ + Fraction& Fraction::operator++() { + return *new Fraction(*this += 1); + } + // Postfix ++ + Fraction& Fraction::operator++(const int) { + auto result = new Fraction(*this); + *this += 1; + return *result; + } } diff --git a/fraction.hpp b/fraction.hpp index 5527134..6b1b013 100644 --- a/fraction.hpp +++ b/fraction.hpp @@ -72,6 +72,8 @@ namespace FractionNS { Fraction& mul(const int) const; Fraction& div(const Fraction&) const; Fraction& div(const int) const; + Fraction& inc(); + Fraction& dec(); // Binary math operators Fraction& operator+(const int) const; @@ -109,12 +111,10 @@ namespace FractionNS { Fraction& operator-() const; Fraction& operator+() const; // Should return a copy of the Fraction. - /* Fraction& operator++(); // ++x Fraction& operator++(const int); // x++ Fraction& operator--(); // --x Fraction& operator--(const int); // x-- - */ }; } diff --git a/tst_fractiontest.cpp b/tst_fractiontest.cpp index 2e52e3f..57c05a3 100644 --- a/tst_fractiontest.cpp +++ b/tst_fractiontest.cpp @@ -31,6 +31,8 @@ private slots: void test_inverse(); void test_pow(); void test_math_methods(); + void test_increment(); + void test_decrement(); }; FractionTest::FractionTest() { @@ -500,6 +502,30 @@ void FractionTest::test_math_methods() { QCOMPARE(f1 / f2, f1.div(f2)); } +void FractionTest::test_increment() { + Fraction f1 = Fraction(2); + const Fraction f2 = f1 + 1; + const Fraction f3 = f2 + 1; + const Fraction f4 = f3 + 1; + + QCOMPARE(++f1, f2); + QCOMPARE(f1++, f2); + QCOMPARE(f1, f3); + QCOMPARE(f1.inc(), f4); +} + +void FractionTest::test_decrement() { + Fraction f1 = Fraction(2); + const Fraction f2 = f1 - 1; + const Fraction f3 = f2 - 1; + const Fraction f4 = f3 - 1; + + QCOMPARE(--f1, f2); + QCOMPARE(f1--, f2); + QCOMPARE(f1, f3); + QCOMPARE(f1.dec(), f4); +} + QTEST_APPLESS_MAIN(FractionTest) #include "tst_fractiontest.moc"