Subtraction, multiplication, light formatting

This commit is contained in:
Tobias Berger 2021-09-21 09:22:46 +02:00
parent e55d2843f6
commit e245ab83a1
4 changed files with 88 additions and 35 deletions

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 5.0.1, 2021-09-20T15:13:59. -->
<!-- Written by QtCreator 5.0.1, 2021-09-20T16:34:51. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
@ -74,14 +74,15 @@
<value type="Qt::CheckState" key="1@C:/Users/Berger.ASGARD/Documents/Git/FractionTask/tst_fractiontest.cpp:test_display">Checked</value>
<value type="Qt::CheckState" key="1@C:/Users/Berger.ASGARD/Documents/Git/FractionTask/tst_fractiontest.cpp:test_equivalence">Checked</value>
<value type="Qt::CheckState" key="1@C:/Users/Berger.ASGARD/Documents/Git/FractionTask/tst_fractiontest.cpp:test_inequivalence">Checked</value>
<value type="Qt::CheckState" key="1@C:/Users/Berger.ASGARD/Documents/Git/FractionTask/tst_fractiontest.cpp:test_substraction">Checked</value>
</valuemap>
<value type="int" key="AutoTest.RunAfterBuild">0</value>
<value type="bool" key="AutoTest.UseGlobal">true</value>
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey">
<value type="QString">-fno-delayed-template-parsing</value>
</valuelist>
<value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value>
<value type="QString" key="ClangCodeModel.WarningConfigId">Builtin.BuildSystem</value>
<value type="bool" key="ClangCodeModel.UseGlobalConfig">false</value>
<value type="QString" key="ClangCodeModel.WarningConfigId">{7e16192f-0076-4a4a-bb5d-2bec1fc8c0c7}</value>
<valuemap type="QVariantMap" key="ClangTools">
<value type="bool" key="ClangTools.AnalyzeOpenFiles">true</value>
<value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value>

View file

@ -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;
}
}

View file

@ -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&);
};
}

View file

@ -1,12 +1,12 @@
#include <QtTest>
#include <QDebug>
#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()
{
@ -119,7 +113,8 @@ void FractionTest::test_equivalence()
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);
@ -154,7 +149,8 @@ void FractionTest::test_addition()
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)