#include #include #include #include class Fraction { private: int integer; int nominator; int denominator; void simplify(){ /*if (nominator < 0){ integer *= -1; nominator *= -1; }*/ if (denominator < 0){ integer *= -1; denominator *= -1; } if (nominator > denominator){ integer = nominator / denominator; nominator %= denominator; } if (std::abs(nominator) < 2)return; //std::cout << integer << " " << nominator << "/" << denominator << std::endl; //std::cout << std::endl; int gcd = getGCD(std::abs(nominator), denominator); nominator /= gcd; denominator /= gcd; } static int getGCD(int a, int b){ while (a != b) if (a > b) a -= b; else b -= a; return a; } public: Fraction(){ } Fraction(int integer, int nominator, int denominator) : integer(integer), nominator(nominator), denominator(denominator){ if (this->denominator == 0) std::cout << "Division by zero" << std::endl; simplify(); } Fraction(Fraction &other) :integer(other.getInteger()), nominator(other.getNominator()), denominator(other.getDenominator()){} int getInteger() { return integer; } int getNominator() { return nominator; } int getDenominator() { return denominator; } friend std::ostream &operator<<(std::ostream &stream, Fraction &fraction){ return stream << fraction.getInteger() << " " << fraction.getNominator() << "/" << fraction.getDenominator(); } friend std::istream &operator>>(std::istream &input, Fraction &fraction){ input >> fraction.integer; input >> fraction.nominator; input >> fraction.denominator; return input; } }; Fraction operator+(Fraction &a, Fraction &b) { int comInteger = a.getInteger() + b.getInteger(); int comNominator = a.getNominator()*b.getDenominator() + b.getNominator()*a.getDenominator(); int comDenominator = a.getDenominator() * b.getDenominator(); return Fraction(comInteger, comNominator, comDenominator); } Fraction operator-(Fraction &a){ return Fraction(-a.getInteger(), -a.getNominator(), a.getDenominator()); } Fraction operator-(Fraction &a, Fraction &b){ return a + (-b); } Fraction operator*(Fraction &a, Fraction &b){ int aNominator = a.getInteger()*a.getDenominator() + a.getNominator(); int bNominator = b.getInteger()*b.getDenominator() + b.getNominator(); int comInteger = 0; return Fraction(comInteger, aNominator*bNominator, a.getDenominator()*b.getDenominator()); } Fraction operator/(Fraction &a, Fraction &b){ int aNominator = a.getInteger()*a.getDenominator() + a.getNominator(); int bNominator = b.getInteger()*b.getDenominator() + b.getNominator(); int comInteger = 0; return Fraction(comInteger, aNominator*b.getDenominator(), a.getDenominator()*bNominator); } int main() { Fraction a, b; std::cin >> a >> b; std::cout << a * b << std::endl; //std::cout << a / b << std::endl; getchar(); getchar(); return 0; }