greater than, less than

This commit is contained in:
Tobias Berger 2021-09-21 10:03:45 +02:00
parent e2d2c4489d
commit 3a49702d09
3 changed files with 188 additions and 14 deletions

View file

@ -99,6 +99,36 @@ namespace FractionNS {
|| (this->m_denominator != other.getDenominator()); || (this->m_denominator != other.getDenominator());
} }
bool Fraction::operator<(const int n) const {
return m_numerator < n * m_denominator;
}
bool Fraction::operator<(const float n) const {
return m_numerator < n * m_denominator;
}
bool Fraction::operator<(const double n) const {
return m_numerator < n * m_denominator;
}
bool Fraction::operator<(const Fraction& other) const {
auto lcm = FractionNS::lcm(m_denominator, other.getDenominator());
return (m_numerator * (lcm / m_denominator)) < (other.getNumerator() * (lcm / other.getDenominator()));
}
bool Fraction::operator>(const int n) const {
return m_numerator > n * m_denominator;
}
bool Fraction::operator>(const float n) const {
return m_numerator > n * m_denominator;
}
bool Fraction::operator>(const double n) const {
return m_numerator > n * m_denominator;
}
bool Fraction::operator>(const Fraction& other) const {
auto lcm = FractionNS::lcm(m_denominator, other.getDenominator());
return (m_numerator * (lcm / m_denominator)) > (other.getNumerator() * (lcm / other.getDenominator()));
}
Fraction& Fraction::operator+(const int n) const { Fraction& Fraction::operator+(const int n) const {
auto newNumerator = m_numerator + (n * m_denominator); auto newNumerator = m_numerator + (n * m_denominator);
return *new Fraction(newNumerator, m_denominator); return *new Fraction(newNumerator, m_denominator);
@ -202,4 +232,11 @@ namespace FractionNS {
reduce(); reduce();
return *this; return *this;
} }
Fraction& Fraction::operator-() const {
return *new Fraction(-m_numerator, m_denominator);
}
Fraction& Fraction::operator+() const {
return *new Fraction(m_numerator, m_denominator);
}
} }

View file

@ -22,6 +22,7 @@ namespace FractionNS {
QString display() const; QString display() const;
// Binary comparison operators
bool operator==(const int) const; bool operator==(const int) const;
bool operator==(const float) const; bool operator==(const float) const;
bool operator==(const double) const; bool operator==(const double) const;
@ -32,6 +33,27 @@ namespace FractionNS {
bool operator!=(const double) const; bool operator!=(const double) const;
bool operator!=(const Fraction&) const; bool operator!=(const Fraction&) const;
bool operator<(const int) const;
bool operator<(const float) const;
bool operator<(const double) const;
bool operator<(const Fraction&) const;
bool operator<=(const int) const;
bool operator<=(const float) const;
bool operator<=(const double) const;
bool operator<=(const Fraction&) const;
bool operator>(const int) const;
bool operator>(const float) const;
bool operator>(const double) const;
bool operator>(const Fraction&) const;
bool operator>=(const int) const;
bool operator>=(const float) const;
bool operator>=(const double) const;
bool operator>=(const Fraction&) const;
// Binary math operators
Fraction& operator+(const int) const; Fraction& operator+(const int) const;
Fraction& operator+(const Fraction&) const; Fraction& operator+(const Fraction&) const;
@ -55,5 +77,9 @@ namespace FractionNS {
Fraction& operator/=(const int); Fraction& operator/=(const int);
Fraction& operator/=(const Fraction&); Fraction& operator/=(const Fraction&);
// Unary operators
Fraction& operator-() const;
Fraction& operator+() const; // Should return a copy of the Fraction.
}; };
} }

View file

@ -22,13 +22,16 @@ private slots:
void test_substraction(); void test_substraction();
void test_multiplication(); void test_multiplication();
void test_division(); void test_division();
void test_negation();
void test_unary_plus();
void test_less_than();
void test_greater_than();
}; };
FractionTest::FractionTest() {}; FractionTest::FractionTest() {};
FractionTest::~FractionTest() {}; FractionTest::~FractionTest() {};
void FractionTest::test_constructor() void FractionTest::test_constructor() {
{
// Normal fraction // Normal fraction
Fraction f1 = Fraction(1, 2); Fraction f1 = Fraction(1, 2);
QCOMPARE(f1.getNumerator(), 1); QCOMPARE(f1.getNumerator(), 1);
@ -52,10 +55,17 @@ void FractionTest::test_constructor()
Fraction f5 = Fraction(-2, -3); Fraction f5 = Fraction(-2, -3);
QCOMPARE(f5.getNumerator(), 2); QCOMPARE(f5.getNumerator(), 2);
QCOMPARE(f5.getDenominator(), 3); QCOMPARE(f5.getDenominator(), 3);
// Zero
Fraction f6 = Fraction(0);
QCOMPARE(f6, 0);
Fraction f7 = Fraction(0, 3);
QCOMPARE(f7, 0);
QCOMPARE(f6, f7);
} }
void FractionTest::test_display() void FractionTest::test_display() {
{
// Normal fraction // Normal fraction
Fraction f1 = Fraction(1, 2); Fraction f1 = Fraction(1, 2);
QCOMPARE(f1.display(), "1/2"); QCOMPARE(f1.display(), "1/2");
@ -73,8 +83,7 @@ void FractionTest::test_display()
QCOMPARE(f5.display(), "-5/2"); QCOMPARE(f5.display(), "-5/2");
} }
void FractionTest::test_equivalence() void FractionTest::test_equivalence() {
{
Fraction f1 = Fraction(1, 1); Fraction f1 = Fraction(1, 1);
Fraction f2 = Fraction(1, 1); Fraction f2 = Fraction(1, 1);
Fraction f3 = Fraction(-1, 1); Fraction f3 = Fraction(-1, 1);
@ -114,8 +123,7 @@ 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);
@ -129,8 +137,7 @@ void FractionTest::test_inequivalence()
QVERIFY(f3 != 1.0f); QVERIFY(f3 != 1.0f);
} }
void FractionTest::test_addition() void FractionTest::test_addition() {
{
Fraction f1 = Fraction(1, 2); Fraction f1 = Fraction(1, 2);
QCOMPARE(f1 + 1, 1.5f); QCOMPARE(f1 + 1, 1.5f);
QCOMPARE(f1, 0.5f); QCOMPARE(f1, 0.5f);
@ -150,8 +157,7 @@ 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);
@ -183,8 +189,7 @@ void FractionTest::test_substraction()
QCOMPARE(f6, Fraction(1590, 199)); QCOMPARE(f6, Fraction(1590, 199));
} }
void FractionTest::test_multiplication() void FractionTest::test_multiplication() {
{
Fraction f1 = Fraction(1, 1); Fraction f1 = Fraction(1, 1);
const Fraction f2 = Fraction(2, 1); const Fraction f2 = Fraction(2, 1);
QCOMPARE(f1 * 2, f2); QCOMPARE(f1 * 2, f2);
@ -210,6 +215,112 @@ void FractionTest::test_division() {
QCOMPARE((f1 / 2) / Fraction(2, 3), Fraction(3, 8)); QCOMPARE((f1 / 2) / Fraction(2, 3), Fraction(3, 8));
} }
void FractionTest::test_negation() {
const Fraction f1 = Fraction(-1, 2);
const Fraction f2 = Fraction(1, 2);
QCOMPARE(-f1, f2);
QCOMPARE(-f2, f1);
}
void FractionTest::test_unary_plus() {
const Fraction f1 = Fraction(2, 3);
const Fraction f2 = +f1;
QCOMPARE(f1, f2);
// Check that it's actually a new Fraction.
QVERIFY(&f1 != &f2);
}
void FractionTest::test_less_than() {
const Fraction f1 = Fraction(9, 2);
QVERIFY(f1 < 5);
QVERIFY(f1 < 9.0);
QVERIFY(f1 < 6.0f);
QVERIFY(!(f1 < (9.0/2.0)));
QVERIFY(!(f1 < 0));
const Fraction large_positive = Fraction(1000000, 17);
const Fraction positive = Fraction(9, 8);
const Fraction zero = Fraction(0);
const Fraction negative = Fraction(-9, 8);
const Fraction large_negative = Fraction(-1000000, 17);
QVERIFY(large_negative < large_positive);
QVERIFY(negative < large_positive);
QVERIFY(zero < large_positive);
QVERIFY(positive < large_positive);
QVERIFY(!(large_positive < large_positive));
QVERIFY(large_negative < positive);
QVERIFY(negative < positive);
QVERIFY(zero < positive);
QVERIFY(!(positive < positive));
QVERIFY(!(large_positive < positive));
QVERIFY(large_negative < zero);
QVERIFY(negative < zero);
QVERIFY(!(zero < zero));
QVERIFY(!(positive < zero));
QVERIFY(!(large_positive < zero));
QVERIFY(large_negative < negative);
QVERIFY(!(negative < negative));
QVERIFY(!(zero < negative));
QVERIFY(!(positive < negative));
QVERIFY(!(large_positive < negative));
QVERIFY(!(large_negative < large_negative));
QVERIFY(!(negative < large_negative));
QVERIFY(!(zero < large_negative));
QVERIFY(!(positive < large_negative));
QVERIFY(!(large_positive < large_negative));
}
void FractionTest::test_greater_than() {
const Fraction f1 = Fraction(-9, 2);
QVERIFY(f1 > -5);
QVERIFY(f1 > -9.0);
QVERIFY(f1 > -6.0f);
QVERIFY(!(f1 > -(9.0/2.0)));
QVERIFY(!(f1 > 0));
const Fraction large_positive = Fraction(1000000, 17);
const Fraction positive = Fraction(9, 8);
const Fraction zero = Fraction(0);
const Fraction negative = Fraction(-9, 8);
const Fraction large_negative = Fraction(-1000000, 17);
QVERIFY(!(large_negative > large_negative));
QVERIFY(negative > large_negative);
QVERIFY(zero > large_negative);
QVERIFY(positive > large_negative);
QVERIFY(large_positive > large_negative);
QVERIFY(!(large_negative > negative));
QVERIFY(!(negative > negative));
QVERIFY(zero > negative);
QVERIFY(positive > negative);
QVERIFY(large_positive > negative);
QVERIFY(!(large_negative > zero));
QVERIFY(!(negative > zero));
QVERIFY(!(zero > zero));
QVERIFY(positive > zero);
QVERIFY(large_positive > zero);
QVERIFY(!(large_negative > positive));
QVERIFY(!(negative > positive));
QVERIFY(!(zero > positive));
QVERIFY(!(positive > positive));
QVERIFY(large_positive > positive);
QVERIFY(!(large_negative > large_positive));
QVERIFY(!(negative > large_positive));
QVERIFY(!(zero > large_positive));
QVERIFY(!(positive > large_positive));
QVERIFY(!(large_positive > large_positive));
}
QTEST_APPLESS_MAIN(FractionTest) QTEST_APPLESS_MAIN(FractionTest)
#include "tst_fractiontest.moc" #include "tst_fractiontest.moc"