Exemplo n.º 1
0
 static void Main()
 {
     Fraction fraction1 = new Fraction(22, 0);
     Fraction fraction2 = new Fraction(40, 4);
     Fraction result = fraction1 + fraction2;
     Console.WriteLine(result.Numerator);
     Console.WriteLine(result.Denominator);
     Console.WriteLine(result);
 }
Exemplo n.º 2
0
        private static Fraction Add(Fraction left, Fraction right)
        {
            var gcd = GCD(left.Denominator, right.Denominator);
            var leftDenominator = left.Denominator/gcd;
            var rightDenominator = right.Denominator/gcd;

            var numerator = left.Numerator*rightDenominator + right.Numerator*leftDenominator;
            var denominator = leftDenominator*rightDenominator*gcd;

            return new Fraction(numerator, denominator);
        }
        static void Main()
        {
            var fraction1 = new Fraction(22, 7);
            var fraction2 = new Fraction(40, 4);
            var result = fraction1 + fraction2;
            Console.WriteLine(result.Numerator);
            Console.WriteLine(result.Denominator);
            Console.WriteLine(result);

            var fraction3 = new Fraction(long.MaxValue, long.MinValue);
            var fraction4 = new Fraction(long.MinValue, long.MaxValue);

            try
            {
                result = fraction3 + fraction4;
                Console.WriteLine(result);
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                result = fraction3 - fraction4;
                Console.WriteLine(result);
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                var fraction5 = new Fraction(1, 0);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 4
0
 private static Fraction Negate(Fraction frac)
 {
     return new Fraction(-frac.Numerator, frac.Denominator);
 }