A.cpp

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
3.6 kB
20
Indexable
Never
std::ostream& operator<<(std::ostream& os, const Fraction& p){
    if(p)   return os << p.nominator_ << "/" << p.denominator_;
    else    return os << "NaN";
}

Fraction::Fraction(){
    nominator_ = 0;   denominator_ = 1;
}

Fraction::Fraction(int nominator, int denominator){
    nominator_ = nominator;   denominator_ = denominator;
    if(denominator == 0){
        nominator = 1;
        return;
    }

    if(denominator_ < 0){
        denominator_ *= -1;
        nominator_ *= -1;
    }

    int gcd = std::gcd(abs(nominator_), denominator_);

    nominator_ /= gcd;
    denominator_ /= gcd;
}

Fraction Fraction::operator+(const Fraction &rhs) const{
    if(rhs.denominator_ == 0 || this->denominator_ == 0){
        return Fraction(1, 0);
    }

    int den = denominator_ * rhs.denominator_;
    int nom = nominator_ * rhs.denominator_ + rhs.nominator_ * denominator_;

    int gcd = std::gcd(abs(nom), den);

    return Fraction(nom / gcd, den / gcd);
}

Fraction Fraction::operator-(const Fraction &rhs) const{
    if(rhs.denominator_ == 0 || this->denominator_ == 0){
        return Fraction(1, 0);
    }

    int den = denominator_ * rhs.denominator_;
    int nom = nominator_ * rhs.denominator_ - rhs.nominator_ * denominator_;

    int gcd = std::gcd(abs(nom), den);

    return Fraction(nom / gcd, den / gcd);
}

Fraction Fraction::operator*(const Fraction &rhs) const{
    if(rhs.denominator_ == 0 || this->denominator_ == 0){
        return Fraction(1, 0);
    }

    int den = denominator_ * rhs.denominator_;
    int nom = nominator_ * rhs.nominator_;

    int gcd = std::gcd(abs(den), nom);

    return Fraction(nom / gcd, den / gcd);
}

Fraction Fraction::operator/(const Fraction &rhs) const{
    if(rhs.denominator_ == 0 || this->denominator_ == 0){
        return Fraction(1, 0);
    }

    int den = denominator_ * rhs.nominator_;
    int nom = nominator_ * rhs.denominator_;

    int gcd = std::gcd(abs(den), nom);

    return Fraction(nom / gcd, den / gcd);
}

bool Fraction::operator==(const Fraction &rhs) const{
    if(this->denominator_ == 0 || rhs.denominator_ == 0)   return 0;
    return denominator_ == rhs.denominator_ && nominator_ == rhs.nominator_;
}

bool Fraction::operator!=(const Fraction &rhs) const{
    if(this->denominator_ == 0 || rhs.denominator_ == 0)   return 0;
    return denominator_ != rhs.denominator_ || nominator_ != rhs.nominator_;
}

bool Fraction::operator<(const Fraction& rhs) const{
    if(this->denominator_ == 0 || rhs.denominator_ == 0)   return 0;
    Fraction diff = Fraction(this->nominator_, this->denominator_);
    diff = diff - rhs;

    return diff.nominator_ < 0;
}

bool Fraction::operator>(const Fraction& rhs) const{
    if(this->denominator_ == 0 || rhs.denominator_ == 0)   return 0;
    Fraction diff = Fraction(this->nominator_, this->denominator_);
    diff = diff - rhs;

    return diff.nominator_ > 0;
}


bool Fraction::operator<=(const Fraction& rhs) const{
    if(this->denominator_ == 0 || rhs.denominator_ == 0)   return 0;
    Fraction diff = Fraction(this->nominator_, this->denominator_);
    diff = diff - rhs;

    return diff.nominator_ <= 0;
}


bool Fraction::operator>=(const Fraction& rhs) const{
    if(this->denominator_ == 0 || rhs.denominator_ == 0)   return 0;
    Fraction diff = Fraction(this->nominator_, this->denominator_);
    diff = diff - rhs;

    return diff.nominator_ >= 0;
}

bool Fraction::IsNegative() const{
    return (this->nominator_ < 0 && this->denominator_ != 0);
}

Fraction::operator bool() const{
    return this->denominator_ != 0;
}
Leave a Comment