Exemplo n.º 1
0
        /// <summary>
        /// Multiply two fractions
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns>The product of a and b</returns>
        public static FractionModel Multiply(FractionModel a, FractionModel b)
        {
            var res = new FractionModel(a.Numerator * b.Numerator, a.Denominator * b.Denominator);

            res.Simplify();
            return(res);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reduce a fraction
        /// </summary>
        /// <param name="m">The fraction to reduce</param>
        public static void Simplify(this FractionModel m)
        {
            int gcd = FindGreatestCommonFactor(m);

            m.Numerator   /= gcd;
            m.Denominator /= gcd;

            //return new FractionModel(m.Numerator / gcd, m.Denominator / gcd);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Subtracts two fractions
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns>The difference of a and f=b</returns>
        public static FractionModel Subtract(FractionModel a, FractionModel b)
        {
            int           lcm = FindLeastCommonMultiple(a.Denominator, b.Denominator);
            int           num = a.Numerator * (lcm / a.Denominator) - b.Numerator * (lcm / b.Denominator);
            FractionModel res = new FractionModel(num, lcm);

            res.Simplify();

            return(res);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Convert a decimal to a fraction
        /// </summary>
        /// <param name="d">The decimal to convert</param>
        /// <returns>The decimal represented as a fraction</returns>
        public static FractionModel ConvertDecimalToFraction(decimal d)
        {
            double decimalPlaces = NumberOfDecimalPlaces(d);
            int    multiplier    = (int)Math.Pow(10, decimalPlaces);

            FractionModel fraction = new FractionModel((int)(d * multiplier), multiplier);

            fraction.Simplify();

            return(fraction);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Divides two fractions
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns>The quotient of a and b</returns>
        public static FractionModel Divide(FractionModel a, FractionModel b)
        {
            var res = Multiply(a, GetReciprocal(b));

            res.Simplify();

            if (res.Denominator < 0 && res.Numerator < 0)
            {
                res.Numerator   = Math.Abs(res.Numerator);
                res.Denominator = Math.Abs(res.Denominator);
            }

            return(res);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Try to parse a fraction
        /// </summary>
        /// <param name="value">The value to parse</param>
        /// <param name="fraction">The result of parsing value, or 0 on failure</param>
        /// <returns>True on success, false on failure</returns>
        public static bool TryParseFraction(string value, out FractionModel fraction)
        {
            fraction = new FractionModel(0, 1);

            try
            {
                fraction = ParseFraction(value);
            }
            catch (ArgumentException)
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Finds the greatest common divisor of a fraction
 /// </summary>
 /// <param name="model">The fraction model to find the GCD of</param>
 /// <returns>The greatest common divisor</returns>
 public static int FindGreatestCommonFactor(FractionModel model)
 {
     return(FindGreatestCommonFactor(model.Denominator, model.Numerator));
 }
Exemplo n.º 8
0
        /// <summary>
        /// Return the number of places after the decimal point
        /// </summary>
        /// <param name="model">The FractionModel to check</param>
        /// <returns>The number of places after the decimal point</returns>
        public static int NumberOfDecimalPlaces(FractionModel model)
        {
            decimal number = decimal.Divide(model.Numerator, model.Denominator);

            return(NumberOfDecimalPlaces(number));
        }
Exemplo n.º 9
0
 /// <summary>
 /// Get the Reciprocal of a fraction
 /// </summary>
 /// <param name="model">The fraction to get the Reciprocal of</param>
 /// <returns>The reciprocal of the fraction</returns>
 public static FractionModel GetReciprocal(FractionModel model)
 {
     return(new FractionModel(model.Denominator, model.Numerator));
 }