#include int gcd(int,int); void simplify(int*,int*); struct rational { int num; int den; rational(){ num = 0; den = 1; } rational(int a){ num = a; den = 1; } int numerator(){ return num; } int denominator(){ return den; } }; int gcd(int a, int b){ if(a < 0) return gcd(-a,b); if(b < 0) return gcd(a,-b); if(b == 0) return a; return gcd(b, a%b); } void simplify(int* a,int* b){ int g = gcd(*a, *b); *a /= g; *b /= g; } void simplify(long* a,long* b){ long g = gcd(*a, *b); *a /= g; *b /= g; } int gcd(long a, long b){ if(a < 0) return gcd(-a,b); if(b < 0) return gcd(a,-b); if(b == 0) return a; return gcd(b, a%b); } rational operator+(rational a,rational b){ rational c; c.den = a.denominator() * b.denominator(); c.num = (a.numerator() * b.denominator()) + (a.denominator() * b.numerator()); simplify(&c.num,&c.den); return c; } rational operator+(rational a,int b){ rational c; c.den = a.denominator(); c.num = a.numerator() + b; simplify(&c.num,&c.den); return c; } rational operator-(rational a, rational b){ rational c; c.den = a.denominator() * b.denominator(); c.num = (a.numerator() * b.denominator()) - (a.denominator() * b.numerator()); simplify(&c.num,&c.den); return c; } rational operator-(int a, rational b){ rational c; c.den = b.denominator(); c.num = a * b.denominator() + b.numerator(); simplify(&c.num, &c.den); return c; } rational operator-(rational a, int b){ rational c; c.den = a.denominator(); c.num = a.numerator() - b; simplify(&c.num, &c.den); return c; } rational operator*(rational a, rational b){ rational c; c.den = a.denominator() * b.denominator(); c.num = a.numerator() * b.numerator(); simplify(&c.num, &c.den); return c; } rational operator*(rational a,int b){ rational c; c.den = a.denominator(); c.num = a.numerator() * b; simplify(&c.num, &c.den); return c; } rational operator/(rational a, rational b){ rational c; long one = a.numerator() * b.denominator(); long two = a.denominator() * b.numerator(); simplify(&one, &two); c.num = (int)one; c.den = (int)two; return c; } rational operator/(int a, rational b){ rational c; c.num = a * b.denominator(); c.den = b.numerator(); simplify(&c.num, &c.den); return c; } rational operator/(rational a,int b){ rational c; c.num = a.numerator(); c.den = a.denominator() * b; simplify(&c.num, &c.den); return c; }