예제 #1
0
 public static SimpleFraction GetQuotient(SimpleFraction fraction1, SimpleFraction fraction2)
 {
     try
     {
         var temp = new SimpleFraction(fraction2.Denominator, fraction2.Numerator);
         return(SimpleFraction.GetComposition(fraction1, temp));
     }
     catch (DivideByZeroException)
     {
         throw new DivideByZeroException("Попытка поделить на дробь с нулевым знаменателем!!!");
     }
     catch (Exception)
     {
         throw;
     }
 }
예제 #2
0
        public static SimpleFraction GetSum(SimpleFraction fraction1, SimpleFraction fraction2)
        {
            SimpleFraction result;

            if (fraction1.Denominator == fraction2.Denominator)
            {
                result = new SimpleFraction(fraction1.Numerator + fraction2.Numerator, fraction1.Denominator);
            }
            else
            {
                long multiple      = GetMinCommonMultiple(fraction1.Denominator, fraction2.Denominator);
                long newNumerator1 = fraction1.Numerator * (multiple / fraction1.Denominator);
                long newNumerator2 = fraction2.Numerator * (multiple / fraction2.Denominator);
                result = new SimpleFraction(newNumerator1 + newNumerator2, multiple);
            }
            return(result);
        }
예제 #3
0
        static void Main(string[] args)
        {
            const int N              = 10;
            var       array          = new SimpleFraction[N];
            var       rndNumerator   = new Random();
            var       rndDenominator = new Random();

            for (int i = 0; i < N; i++)
            {
                array[i] = new SimpleFraction(rndNumerator.Next(-100, 100), rndDenominator.Next(1, 100));
            }
            Console.WriteLine("Original array (size = {0}):", N);
            foreach (var i in array)
            {
                Console.Write("{0} ", i.ToString());
            }
            Console.WriteLine();
            Array.Sort(array);
            Console.WriteLine("Sorted array (size = {0}):", N);
            foreach (var i in array)
            {
                Console.Write("{0} ", i.ToString());
            }
            Console.WriteLine();
            var sum = new SimpleFraction();

            foreach (var i in array)
            {
                sum = SimpleFraction.GetSum(sum, i);
            }
            Console.WriteLine("sum of all fractions = {0}", sum.ToString());
            var rndIndex = new Random();
            var index1   = rndIndex.Next(0, 9);
            var index2   = N - index1 - 1;

            Console.WriteLine("For example,\nresults of operation between {0}-th ({1}) and {2}-th ({3}) fractions:", index1 + 1, array[index1], index2 + 1, array[index2]);
            Console.WriteLine("Sum = {0}", SimpleFraction.GetSum(array[index1], array[index2]));
            Console.WriteLine("Difference = {0}", SimpleFraction.GetDifference(array[index1], array[index2]));
            Console.WriteLine("Composition = {0}", SimpleFraction.GetComposition(array[index1], array[index2]));
            Console.WriteLine("Quotient = {0}", SimpleFraction.GetQuotient(array[index1], array[index2]));
        }
예제 #4
0
 public static SimpleFraction GetComposition(SimpleFraction fraction1, SimpleFraction fraction2)
 {
     try
     {
         SimpleFraction result;
         var            temp1 = new SimpleFraction(fraction1.Numerator, fraction2.Denominator);
         var            temp2 = new SimpleFraction(fraction2.Numerator, fraction1.Denominator);
         result = new SimpleFraction(temp1.Numerator * temp2.Numerator, temp1.Denominator * temp2.Denominator);
         return(result);
     }
     catch (ArgumentOutOfRangeException)
     {
         throw new ArgumentOutOfRangeException("Числитель и/или знаменатель выходит за границы диапазона для целых чисел!!!");
     }
     catch (ArithmeticException)
     {
         throw;
     }
     catch (Exception)
     {
         throw new Exception("Произошла какая-то ошибка!!!");
     }
 }
예제 #5
0
        public static SimpleFraction GetQuotient(SimpleFraction fraction1, SimpleFraction fraction2)
        {
            var temp = new SimpleFraction(fraction2.Denominator, fraction2.Numerator);

            return(SimpleFraction.GetComposition(fraction1, temp));
        }
예제 #6
0
        public static SimpleFraction GetDifference(SimpleFraction fraction1, SimpleFraction fraction2)
        {
            var temp = new SimpleFraction(-fraction2.Numerator, fraction2.Denominator);

            return(SimpleFraction.GetSum(fraction1, temp));
        }