#ifndef FRACTION_H #define FRACTION_H #include namespace ecci { #define implicit typedef long long integer; class Fraction { private: integer numerator = 0; // esto es tambien es inicializacion a partir de C++11 integer denominator = 1; public: // Default constructor // Conversion constructor /*explicit*/ implicit Fraction(integer numerator = 0, integer denominator = 1); /** * [getNumerator description] * @return [description] */ inline integer getNumerator() const { return this->numerator; } inline void setNumerator(integer numerator) { Fraction::numerator = numerator; this->simplify(); } inline integer getDenominator() const { return this->denominator; } inline void setDenominator(integer denominator) { if ( denominator ) { this->denominator = denominator; this->simplify(); } } void print() const; void simplify(); // Metodo de clase static integer gcd(integer a, integer b); Fraction add(Fraction other) const; // Fraction operator+(Fraction other) const; friend Fraction operator+(const Fraction& one, const Fraction& two) { return Fraction( one.numerator * two.denominator + one.denominator * two.numerator , one.denominator * two.denominator ); } friend std::istream& operator>>(std::istream& in, Fraction& fraction) { integer denominator; in >> fraction.numerator; in.ignore(std::numeric_limits::max(), '/'); in >> denominator; fraction.setDenominator(denominator); return in; } friend std::ostream& operator<<(std::ostream& out, const Fraction& fraction) { return out << fraction.getNumerator() << '/' << fraction.denominator; } }; } // namespace ecci #endif // FRACTION_H