public void Divide_AnyThing()
        {
            // arrange
            Fraction expected1 = new Fraction(10, 1);
            Fraction expected2 = new Fraction(4, 3);
            Fraction expected3 = new Fraction(6, 5);
            Fraction expected4 = new Fraction(4, 15);
            Fraction expected5 = new Fraction(6, 15);
            Fraction expected6 = new Fraction(9, 8);

            // action
            Fraction actual1 = FractionOperations.Divide(new Fraction(2), new Fraction(1, 5));
            Fraction actual2 = FractionOperations.Divide(new Fraction(2), new Fraction(3, 2));
            Fraction actual3 = FractionOperations.Divide(new Fraction(2), new Fraction(1, 2, 3));
            Fraction actual4 = FractionOperations.Divide(new Fraction(2, 3), new Fraction(5, 2));
            Fraction actual5 = FractionOperations.Divide(new Fraction(2, 3), new Fraction(1, 2, 3));
            Fraction actual6 = FractionOperations.Divide(new Fraction(3, 2), new Fraction(1, 1, 3));

            // assert
            Assert.AreEqual(expected1, actual1, "Dividing whole number with proper fraction yielded an invalid result");
            Assert.AreEqual(expected2, actual2, "Dividing whole number with improper fraction yielded an invalid result");
            Assert.AreEqual(expected3, actual3, "Dividing whole number with mixed number fraction fractions yielded an invalid result");
            Assert.AreEqual(expected4, actual4, "Dividing proper with improper fractions yielded an invalid result");
            Assert.AreEqual(expected3, actual3, "Dividing proper with mixed number fractions yielded an invalid result");
            Assert.AreEqual(expected4, actual4, "Dividing improper with mixed number fractions yielded an invalid result");
        }
        public void Multiply_Negatives()
        {
            // arrange
            Fraction fraction1a = new Fraction(2);
            Fraction fraction1b = new Fraction(-3);
            Fraction fraction2a = new Fraction(3, 4);
            Fraction fraction2b = new Fraction(-2, 5);
            Fraction fraction3a = new Fraction(1, 1, 2);
            Fraction fraction3b = new Fraction(-1, 2, 3);

            Fraction expected1 = new Fraction(-6, 1);
            Fraction expected2 = new Fraction(-6, 20);
            Fraction expected3 = new Fraction(-15, 6);
            Fraction expected4 = new Fraction(4, 25);

            // action
            Fraction actual1 = FractionOperations.Multiply(fraction1a, fraction1b);
            Fraction actual2 = FractionOperations.Multiply(fraction2a, fraction2b);
            Fraction actual3 = FractionOperations.Multiply(fraction3a, fraction3b);
            Fraction actual4 = FractionOperations.Multiply(fraction2b, fraction2b);

            // assert
            Assert.AreEqual(expected1.WholePart, actual1.WholePart, "Invalid result when multiplying positive whole with negative whole.");
            Assert.AreEqual(expected2, actual2, "Invalid result when multiplying positive proper with negative proper.");
            Assert.AreEqual(expected3, actual3, "Invalid result when multiplying positive mixed number with negative mixed number");
            Assert.AreEqual(expected4, actual4, "Invalid result when multiplying two negative proper fractions.");
        }
        public void IsEquivalent_IsValidTrue()
        {
            // arrange
            Fraction fraction1a = new Fraction(3);
            Fraction fraction1b = new Fraction(6, 2);
            Fraction fraction2a = new Fraction(1, 4);
            Fraction fraction2b = new Fraction(3, 12);
            Fraction fraction3a = new Fraction(3, 2);
            Fraction fraction3b = new Fraction(6, 4);
            Fraction fraction4a = new Fraction(1, 1, 2);
            Fraction fraction4b = new Fraction(1, 3, 6);

            //Boolean expected = true;

            // action
            Boolean actual1 = FractionOperations.IsEquivalent(fraction1a, fraction1b);
            Boolean actual2 = FractionOperations.IsEquivalent(fraction2a, fraction2b);
            Boolean actual3 = FractionOperations.IsEquivalent(fraction3a, fraction3b);
            Boolean actual4 = FractionOperations.IsEquivalent(fraction4a, fraction4b);

            // assert
            Assert.IsTrue(actual1, "Result not equivalent for whole number and improper fraction.");
            Assert.IsTrue(actual2, "Result not equivalent for proper fractions.");
            Assert.IsTrue(actual3, "Result not equivalent for improper fractions.");
            Assert.IsTrue(actual4, "Result not equivalent for mixed number fractions.");
        }
Ejemplo n.º 4
0
        public static void Main()
        {
            var fraction1 = new Fraction(22, 7);
            var fraction2 = new Fraction(40, 4);

            Console.WriteLine("Fraction 1: {0}/{1} \r\nFraction 2: {2}/{3}\r\n--------------------\r\n",
                fraction1.Numerator, fraction1.Denominator, fraction2.Numerator, fraction2.Denominator);

            var oprations = new[] { "Add", "Subtract", "Multiply", "Divide", "Power" };
            int index = 0;

            var fractionOperations = new List<Fraction>
            {
                fraction1 + fraction2,
                fraction1 - fraction2,
                fraction1 * fraction2,
                fraction1 / fraction2,
                fraction1 ^ 2
            };

            foreach (var opetarion in fractionOperations)
            {
                Console.WriteLine("{0}\r\nFraction: {1} \r\nResult: {2}",
                    oprations[index++], opetarion, (double)opetarion + Environment.NewLine);
            }

            try
            {
                new Fraction(22, 0);
            }
            catch (ArithmeticException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        public void Subtract_MixedTypedPositives()
        {
            // arrange
            Fraction fraction1 = new Fraction(1);
            Fraction fraction2 = new Fraction(1, 2);
            Fraction fraction3 = new Fraction(3, 2);
            Fraction fraction4 = new Fraction(1, 2, 3);

            Fraction expected1 = new Fraction(1, 2);
            Fraction expected2 = new Fraction(1, 2);
            Fraction expected3 = new Fraction(2, 3);

            Fraction expected4 = new Fraction(2, 2);
            Fraction expected5 = new Fraction(7, 6);

            Fraction expected6 = new Fraction(1, 6);

            // aciton
            Fraction actual1 = FractionOperations.Subtract(fraction1, fraction2);
            Fraction actual2 = FractionOperations.Subtract(fraction3, fraction1);
            Fraction actual3 = FractionOperations.Subtract(fraction4, fraction1);

            Fraction actual4 = FractionOperations.Subtract(fraction3, fraction2);
            Fraction actual5 = FractionOperations.Subtract(fraction4, fraction2);
            Fraction actual6 = FractionOperations.Subtract(fraction4, fraction3);

            // assert
            Assert.AreEqual(expected1, actual1, "Invalid difference of a whole number and a proper fraction.");
            Assert.AreEqual(expected2, actual2, "Invalid difference of whole number and an improper fraction.");
            Assert.AreEqual(expected3, actual3, "Invalid difference of whole number and an mixed number fraction.");

            Assert.AreEqual(expected4, actual4, "Invalid difference of a proper fraction and  an improper fraction.");
            Assert.AreEqual(expected5, actual5, "Invalid difference of a proper fraction and a mixed number fraction.");
            Assert.AreEqual(expected5, actual5, "Invalid difference of an improper fraction a mixed number fraction.");
        }
        public void MakeImproper_IsValidResult()
        {
            // arrange
            Fraction fraction1 = new Fraction(2);
            Fraction fraction2 = new Fraction(3, 5);
            Fraction fraction3 = new Fraction(4, 3);
            Fraction fraction4 = new Fraction(1, 1, 2);

            Fraction expected1 = new Fraction(2, 1);
            Fraction expected2 = new Fraction(3, 5);
            Fraction expected3 = new Fraction(4, 3);
            Fraction expected4 = new Fraction(3, 2);

            // action
            Fraction actual1 = FractionOperations.MakeImproper(fraction1);
            Fraction actual2 = FractionOperations.MakeImproper(fraction2);
            Fraction actual3 = FractionOperations.MakeImproper(fraction3);
            Fraction actual4 = FractionOperations.MakeImproper(fraction4);

            // assert
            Assert.AreEqual(expected1, actual1, "Invalid Improper fraction result for whole number.");
            Assert.AreEqual(expected2, actual2, "Invalid Improper fraction result for proper fraction.");
            Assert.AreEqual(expected3, actual3, "Invalid Improper fraction result for improper fraction.");
            Assert.AreEqual(expected4, actual4, "Invalid Improper fraction result for mixed number.");
        }
        public void FindReciprocal_IsValidResultNegative()
        {
            // arrange
            Fraction fraction1 = new Fraction(-3);
            Fraction fraction2 = new Fraction(-1, 6);
            Fraction fraction3 = new Fraction(-4, 3);
            Fraction fraction4 = new Fraction(-1, 2, 5);

            Fraction expected1 = new Fraction(-1, 3);
            Fraction expected2 = new Fraction(-6, 1);
            Fraction expected3 = new Fraction(-3, 4);
            Fraction expected4 = new Fraction(-5, 7);

            // action
            Fraction actual1 = FractionOperations.FindReciprocal(fraction1);
            Fraction actual2 = FractionOperations.FindReciprocal(fraction2);
            Fraction actual3 = FractionOperations.FindReciprocal(fraction3);
            Fraction actual4 = FractionOperations.FindReciprocal(fraction4);

            // assert
            Assert.AreEqual(expected1, actual1, "Invalid reciprocal fraction for negative whole number.");
            Assert.AreEqual(expected2, actual2, "Invalid reciprocal fraction for negative proper fraction.");
            Assert.AreEqual(expected3, actual3, "Invalid reciprocal fraction for negative improper fraction.");
            Assert.AreEqual(expected4, actual4, "Invalid reciprocal fraction for negative mixed number.");
        }
        public void MakeImproper_IsValidForNegatives()
        {
            // arrange
            Fraction fraction1 = new Fraction(-2);
            Fraction fraction2 = new Fraction(-3, 5);
            Fraction fraction3 = new Fraction(-4, 3);
            Fraction fraction4 = new Fraction(-1, 1, 2);

            Fraction expected1 = new Fraction(-2, 1);
            Fraction expected2 = new Fraction(-3, 5);
            Fraction expected3 = new Fraction(-4, 3);
            Fraction expected4 = new Fraction(-3, 2);

            // action
            Fraction actual1 = FractionOperations.MakeImproper(fraction1);
            Fraction actual2 = FractionOperations.MakeImproper(fraction2);
            Fraction actual3 = FractionOperations.MakeImproper(fraction3);
            Fraction actual4 = FractionOperations.MakeImproper(fraction4);

            // assert
            Assert.AreEqual(expected1, actual1, "Invalid Improper fraction result for negative whole number.");
            Assert.AreEqual(expected2, actual2, "Invalid Improper fraction result for negative proper fraction.");
            Assert.AreEqual(expected3, actual3, "Invalid Improper fraction result for negative improper fraction.");
            Assert.AreEqual(expected4, actual4, "Invalid Improper fraction result for negative mixed number.");
        }
Ejemplo n.º 9
0
        protected void Page_Load(object sender, EventArgs e)
        {
            try {
            /*
                int num1 = 4, num2 = 0;
                strList = String.Format("Common Multiples of {0} and {1}: ", num1, num2);

                IEnumerable<int> factorList = Factors.Factors.ListCommonMultiples(num1,num2);

                foreach(int i in factorList){
                    strList += String.Format("{0}  ", i);
                }
            */
                Fraction fraction1 = new Fraction(3, 2);
                Fraction fraction2 = new Fraction(4, 2);

                Fraction total = FractionOperations.Add(fraction1, fraction2);
                lblResult.Text = String.Format("{0}/{1} ", fraction1.Numerator, fraction1.Denominator);
                lblResult.Text += String.Format("+ {0}/{1} ", fraction2.Numerator, fraction2.Denominator);

                lblResult.Text += String.Format("= {0}/{1} ", total.Numerator, total.Denominator);
                lblResult.Text += "<br />" ;
                IEnumerable someList = Factors.FindPrimeFactors(24);
                foreach (DictionaryEntry item in someList){
                    lblResult.Text +=  item.Key +"^" + item.Value + " ";
                }
                //lblResult.Text = String.Format("{0}/{1} + {2}/{3} = {4}/{5}", fraction1.Numerator.ToString(), fraction1.Denominator.ToString(), fraction2.Numerator.ToString(), fraction2.Denominator.ToString());

            } catch(ArgumentException ex) {
                lblResult.Text  = "Argument exception: " + ex.Message;
            } catch(Exception ex) {
                lblResult.Text = "General exception: " + ex.Message;
            }
        }
        public void FindReduced_IsValidNegative()
        {
            // arrange
            Fraction fraction1 = new Fraction(-3);
            Fraction fraction2 = new Fraction(-3, 6);
            Fraction fraction3 = new Fraction(-6, 3);
            Fraction fraction4 = new Fraction(-1, 3, 6);

            Fraction expected1 = new Fraction(-3, 1);
            Fraction expected2 = new Fraction(-1, 2);
            Fraction expected3 = new Fraction(-2, 1);
            Fraction expected4 = new Fraction(-3, 2);

            // action
            Fraction actual1 = FractionOperations.FindReduced(fraction1);
            Fraction actual2 = FractionOperations.FindReduced(fraction2);
            Fraction actual3 = FractionOperations.FindReduced(fraction3);
            Fraction actual4 = FractionOperations.FindReduced(fraction4);

            // assert
            Assert.AreEqual(expected1, actual1, "Invalid reduced fraction for negative whole number.");
            Assert.AreEqual(expected2, actual2, "Invalid reduced fraction for negative proper fraction.");
            Assert.AreEqual(expected3, actual3, "Invalid reduced fraction for negative improper fraction.");
            Assert.AreEqual(expected4, actual4, "Invalid reduced fraction for negative mixed number.");
        }
        public void ToString_IsValid()
        {
            // arrange
            Fraction fraction1 = new Fraction(2);
            Fraction fraction2 = new Fraction(-4);
            Fraction fraction3 = new Fraction(1, 2);
            Fraction fraction4 = new Fraction(-3, 2);
            Fraction fraction5= new Fraction(1, 2, 3);
            Fraction fraction6 = new Fraction(-2, 4, 5);

            string expected1 = "2";
            string expected2 = "-4";
            string expected3 = "1/2";
            string expected4 = "-3/2";
            string expected5 = "1 2/3";
            string expected6 = "-2 4/5";

            // action
            string actual1 = fraction1.ToString();
            string actual2 = fraction2.ToString();
            string actual3 = fraction3.ToString();
            string actual4 = fraction4.ToString();
            string actual5 = fraction5.ToString();
            string actual6 = fraction6.ToString();

            // assert
            Assert.AreEqual(expected1, actual1, "Invalid string for positive whole integer.");
            Assert.AreEqual(expected2, actual2, "Invalid string for negative whole integer.");
            Assert.AreEqual(expected3, actual3, "Invalid string for positive proper fraction.");
            Assert.AreEqual(expected4, actual4, "Invalid string for negative improper fraction.");
            Assert.AreEqual(expected5, actual5, "Invalid string for positive mixed number fraction.");
            Assert.AreEqual(expected6, actual6, "Invalid string for negative mixed number fraction.");
        }
        public void Compare_IsValidGreaterThanWithDiffferentTypes()
        {
            // arrange
            Fraction fraction1 = new Fraction(3);
            Fraction fraction2 = new Fraction(1, 2);
            Fraction fraction3 = new Fraction(5, 2);
            Fraction fraction4 = new Fraction(2, 1, 4);

            int expected = 1;

            // action
            int actual1 = FractionOperations.Compare(fraction1, fraction2);
            int actual2 = FractionOperations.Compare(fraction1, fraction3);
            int actual3 = FractionOperations.Compare(fraction1, fraction4);
            int actual4 = FractionOperations.Compare(fraction3, fraction2);
            int actual5 = FractionOperations.Compare(fraction4, fraction2);
            int actual6 = FractionOperations.Compare(fraction3, fraction4);

            // assert
            Assert.AreEqual(expected, actual1, "Invalid greater than comparison result for whole number and improper fraction.");
            Assert.AreEqual(expected, actual2, "Invalid greater than comparison result for whole number and proper fraction.");
            Assert.AreEqual(expected, actual3, "Invalid greater than comparison result for whole number and improper fraction.");
            Assert.AreEqual(expected, actual4, "Invalid greater than comparison result for proper and improper fractions.");
            Assert.AreEqual(expected, actual5, "Invalid greater than comparison result for proper and mixed number fractions.");
            Assert.AreEqual(expected, actual6, "Invalid greater than comparison result for improper and mixed number fractions.");
        }
Ejemplo n.º 13
0
 public static Fraction operator -(Fraction a, Fraction b)
 {
     Fraction x = new Fraction();
     x.Numerator = a.Numerator * b.Denominator - b.Numerator * a.Denominator;
     x.Denominator = a.Denominator * b.Denominator;
     return x;
 }
Ejemplo n.º 14
0
        public void Fraction_NegativeArgument_3()
        {
            // arrange
            Fraction fraction1 = new Fraction(1, -2, 5);
            Fraction fraction2 = new Fraction(1, 2, -5);
            Fraction fraction3 = new Fraction(1, -2, -5);
            Fraction fraction4 = new Fraction(-1, -2, 5);
            Fraction fraction5 = new Fraction(-1, 2, -5);
            Fraction fraction6 = new Fraction(-1, -2, -5);

            // assert
            Assert.AreEqual(-1, fraction1.WholePart, "Invalid whole part for three parameters constructor with a positive whole part, negative numerator.");
            Assert.AreEqual(2, fraction1.Numerator, "Invalid numerator for three parameters constructor with apositive whole part,  negative numerator.");
            Assert.AreEqual(5, fraction1.Denominator, "Invalid denominator for three parameters constructor with a positive whole part,  negative numerator.");

            Assert.AreEqual(-1, fraction2.WholePart, "Invalid whole part for three parameters constructor with a positive whole part,  negative denominator.");
            Assert.AreEqual(2, fraction2.Numerator, "Invalid numerator for three parameters constructor with a positive whole part, negative denominator.");
            Assert.AreEqual(5, fraction2.Denominator, "Invalid denominator for three parameters constructor with a positive whole part, negative denominator.");

            Assert.AreEqual(1, fraction3.WholePart, "Invalid whole part for three parameters constructor with a positive whole part,  negative numerator and denominator.");
            Assert.AreEqual(2, fraction3.Numerator, "Invalid numerator for three parameters constructor with a negative numerator and denominator.");
            Assert.AreEqual(5, fraction3.Denominator, "Invalid denominator three two parameters constructor with a positive whole part,  negative numerator and denominator.");

            Assert.AreEqual(1, fraction4.WholePart, "Invalid whole part for three parameters constructor with a negative whole part, negative numerator.");
            Assert.AreEqual(2, fraction4.Numerator, "Invalid numerator for three parameters constructor with a negative whole part,  negative numerator.");
            Assert.AreEqual(5, fraction4.Denominator, "Invalid denominator for three parameters constructor with a negative whole part,  negative numerator.");

            Assert.AreEqual(1, fraction5.WholePart, "Invalid whole part for three parameters constructor with a negative whole part,  negative denominator.");
            Assert.AreEqual(2, fraction5.Numerator, "Invalid numerator for three parameters constructor with a positive whole part, negative denominator.");
            Assert.AreEqual(5, fraction5.Denominator, "Invalid denominator for three parameters constructor with a positive whole part, negative denominator.");

            Assert.AreEqual(-1, fraction6.WholePart, "Invalid whole part for three parameters constructor with a negative whole part,  negative numerator and denominator.");
            Assert.AreEqual(2, fraction6.Numerator, "Invalid numerator for three parameters constructor with a negative numerator and denominator.");
            Assert.AreEqual(5, fraction6.Denominator, "Invalid denominator three two parameters constructor with a positive whole part,  negative numerator and denominator.");
        }
        public void Compare_IsValidEqualWithSameType()
        {
            // arrange
            Fraction fraction1a = new Fraction(2);
            Fraction fraction1b = new Fraction(2);
            Fraction fraction2a = new Fraction(1, 2);
            Fraction fraction2b = new Fraction(1, 2);
            Fraction fraction3a = new Fraction(3, 2);
            Fraction fraction3b = new Fraction(3, 2);
            Fraction fraction4a = new Fraction(1, 1, 8);
            Fraction fraction4b = new Fraction(1, 1, 8);

            int expected = 0;

            // action
            int actual1 = FractionOperations.Compare(fraction1a, fraction1b);
            int actual2 = FractionOperations.Compare(fraction2a, fraction2b);
            int actual3 = FractionOperations.Compare(fraction3a, fraction3b);
            int actual4 = FractionOperations.Compare(fraction4a, fraction4b);

            // assert
            Assert.AreEqual(expected, actual1, "Invalid equal comparison result for two whole numbers.");
            Assert.AreEqual(expected, actual2, "Invalid equal comparison result for two proper fractions.");
            Assert.AreEqual(expected, actual3, "Invalid equal comparison result for two improper fractions.");
            Assert.AreEqual(expected, actual4, "Invalid equal comparison result for two mixed number fractions.");
        }
        public void IsUnit_IsValidResultNegative()
        {
            // arrange
            Fraction fraction1 = new Fraction(-3);
            Fraction fraction2 = new Fraction(-3, 4);
            Fraction fraction3 = new Fraction(-3, 2, 5);
            Fraction fraction4 = new Fraction(-1, 4);
            Fraction fraction5 = new Fraction(-2, 1, 4);

            Boolean expected1 = false;
            Boolean expected2 = false;
            Boolean expected3 = false;
            Boolean expected4 = true;
            Boolean expected5 = false;

            // action
            Boolean actual1 = fraction1.IsUnit();
            Boolean actual2 = fraction2.IsUnit();
            Boolean actual3 = fraction3.IsUnit();
            Boolean actual4 = fraction4.IsUnit();
            Boolean actual5 = fraction5.IsUnit();

            // assert
            Assert.IsNotNull(actual1, "Invalid result with one argument, result is null.");
            Assert.AreEqual(expected1, actual1, "Invalid result with one argument, numerator is not one; negative whole number.");

            Assert.AreEqual(expected2, actual2, "Invalid result with two argument, numerator is not one; negative proper fraction");
            Assert.AreEqual(expected3, actual3, "Invalid result with three arguments, numerator is not one; negative mixed number.");
            Assert.AreEqual(expected4, actual4, "Invalid result with two arguments, numerator is one; negative proper fraction.");
            Assert.AreEqual(expected5, actual5, "Invalid result with three arguments, numerator is one; negative mixed number.");
        }
        public void Multiply_IsValidResultOfAnyType()
        {
            // arrange
            Fraction expected1 = new Fraction(2, 5);
            Fraction expected2 = new Fraction(6, 2);
            Fraction expected3 = new Fraction(10, 3);
            Fraction expected4 = new Fraction(10, 6);
            Fraction expected5 = new Fraction(10, 9);
            Fraction expected6 = new Fraction(12,6);

            // action
            Fraction actual1 = FractionOperations.Multiply(new Fraction(2), new Fraction(1, 5));
            Fraction actual2 = FractionOperations.Multiply(new Fraction(2), new Fraction(3, 2));
            Fraction actual3 = FractionOperations.Multiply(new Fraction(2), new Fraction(1, 2, 3));
            Fraction actual4 = FractionOperations.Multiply(new Fraction(2, 3), new Fraction(5, 2));
            Fraction actual5 = FractionOperations.Multiply(new Fraction(2, 3), new Fraction(1, 2, 3));
            Fraction actual6 = FractionOperations.Multiply(new Fraction(3, 2), new Fraction(1, 1, 3));

            // assert
            Assert.AreEqual(expected1, actual1, "Multiplying whole number with proper fraction yielded an invalid result");
            Assert.AreEqual(expected2, actual2, "Multiplying whole number with improper fraction yielded an invalid result");
            Assert.AreEqual(expected3, actual3, "Multiplying whole number with mixed number fraction fractions yielded an invalid result");
            Assert.AreEqual(expected4, actual4, "Multiplying proper with improper fractions yielded an invalid result");
            Assert.AreEqual(expected3, actual3, "Multiplying proper with mixed number fractions yielded an invalid result");
            Assert.AreEqual(expected4, actual4, "Multiplying improper with mixed number fractions yielded an invalid result");
        }
        public void MakeDecimal_IsValidResult()
        {
            // arrange
            Fraction fraction1 = new Fraction(3);
            Fraction fraction2 = new Fraction(1, 5);
            Fraction fraction3 = new Fraction(3, 2);
            Fraction fraction4 = new Fraction(1, 1, 2);

            decimal expected1 = 3.00M;
            decimal expected2 = 0.20M;
            decimal expected3 = 1.5M;
            decimal expected4 = 1.5M;
            double delta = 0.1;

            // action
            decimal actual1 = FractionOperations.MakeDecimal(fraction1);
            decimal actual2 = FractionOperations.MakeDecimal(fraction2);
            decimal actual3 = FractionOperations.MakeDecimal(fraction3);
            decimal actual4 = FractionOperations.MakeDecimal(fraction4);

            // assert
            Assert.AreEqual(Convert.ToDouble(expected1), Convert.ToDouble(actual1), delta, "Invalid whole to decimal conversion.");
            Assert.AreEqual(Convert.ToDouble(expected2), Convert.ToDouble(actual2), delta, "Invalid proper fraction to decimal conversion.");
            Assert.AreEqual(Convert.ToDouble(expected3), Convert.ToDouble(actual3), delta, "Invalid improper fraction to decimal conversion.");
            Assert.AreEqual(Convert.ToDouble(expected4), Convert.ToDouble(actual4), delta, "Mixed number result is invalid");
        }
Ejemplo n.º 19
0
 public static void Main()
 {
     Fraction a = new Fraction(6, 18);
     Fraction b = new Fraction(7, 8);
     Console.WriteLine(a + b);
     Console.WriteLine((a + b).Simplify);
 }
        public void Add_MixedTypedPositives()
        {
            // arrange
            Fraction fraction1 = new Fraction(1);
            Fraction fraction2 = new Fraction(1, 2);
            Fraction fraction3 = new Fraction(3, 2);
            Fraction fraction4 = new Fraction(1, 2, 3);

            Fraction expected1 = new Fraction(3, 2);
            Fraction expected2 = new Fraction(5, 2);
            Fraction expected3 = new Fraction(8, 3);

            Fraction expected4 = new Fraction(4, 2);
            Fraction expected5 = new Fraction(13, 6);

            Fraction expected6 = new Fraction(19, 6);

            // aciton
            Fraction actual1 = FractionOperations.Add(fraction1, fraction2);
            Fraction actual2 = FractionOperations.Add(fraction1, fraction3);
            Fraction actual3 = FractionOperations.Add(fraction1, fraction4);

            Fraction actual4 = FractionOperations.Add(fraction2, fraction3);
            Fraction actual5 = FractionOperations.Add(fraction2, fraction4);
            Fraction actual6 = FractionOperations.Add(fraction3, fraction4);

            // assert
            Assert.AreEqual(expected1, actual1, "Invalid sum of a whole number and a proper fraction.");
            Assert.AreEqual(expected2, actual2, "Invalid sum of whole number and an improper fraction.");
            Assert.AreEqual(expected3, actual3, "Invalid sum of whole number and an mixed number fraction.");

            Assert.AreEqual(expected4, actual4, "Invalid sum of a proper fraction and  an improper fraction.");
            Assert.AreEqual(expected5, actual5, "Invalid sum of a proper fraction and a mixed number fraction.");
            Assert.AreEqual(expected5, actual5, "Invalid sum of an improper fraction a mixed number fraction.");
        }
Ejemplo n.º 21
0
 static void Main(string[] args)
 {
     Fraction fr1 = new Fraction(2 , 3);
     Fraction fr2 = new Fraction(1, 3);
     Fraction result = fr1 + fr2;
     Console.WriteLine(result);
     Console.WriteLine(fr2-fr1);
 }
Ejemplo n.º 22
0
        static void Main()
        {
            Fraction testOne = new Fraction(22, 7);
            Fraction testTwo = new Fraction(40, 4);
            Fraction testThr = testOne + testTwo;
            Console.WriteLine(testThr);

        }
Ejemplo n.º 23
0
        public static Fraction operator -(Fraction a, Fraction b)
        {
            long numerator = a.Numerator * b.Denominator - b.Numerator * a.Denominator;
            long denominator = a.Denominator * b.Denominator;

            var result = new Fraction(numerator, denominator);
            return result;
        }
 static void Main()
 {
     Fraction f1 = new Fraction(22, 7);
     Fraction f2 = new Fraction(40, 4);
     Fraction result = f1 + f2;
     Console.WriteLine(result.Numerator);
     Console.WriteLine(result.Denominator);
     Console.WriteLine(result);
 }
Ejemplo n.º 25
0
 static void Main(string[] args)
 {
     Fraction fraction1 = new Fraction(22, 7);
     Fraction fraction2 = new Fraction(40, 4);
     Fraction result = fraction1 + fraction2;
     Console.WriteLine(result.Numerator);
     Console.WriteLine(result.Denominator);
     Console.WriteLine(result);
 }
Ejemplo n.º 26
0
 public static Fraction operator +(Fraction one, Fraction two)
 {
     BigInteger CommonDenominator=one.denominator*two.denominator;
     BigInteger devisorOne=CommonDenominator/one.denominator;
     BigInteger devisorTwo=CommonDenominator/two.denominator;
     BigInteger MainNumerator=(devisorOne*one.numerator)+(devisorTwo*two.numerator);
     
     Fraction CalculatedFraction=new Fraction(MainNumerator,CommonDenominator);
     return CalculatedFraction;
 }
        public void FindReciprocal_DivideByZero()
        {
            // assign
            Fraction fraction = new Fraction(0);

            // action
            Fraction actual = FractionOperations.FindReciprocal(fraction);

            Assert.IsNotNull(actual, "Actual fraction object was null.");
        }
        public void Divide_ByZero()
        {
            // assign
            Fraction fraction = new Fraction(0);

            // action
            Fraction actual = FractionOperations.Divide(new Fraction(1,2), fraction);

            Assert.IsNotNull(actual, "Actual fraction object was null.");
        }
Ejemplo n.º 29
0
        public Fraction(long num, long den)
            : this()
        {
            CheckDenominatorZero(den);

            CheckMinValue(num);
            CheckMinValue(den);

            var f = new Fraction((decimal)num, den);
            this.Initialize(f.numerator, f.denominator);
        }
Ejemplo n.º 30
0
        public void Fraction_NegativeArguments_1()
        {
            int whole = -5;

            // arrange
            Fraction fraction = new Fraction(whole);

            // assert
            Assert.AreEqual(-5, fraction.WholePart, "Invalid whole part for single parameter constructor with a negative argument.");
            Assert.AreEqual(0, fraction.Numerator, "Invalid numerator for single parameter constructor with a negative argument.");
            Assert.AreEqual(0, fraction.Denominator, "Invalid denominator for single parameter constructor with a negative argument.");
        }