2021-09-17 12:32:14 +02:00
|
|
|
#pragma once
|
2021-09-17 11:59:28 +02:00
|
|
|
|
2021-09-17 14:47:29 +02:00
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
namespace FractionNS {
|
2021-09-21 11:40:44 +02:00
|
|
|
struct divide_by_zero_error : public std::logic_error { divide_by_zero_error() : std::logic_error("tried to divide by zero") {}; };
|
|
|
|
struct not_real_error : public std::logic_error { not_real_error() : std::logic_error("value is not a real number") {}; };
|
|
|
|
struct not_defined_error : public std::logic_error { not_defined_error() : std::logic_error("operation result is not defined") {}; };
|
2021-09-21 10:42:04 +02:00
|
|
|
|
2021-09-17 14:47:29 +02:00
|
|
|
class Fraction
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
int m_numerator, m_denominator;
|
2021-09-20 16:34:45 +02:00
|
|
|
void reduce();
|
2021-09-17 14:47:29 +02:00
|
|
|
public:
|
2021-09-17 12:32:14 +02:00
|
|
|
Fraction(int, int);
|
2021-09-17 14:47:29 +02:00
|
|
|
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;
|
2021-09-21 11:40:44 +02:00
|
|
|
Fraction& inv() const;
|
|
|
|
|
|
|
|
float toFloat() const;
|
|
|
|
double toDouble() const;
|
2021-09-17 14:47:29 +02:00
|
|
|
|
2021-09-21 10:03:45 +02:00
|
|
|
// Binary comparison operators
|
2021-09-17 14:47:29 +02:00
|
|
|
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;
|
|
|
|
|
2021-09-21 10:03:45 +02:00
|
|
|
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;
|
|
|
|
|
2021-09-21 11:43:00 +02:00
|
|
|
// <=> would go here
|
|
|
|
|
2021-09-21 10:03:45 +02:00
|
|
|
// Binary math operators
|
2021-09-17 14:47:29 +02:00
|
|
|
Fraction& operator+(const int) const;
|
|
|
|
Fraction& operator+(const Fraction&) const;
|
|
|
|
|
|
|
|
Fraction& operator+=(const int);
|
|
|
|
Fraction& operator+=(const Fraction&);
|
2021-09-20 16:34:45 +02:00
|
|
|
|
|
|
|
Fraction& operator-(const int) const;
|
|
|
|
Fraction& operator-(const Fraction&) const;
|
|
|
|
|
|
|
|
Fraction& operator-=(const int);
|
|
|
|
Fraction& operator-=(const Fraction&);
|
2021-09-21 09:22:46 +02:00
|
|
|
|
|
|
|
Fraction& operator*(const int) const;
|
|
|
|
Fraction& operator*(const Fraction&) const;
|
|
|
|
|
|
|
|
Fraction& operator*=(const int);
|
|
|
|
Fraction& operator*=(const Fraction&);
|
2021-09-21 09:30:49 +02:00
|
|
|
|
|
|
|
Fraction& operator/(const int) const;
|
|
|
|
Fraction& operator/(const Fraction&) const;
|
|
|
|
|
|
|
|
Fraction& operator/=(const int);
|
|
|
|
Fraction& operator/=(const Fraction&);
|
2021-09-21 10:03:45 +02:00
|
|
|
|
2021-09-21 11:42:53 +02:00
|
|
|
// Maybe replace with ^ operator, as it's unused for this class.
|
|
|
|
// Might be syntactically not-so-nice though?
|
|
|
|
// Python uses / for combining paths, does cpp do something similar anywhere?
|
|
|
|
// How does one raise a Fraction to a power while keeping it a Fraction (avoid double return type)
|
|
|
|
double pow(const int) const;
|
|
|
|
double pow(const Fraction&) const;
|
|
|
|
|
2021-09-21 10:03:45 +02:00
|
|
|
// Unary operators
|
|
|
|
Fraction& operator-() const;
|
|
|
|
Fraction& operator+() const; // Should return a copy of the Fraction.
|
2021-09-17 14:47:29 +02:00
|
|
|
};
|
|
|
|
}
|