コード例 #1
0
ファイル: Fractional.cs プロジェクト: balazsmolnar/Euler
 public static Fractional operator +(Fractional f1, Fractional f2)
 {
     var gcd = GCD(f1.Denominator, f2.Denominator);
     var denominator = f1.Denominator / gcd * f2.Denominator;
     var nominator = checked(f1.Nominator * (f2.Denominator / gcd) + f2.Nominator * (f1.Denominator / gcd));
     var result = new Fractional() { Nominator = nominator, Denominator = denominator };
     result.Simplify();
     return result;
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: balazsmolnar/Euler
 public static Fractional Calculate(params int[] coefficients)
 {
     Fractional frac = new Fractional() { Nominator = 1, Denominator = coefficients.Last() };
     for (int i = coefficients.Length - 2; i >= 0; i--)
     {
         frac = (frac + new Fractional() { Nominator = coefficients[i], Denominator = 1 }).Reciprocal();
         frac.Simplify();
     }
     return frac.Reciprocal();
 }
コード例 #3
0
ファイル: Fractional.cs プロジェクト: balazsmolnar/Euler
 public void TestAdd(int nominator1, int denominator1, int nominator2, int denominator2, int resultNominator, int resultDenominator)
 {
     var sut1 = new Fractional() { Nominator = (BigInt)nominator1, Denominator = (BigInt)denominator1 };
     var sut2 = new Fractional() { Nominator = (BigInt)nominator2, Denominator = (BigInt)denominator2 };
     var result = sut1 + sut2;
     Assert.That((int)result.Nominator, Is.EqualTo(resultNominator));
     Assert.That((int)result.Denominator, Is.EqualTo(resultDenominator));
 }
コード例 #4
0
ファイル: Fractional.cs プロジェクト: balazsmolnar/Euler
 public void TestSimplify(int nominator, int denominator, int resultNominator, int resultDenominator)
 {
     var sut = new Fractional() { Nominator = nominator, Denominator = denominator };
     sut.Simplify();
     Assert.That((int)sut.Nominator, Is.EqualTo(resultNominator));
     Assert.That((int)sut.Denominator, Is.EqualTo(resultDenominator));
 }