Пример #1
0
        public static MyFraction AddSubstractHandler(MyFraction num1, MyFraction num2, MathAction action)
        {
            // preparing output variable
            MyFraction resultFraction = new MyFraction();

            // creating variables which we can change later in the if statement
            int newNominator1, newNominator2, newNominator, newDenominator;

            // based on equality or unequality of denominators, their further calculations are different
            if (num1.Denominator != num2.Denominator)
            {
                newNominator1 = num1.Nominator * num2.Denominator;
                newNominator2 = num2.Nominator * num1.Denominator;
                // the only difference between addition and substration is handled here by a ternary operator
                newNominator               = action == MathAction.ADD ? newNominator1 + newNominator2 : newNominator1 - newNominator2;
                newDenominator             = num1.Denominator * num2.Denominator;
                resultFraction.Nominator   = newNominator;
                resultFraction.Denominator = newDenominator;
            }
            else
            {
                // ternary operator again
                newNominator               = action == MathAction.ADD ? num1.Nominator + num2.Nominator : num1.Nominator - num2.Nominator;
                resultFraction.Nominator   = newNominator;
                resultFraction.Denominator = num1.Denominator;
            }

            return(resultFraction);
        }
Пример #2
0
        public static MyFraction ShortenFraction(MyFraction num)
        {
            // greatest common divisor
            int gcd = Utils.FindGreatestCommonDivisor(num.Nominator, num.Denominator);

            num.Nominator   /= gcd;
            num.Denominator /= gcd;

            return(num);
        }
Пример #3
0
        public static MyFraction MathOperationHandler(MyFraction num1, MyFraction num2, MathAction action)
        {
            MyFraction output = new MyFraction();

            switch (action)
            {
            case MathAction.ADD:
            case MathAction.SUBSTRACT:
                output = Utils.AddSubstractHandler(num1, num2, action);
                break;

            case MathAction.MULTIPLY:
            case MathAction.DIVIDE:
                output = Utils.MultiplyDivideHandler(num1, num2, action);
                break;

            default:
                break;
            }

            return(output);
        }
Пример #4
0
        public static MyFraction MultiplyDivideHandler(MyFraction num1, MyFraction num2, MathAction action)
        {
            // preparing output variable
            MyFraction resultFraction = new MyFraction();

            // in case of division we simply reverse the second fraction
            if (action == MathAction.DIVIDE)
            {
                int nominator   = num2.Nominator;
                int denominator = num2.Denominator;
                num2.Denominator = nominator;
                num2.Nominator   = denominator;
            }

            resultFraction.Nominator   = num1.Nominator * num2.Nominator;
            resultFraction.Denominator = num1.Denominator * num2.Denominator;

            // attempting to shorten the result fraction
            resultFraction = Utils.ShortenFraction(resultFraction);

            return(resultFraction);
        }
Пример #5
0
 // method for the delegate and event from the 4th task
 public static void FractionChange(MyFraction fraction, int num)
 {
     Console.WriteLine("Change event fired");
 }
Пример #6
0
        // TESTING
        public static void Main(string[] args)
        {
            // creating 2 fractions
            MyFraction num1 = new MyFraction(1, 5);
            MyFraction num2 = new MyFraction(5, 3);

            // signing to the event from the 4th task
            Console.WriteLine("Checking Change event:");
            num1.Change     += FractionChange;
            num1.Nominator   = 2;
            num1.Denominator = 5;

            Console.WriteLine("");

            // checking getting a decimal from a fraction
            Console.WriteLine($"A decimal from the first fraction: {Utils.GetDecimal(num1)}");

            Console.WriteLine("");

            // checking addition
            MyFraction result1 = num1 + num2;

            Console.WriteLine("Addition:");
            Console.WriteLine(result1.Nominator);
            Console.WriteLine("---");
            Console.WriteLine(result1.Denominator);

            Console.WriteLine("");

            // checking substration
            MyFraction result2 = num1 - num2;

            Console.WriteLine("Substraction:");
            Console.WriteLine(result2.Nominator);
            Console.WriteLine("---");
            Console.WriteLine(result2.Denominator);

            Console.WriteLine("");

            // checking multiplication and fraction shortening
            MyFraction result3 = num1 * num2;

            Console.WriteLine("Multiplication:");
            Console.WriteLine(result3.Nominator);
            Console.WriteLine("---");
            Console.WriteLine(result3.Denominator);

            Console.WriteLine("");

            // checking division and fraction shortening
            MyFraction result4 = num1 / num2;

            Console.WriteLine("Division:");
            Console.WriteLine(result4.Nominator);
            Console.WriteLine("---");
            Console.WriteLine(result4.Denominator);

            Console.WriteLine("");

            // indexer check
            MyFraction num3 = new MyFraction(10, 35);

            Console.WriteLine("Indexer checking:");
            Console.WriteLine($"index 0 for num1: {num3[0]}");
            Console.WriteLine($"index 1 for num1: {num3[1]}");
        }
Пример #7
0
 public static decimal GetDecimal(MyFraction num)
 {
     // nominator and denominator are of int type, that's why we have type convertion here
     return(Math.Round((decimal)num.Nominator / (decimal)num.Denominator, 4));
 }