Пример #1
0
		/// <summary>
		/// add fractions
		/// </summary>
		/// <param name="f1">fraction</param>
		/// <param name="f2">fraction to be added</param>
		/// <returns>resulting fraction</returns>
		public static fraction operator + (fraction f1, fraction f2)
		{
			fraction frac = new fraction();
			if ((f1.numerator != 0) && (f2.numerator != 0))
			{
			    frac.numerator = (f1.numerator * f2.denominator) + (f2.numerator * f1.denominator);
			    frac.denominator = f2.denominator * f1.denominator;
			}
			else
			{
			    if (f1.numerator == 0)
			    {
			        frac.numerator = f2.numerator;
			        frac.denominator = f2.denominator;
			    }
			    else
			    {
			        frac.numerator = f1.numerator;
			        frac.denominator = f1.denominator;
			    }
			}
			if ((frac.denominator < 0) && (frac.numerator >= 0))
			{
			    frac.numerator = -frac.numerator;
			    frac.denominator = -frac.denominator;
			}
			return(frac);
		}
Пример #2
0
		public void Divide()
		{
		    fraction f1 = new fraction(4,5);
		    fraction f2 = new fraction(3,2);
		    fraction result = f1 / f2;
		    Assert.AreEqual(8, result.numerator);
		    Assert.AreEqual(15, result.denominator);
		}
Пример #3
0
		public void Multiply()
		{
		    fraction f1 = new fraction(4,5);
		    fraction f2 = new fraction(7,3);
		    fraction result = f1 * f2;
		    Assert.AreEqual(4*7, result.numerator);
		    Assert.AreEqual(5*3, result.denominator);
		}
Пример #4
0
		public void Add()
		{
		    fraction f1 = new fraction(4,5);
		    fraction f2 = new fraction(3,5);
		    fraction result = f1 + f2;
		    result.Reduce();
		    Assert.AreEqual(7, result.numerator);
		    Assert.AreEqual(5, result.denominator);
		}
Пример #5
0
		public void Subtract2()
		{
		    fraction f1 = new fraction(-2000,2000);
		    fraction f2 = new fraction(-49,45);
		    fraction result = f1 - f2;
		    result.Reduce();
		    Console.WriteLine("numerator " + result.numerator.ToString());
		    Console.WriteLine("denominator " + result.denominator.ToString());
		    Assert.AreEqual(4, result.numerator);
		    Assert.AreEqual(45, result.denominator);
		}
Пример #6
0
		public void SubtractFromZero2()
		{
		    fraction f1 = new fraction(0,2000);
		    fraction f2 = new fraction(7,86);
		    fraction result = f1 - f2;
		    result.Reduce();
		    Console.WriteLine("numerator " + result.numerator.ToString());
		    Console.WriteLine("denominator " + result.denominator.ToString());
		    Assert.AreEqual(-7, result.numerator);
		    Assert.AreEqual(86, result.denominator);
		}
Пример #7
0
		public void SubtractFromZero()
		{
		    fraction f1 = new fraction(3,109);
		    fraction f2 = new fraction(8,109);
		    fraction result = f1 - f2;
		    result.Reduce();
		    Console.WriteLine("numerator " + result.numerator.ToString());
		    Console.WriteLine("denominator " + result.denominator.ToString());
		    Assert.AreEqual(-5, result.numerator);
		    Assert.AreEqual(109, result.denominator);
		}
Пример #8
0
		/// <summary>
		/// divide fractions
		/// </summary>
		/// <param name="f1">fraction</param>
		/// <param name="f2">fraction to be divided by</param>
		/// <returns>resulting fraction</returns> 
		public static fraction operator /(fraction f1, fraction f2)
		{
			fraction frac = new fraction();
			frac.numerator = f1.numerator * f2.denominator;
			frac.denominator = f1.denominator * f2.numerator;
			if(frac.denominator < 0)
			{
			    frac.numerator *= -1;
			    frac.denominator *= -1;
			}
			if ((frac.denominator < 0) && (frac.numerator >= 0))
			{
			    frac.numerator = -frac.numerator;
			    frac.denominator = -frac.denominator;
			}
			return(frac);
		}
Пример #9
0
		public void AddToZero()
		{
		    fraction f1 = new fraction(0,2000);
		    fraction f2 = new fraction(-8,109);
		    fraction result = f1 + f2;
		    result.Reduce();
		    Console.WriteLine("numerator " + result.numerator.ToString());
		    Console.WriteLine("denominator " + result.denominator.ToString());
		    Assert.AreEqual(-8, result.numerator);
		    Assert.AreEqual(109, result.denominator);
		}