#include "Fraction.h" namespace ecci { /*ecci::*/Fraction::Fraction(integer numerator, integer denominator) : numerator(numerator) // esto SI es inicializacion en C++ , denominator{denominator ? denominator : 1} { //numerator = 0; // Esto NO es inicializacion //denominator = 1; // es asignacion this->simplify(); } void /*ecci::*/Fraction::print() const { std::cout << this->getNumerator() << '/' << this->denominator; } void Fraction::simplify() { integer gcd = Fraction::gcd(this->numerator, this->denominator); this->numerator /= gcd; this->denominator /= gcd; if ( this->denominator < 0 ) { this->numerator *= -1; this->denominator *= -1; } } integer Fraction::gcd(integer a, integer b) { a = a >= 0 ? a : -a; b = b >= 0 ? b : -b; while ( b > 0 ) { integer t = b; b = a % b; a = t; } return a; } Fraction Fraction::add(Fraction other) const { Fraction result; result.numerator = this->numerator * other.denominator + this->denominator * other.numerator; result.denominator = this->denominator * other.denominator; result.simplify(); return result; } /* Fraction Fraction::operator+(Fraction other) const { return Fraction( this->numerator * other.denominator + this->denominator * other.numerator , this->denominator * other.denominator ); } */ } // namespace ecci