Hey everyone,
I am currently writing a small finger exercise in C# dealing with fractions. The class for the fractions is almost done, only the method of simplifying a fraction using Euclids algorithm for finding the GCD is kind of messy

| CODE |
public Fraction Simplify() { int divisor = this.GCD(); if (divisor != 0) { Fraction tmpFrac = new Fraction((this.Numerator / divisor), (this.Denominator / divisor)); return tmpFrac; } else { Fraction dummyFraction = new Fraction(1, 1); return dummyFraction; } }
|
| CODE |
public int GCD() { int tmpNumerator = this.Numerator; int tmpDenominator = this.Denominator;
if (tmpNumerator == 0 || tmpNumerator == tmpDenominator) { return tmpDenominator; } else { while ((tmpDenominator != 0) && (tmpNumerator != 0)) { if (tmpNumerator > tmpDenominator) { tmpNumerator = tmpNumerator % tmpDenominator; } else { tmpDenominator = tmpDenominator % tmpNumerator; } } } return tmpNumerator; }
|
and these are the members:
| CODE |
//members #region Members //private private int _numerator; private int _denominator; private string _absoluteValue;
//public public int Numerator { get { return this._numerator; } private set { this._numerator = value; } }
public int Denominator { get { return this._denominator; } private set { this._denominator = value; } }
public string AbsoluteValue { get { return this._absoluteValue; } private set { this._absoluteValue = value; } }
#endregion Members |
This method is based on an improved algorithm PatrickW posted in this forum a long time ago.
I don't really see any mistake in my code, nor do I recognise any mathematical issues :S
Help is very much appreciated.