public static Rational operator *(Rational num1, Rational num2) { int numer = num1.numerator * num2.GetNumerator(); int denom = num1.denominator * num2.GetDenominator(); Rational result = new Rational(numer, denom); return result; }
public static Rational operator -(Rational num1, Rational num2) { int commonDenom = num1.denominator * num2.GetDenominator(); int numer1 = num1.numerator * num2.GetDenominator(); int numer2 = num2.GetNumerator() * num1.denominator; int difference = numer1 - numer2; Rational result = new Rational(difference, commonDenom); return result; }
public static Rational operator +(Rational num1, Rational num2) { int commonDenom = num1.denominator * num2.GetDenominator(); int numer1 = num1.numerator * num2.GetDenominator(); int numer2 = num2.GetNumerator() * num1.denominator; int sum = numer1 + numer2; Rational result = new Rational(sum, commonDenom); return result; }
public static void Main(string[] args) { Rational a = new Rational(3, 4); Rational b = new Rational(4, 5); Rational c = a + b; Console.WriteLine(c.StrVal()); Console.WriteLine(c.ToString()); c = b - a; Console.WriteLine(c.StrVal()); Console.WriteLine(c.ToString()); }