#include "fraction.h"

fraction fraction::operator+(const fraction &frac) {
        fraction tmp = *this;
        tmp.numerator = tmp.numerator * frac.denominator +
                        tmp.denominator * frac.numerator;
        tmp.denominator = tmp.denominator * frac.denominator;
        return tmp.simplify();
}

fraction fraction::operator*(const fraction &frac) {
        fraction tmp = *this;
        tmp.numerator = tmp.numerator * frac.numerator;
        tmp.denominator = tmp.denominator * frac.denominator;
        return tmp.simplify();
}

bool fraction::operator==(const fraction &frac) {
        this->simplify();
        return gcd(frac.numerator, this->numerator) == this->numerator &&
               gcd(frac.denominator, this->denominator) == this->denominator;
}

bool fraction::operator!=(const fraction &frac) {
        return !(*this == frac);
}

fraction &fraction::simplify() {
        int g = gcd(numerator, denominator);
        numerator /= g;
        denominator /= g;
        return *this;
}

float fraction::real() const {
        return static_cast<float>(numerator) / denominator;
}

int fraction::gcd(int a, int b) {
        if (a % b == 0)
                return b;

        return gcd(b, a % b);
}

fraction &fraction::operator=(const fraction &frac) {
        numerator = frac.numerator;
        denominator = frac.denominator;
        this->simplify();
        return *this;
}

fraction &fraction::operator*=(const fraction &frac) {
        numerator *= frac.numerator;
        denominator *= frac.denominator;
        return *this;
}
