90 lines
2.4 KiB
C++
90 lines
2.4 KiB
C++
#pragma once
|
|
|
|
#include <QString>
|
|
|
|
namespace FractionNS {
|
|
class divide_by_zero_error : public std::logic_error {
|
|
public:
|
|
divide_by_zero_error() : std::logic_error("tried to divide by zero") {};
|
|
};
|
|
|
|
class Fraction
|
|
{
|
|
private:
|
|
int m_numerator, m_denominator;
|
|
void reduce();
|
|
public:
|
|
Fraction(int, int);
|
|
Fraction(int);
|
|
Fraction(double);
|
|
Fraction(float);
|
|
Fraction(const Fraction&);
|
|
|
|
int getNumerator() const;
|
|
int getDenominator() const;
|
|
void setNumerator(const int);
|
|
void setDenominator(const int);
|
|
|
|
QString display() const;
|
|
|
|
// Binary comparison operators
|
|
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;
|
|
|
|
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 Fraction&) const;
|
|
|
|
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&);
|
|
|
|
Fraction& operator*(const int) const;
|
|
Fraction& operator*(const Fraction&) const;
|
|
|
|
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&);
|
|
|
|
// Unary operators
|
|
Fraction& operator-() const;
|
|
Fraction& operator+() const; // Should return a copy of the Fraction.
|
|
};
|
|
}
|