Основы объектно-ориентированного программирования на языке C# book | Page 149

7.7 Полiморфiзм Euklid(ref a, ref b); } // Замiна знака public static Rational operator -(Rational x) { Rational t = new Rational(); t.a = -x.a; t.b = x.b; return t; } // Iнкремент public static Rational operator ++(Rational x) { Rational t = new Rational(); t.a = x.a + x.b; t.b = x.b; return t; } // Додавання дробiв public static Rational operator +(Rational x, Rational y) { int t1 = x.a * y.b + x.b * y.a; int t2 = x.b * y.b; Euklid(ref t1, ref t2); return new Rational(t1, t2); } // Множення дробiв public static Rational operator *(Rational x, Rational y) { int t1 = x.a * y.a; int t2 = x.b * y.b; Euklid(ref t1, ref t2); return new Rational(t1, t2); } // Скорочення дробi private static void Euklid(ref int a, ref int b) { int x = Math.Abs(a), y = b; while (x > 0 && y > 0) { if (x > y) x = x % y; else y = y % x; } int k = x + y; if (k > 1) { a /= k; b /= k; } } // Друк дробi public void Display() { Console.WriteLine("{0}/{1}", a, b); } // Перевантаження оператору порiвняння public static bool operator >(Rational x, Rational y) 149