Subtraction, multiplication, light formatting
This commit is contained in:
parent
e55d2843f6
commit
e245ab83a1
4 changed files with 88 additions and 35 deletions
|
@ -1,6 +1,6 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE QtCreatorProject>
|
<!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>
|
<qtcreator>
|
||||||
<data>
|
<data>
|
||||||
<variable>EnvironmentId</variable>
|
<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_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_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_inequivalence">Checked</value>
|
||||||
|
<value type="Qt::CheckState" key="1@C:/Users/Berger.ASGARD/Documents/Git/FractionTask/tst_fractiontest.cpp:test_substraction">Checked</value>
|
||||||
</valuemap>
|
</valuemap>
|
||||||
<value type="int" key="AutoTest.RunAfterBuild">0</value>
|
<value type="int" key="AutoTest.RunAfterBuild">0</value>
|
||||||
<value type="bool" key="AutoTest.UseGlobal">true</value>
|
<value type="bool" key="AutoTest.UseGlobal">true</value>
|
||||||
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey">
|
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey">
|
||||||
<value type="QString">-fno-delayed-template-parsing</value>
|
<value type="QString">-fno-delayed-template-parsing</value>
|
||||||
</valuelist>
|
</valuelist>
|
||||||
<value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value>
|
<value type="bool" key="ClangCodeModel.UseGlobalConfig">false</value>
|
||||||
<value type="QString" key="ClangCodeModel.WarningConfigId">Builtin.BuildSystem</value>
|
<value type="QString" key="ClangCodeModel.WarningConfigId">{7e16192f-0076-4a4a-bb5d-2bec1fc8c0c7}</value>
|
||||||
<valuemap type="QVariantMap" key="ClangTools">
|
<valuemap type="QVariantMap" key="ClangTools">
|
||||||
<value type="bool" key="ClangTools.AnalyzeOpenFiles">true</value>
|
<value type="bool" key="ClangTools.AnalyzeOpenFiles">true</value>
|
||||||
<value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value>
|
<value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value>
|
||||||
|
|
37
fraction.cpp
37
fraction.cpp
|
@ -80,9 +80,9 @@ namespace FractionNS {
|
||||||
bool Fraction::operator==(const double n) const {
|
bool Fraction::operator==(const double n) const {
|
||||||
return (double(m_numerator) / double(m_denominator)) == n;
|
return (double(m_numerator) / double(m_denominator)) == n;
|
||||||
}
|
}
|
||||||
bool Fraction::operator==(const Fraction& n) const {
|
bool Fraction::operator==(const Fraction& other) const {
|
||||||
return (this->m_numerator == n.getNumerator())
|
return (this->m_numerator == other.getNumerator())
|
||||||
&& (this->m_denominator == n.getDenominator());
|
&& (this->m_denominator == other.getDenominator());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Fraction::operator!=(const int n) const {
|
bool Fraction::operator!=(const int n) const {
|
||||||
|
@ -112,8 +112,8 @@ namespace FractionNS {
|
||||||
return *new Fraction(ownNumeratorPart + otherNumeratorPart, lcm);
|
return *new Fraction(ownNumeratorPart + otherNumeratorPart, lcm);
|
||||||
}
|
}
|
||||||
|
|
||||||
Fraction& Fraction::operator+=(const int other) {
|
Fraction& Fraction::operator+=(const int n) {
|
||||||
this->m_numerator = m_numerator + (other * m_denominator);
|
this->m_numerator = m_numerator + (n * m_denominator);
|
||||||
reduce();
|
reduce();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -148,8 +148,8 @@ namespace FractionNS {
|
||||||
}
|
}
|
||||||
|
|
||||||
Fraction& Fraction::operator-=(const int n) {
|
Fraction& Fraction::operator-=(const int n) {
|
||||||
auto newNumerator = m_numerator - (n * m_denominator);
|
m_numerator -= n * m_denominator;
|
||||||
return *new Fraction(newNumerator, m_denominator);
|
return *this;
|
||||||
};
|
};
|
||||||
Fraction& Fraction::operator-=(const Fraction& other) {
|
Fraction& Fraction::operator-=(const Fraction& other) {
|
||||||
// Sanity check for Clang
|
// Sanity check for Clang
|
||||||
|
@ -160,6 +160,27 @@ 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());
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,5 +43,11 @@ namespace FractionNS {
|
||||||
|
|
||||||
Fraction& operator-=(const int);
|
Fraction& operator-=(const int);
|
||||||
Fraction& operator-=(const Fraction&);
|
Fraction& operator-=(const Fraction&);
|
||||||
|
|
||||||
|
Fraction& operator*(const int) const;
|
||||||
|
Fraction& operator*(const Fraction&) const;
|
||||||
|
|
||||||
|
Fraction& operator*=(const int);
|
||||||
|
Fraction& operator*=(const Fraction&);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
#include <QtTest>
|
#include <QtTest>
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
#include "fraction.hpp"
|
#include "fraction.hpp"
|
||||||
|
|
||||||
using namespace FractionNS;
|
using namespace FractionNS;
|
||||||
|
|
||||||
class FractionTest : public QObject
|
class FractionTest : public QObject {
|
||||||
{
|
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -20,17 +20,11 @@ private slots:
|
||||||
void test_addition();
|
void test_addition();
|
||||||
void test_inequivalence();
|
void test_inequivalence();
|
||||||
void test_substraction();
|
void test_substraction();
|
||||||
|
void test_multiplication();
|
||||||
};
|
};
|
||||||
|
|
||||||
FractionTest::FractionTest()
|
FractionTest::FractionTest() {};
|
||||||
{
|
FractionTest::~FractionTest() {};
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
FractionTest::~FractionTest()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void FractionTest::test_constructor()
|
void FractionTest::test_constructor()
|
||||||
{
|
{
|
||||||
|
@ -119,7 +113,8 @@ void FractionTest::test_equivalence()
|
||||||
QCOMPARE(f6, -2.0 / 3.0);
|
QCOMPARE(f6, -2.0 / 3.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FractionTest::test_inequivalence() {
|
void FractionTest::test_inequivalence()
|
||||||
|
{
|
||||||
Fraction f1 = Fraction(1, 1);
|
Fraction f1 = Fraction(1, 1);
|
||||||
Fraction f2 = Fraction(-1, 1);
|
Fraction f2 = Fraction(-1, 1);
|
||||||
Fraction f3 = Fraction(2, 3);
|
Fraction f3 = Fraction(2, 3);
|
||||||
|
@ -154,7 +149,8 @@ void FractionTest::test_addition()
|
||||||
QCOMPARE(f3, 0.0f);
|
QCOMPARE(f3, 0.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FractionTest::test_substraction() {
|
void FractionTest::test_substraction()
|
||||||
|
{
|
||||||
Fraction f1 = Fraction(11) - 10;
|
Fraction f1 = Fraction(11) - 10;
|
||||||
QCOMPARE(f1, 1);
|
QCOMPARE(f1, 1);
|
||||||
|
|
||||||
|
@ -169,6 +165,35 @@ void FractionTest::test_substraction() {
|
||||||
Fraction f4 = Fraction(9, 3);
|
Fraction f4 = Fraction(9, 3);
|
||||||
f4 -= 3;
|
f4 -= 3;
|
||||||
QCOMPARE(f4, 0);
|
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)
|
QTEST_APPLESS_MAIN(FractionTest)
|
||||||
|
|
Reference in a new issue