2021-09-17 12:32:14 +02:00
|
|
|
#include "fraction.hpp"
|
2021-09-17 11:59:28 +02:00
|
|
|
|
2021-09-17 14:47:29 +02:00
|
|
|
namespace FractionNS {
|
|
|
|
|
|
|
|
int gcd(int p, int q) {
|
|
|
|
while(true) {
|
|
|
|
if(p == 0) return q;
|
|
|
|
q %= p;
|
|
|
|
if(q == 0) return p;
|
|
|
|
p %= q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int lcm(int p, int q) {
|
|
|
|
int gcd = FractionNS::gcd(p, q);
|
|
|
|
|
|
|
|
return gcd != 0 ? (p / gcd * q) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int abs(int n) {
|
|
|
|
if(n < 0) return -n;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Fraction::Fraction(int numerator, int denominator)
|
|
|
|
{
|
|
|
|
auto gcd = FractionNS::gcd(FractionNS::abs(numerator), FractionNS::abs(denominator));
|
|
|
|
|
|
|
|
if(denominator < 0) {
|
|
|
|
numerator *= -1;
|
|
|
|
denominator *= -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->m_numerator = numerator / gcd;
|
|
|
|
this->m_denominator = denominator / gcd;
|
|
|
|
}
|
2021-09-17 14:52:24 +02:00
|
|
|
Fraction::Fraction(int n) {
|
|
|
|
this->m_numerator = n;
|
|
|
|
this->m_denominator = 1;
|
|
|
|
}
|
|
|
|
Fraction::Fraction(float n) {
|
|
|
|
// TODO
|
|
|
|
throw std::runtime_error("Not yet implemented");
|
|
|
|
}
|
|
|
|
Fraction::Fraction(const Fraction& n) {
|
|
|
|
this->m_numerator = n.getNumerator();
|
|
|
|
this->m_denominator = n.getDenominator();
|
|
|
|
}
|
2021-09-17 14:47:29 +02:00
|
|
|
|
|
|
|
int Fraction::getNumerator() const { return m_numerator; }
|
|
|
|
int Fraction::getDenominator() const { return m_denominator; }
|
|
|
|
|
|
|
|
void Fraction::setNumerator(int numerator) {
|
|
|
|
auto gcd = std::__gcd(numerator, this->m_denominator);
|
|
|
|
this->m_numerator = numerator / gcd;
|
|
|
|
}
|
|
|
|
void Fraction::setDenominator(int denominator) {
|
|
|
|
auto gcd = std::__gcd(this->m_numerator, denominator);
|
|
|
|
this->m_denominator = denominator / gcd;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Fraction::display() const {
|
|
|
|
return QString::number(m_numerator) + '/' + QString::number(m_denominator);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Fraction::operator==(const int n) const {
|
|
|
|
return (double(m_numerator) / double(m_denominator)) == n;
|
|
|
|
}
|
|
|
|
bool Fraction::operator==(const float n) const {
|
|
|
|
return (double(m_numerator) / double(m_denominator)) == n;
|
|
|
|
}
|
|
|
|
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 int n) const {
|
|
|
|
return (double(m_numerator) / double(m_denominator)) != n;
|
|
|
|
}
|
|
|
|
bool Fraction::operator!=(const float n) const {
|
|
|
|
return (double(m_numerator) / double(m_denominator)) != n;
|
|
|
|
}
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
Fraction& Fraction::operator+(const int n) const {
|
|
|
|
return Fraction(n) + *this;
|
|
|
|
}
|
|
|
|
Fraction& Fraction::operator+(const float n) const {
|
|
|
|
return Fraction(n) + *this;
|
|
|
|
}
|
|
|
|
Fraction& Fraction::operator+(const double n) const {
|
|
|
|
return Fraction(n) + *this;
|
|
|
|
}
|
|
|
|
Fraction& Fraction::operator+(const Fraction& other) const {
|
|
|
|
auto lcm = FractionNS::lcm(getDenominator(), other.getDenominator());
|
|
|
|
return *new Fraction((getNumerator() + other.getNumerator()) * lcm, getDenominator() * lcm);
|
|
|
|
}
|
2021-09-17 11:59:28 +02:00
|
|
|
}
|