public void BasicComparisonTests() { Rational r1 = new Rational(-3, 6); Rational r2 = new Rational(2, 4); Rational r3 = new Rational(1, 2); Assert.IsTrue(r1.CompareTo(r2) < 0); Assert.IsTrue(r2.CompareTo(r1) > 0); Assert.IsTrue(r2.CompareTo(r3) == 0); }
public void ConversionTests() { Rational r1 = new Rational(3, 6); Rational r2 = new Rational(-3, 6); Rational r3 = new Rational(10, -2); Assert.IsTrue(r1.ToDecimal() == 0.5m); Assert.IsTrue(r2.ToDecimal() == -0.5m); Assert.IsTrue(r1.ToDouble() == 0.5); //.5 is stored exactly Assert.IsTrue(r2.ToDouble() == -0.5); Assert.IsTrue(r2.ToString() == "-1/2"); Assert.IsTrue("" + r3 == "-5"); //implicit use of ToString }
public void ConstructorTest() { Rational r = new Rational(3, 5); Assert.IsTrue(r.GetNumerator() == 3); Assert.IsTrue(r.GetDenominator() == 5); r = new Rational(3, -5); Assert.IsTrue(r.GetNumerator() == -3); Assert.IsTrue(r.GetDenominator() == 5); r = new Rational(6, 10); Assert.IsTrue(r.GetNumerator() == 3); Assert.IsTrue(r.GetDenominator() == 5); r = new Rational(125, 1); Assert.IsTrue(r.GetNumerator() == 125); Assert.IsTrue(r.GetDenominator() == 1); }
public static void Main() { Rational f = new Rational(6, -10); Console.WriteLine("6/(-10) simplifies to {0}", f); Console.WriteLine("reciprocal of {0} is {1}", f, f.Reciprocal()); Console.WriteLine("{0} negated is {1}", f, f.Negate()); Rational h = new Rational(1,2); Console.WriteLine("{0} + {1} is {2}", f, h, f.Add(h)); Console.WriteLine("{0} - {1} is {2}", f, h, f.Subtract(h)); Console.WriteLine("{0} * {1} is {2}", f, h, f.Multiply(h)); Console.WriteLine("({0}) / ({1}) is {2}", f, h, f.Divide(h)); Console.WriteLine("{0} > {1} ? {2}", h, f, (h.CompareTo(f) > 0)); Console.WriteLine("{0} as a double is {1}", f, f.ToDouble()); Console.WriteLine("{0} as a decimal is {1}", h, h.ToDecimal()); ShowParse("-12/30"); // see helping function below ShowParse("123"); ShowParse("1.125"); }
public void BasicArithmeticTests() { Rational r, r1, r2; r1 = new Rational(47, 64); r2 = new Rational(-11, 64); r = r1.Add(r2); Assert.IsTrue(r.CompareTo(new Rational(36, 64)) == 0); r = r1.Subtract(r2); Assert.IsTrue(r.CompareTo(new Rational(58, 64)) == 0); r = r1.Multiply(r2); Assert.IsTrue(r.CompareTo(new Rational(47 * -11, 64 * 64)) == 0); r = r1.Divide(r2); Assert.IsTrue(r.CompareTo(new Rational(47, -11)) == 0); r = r1.Reciprocal(); Assert.IsTrue(r.CompareTo(new Rational(64, 47)) == 0); r = r1.Negate(); Assert.IsTrue(r.CompareTo(new Rational(-47, 64)) == 0); }
public Rational Length() { Rational sum = new Rational(0, 1); foreach (Note n in voice) { sum = sum.Add(n.GetDuration()); } return sum; }
public Measure(Rational duration) { this.voice = new List<Note>(); this.duration = duration; }
public Score(Rational timeSignature, Scale keySignature, string[] staffLabels) { this.timeSignature = timeSignature; this.keySignature = keySignature; staff = new Dictionary<string, List<Measure>>(); foreach (string label in staffLabels) { staff[label] = new List<Measure>(); } }
public Note(int tone, int octave, Rational duration) { this.tone = tone; this.octave = octave; this.duration = duration; }
// Multiply chunk /// Return a new Rational which is the product of this Rational and f. public Rational Multiply(Rational f) { // end Multiply heading chunk return new Rational(num*f.num, denom*f.denom); }
// Divide chunk /// Return a new Rational which is the quotient of this Rational and f. public Rational Divide(Rational f) { return new Rational(num*f.denom, denom*f.num); }
/// Return a number that is positive, zero or negative, respectively, if /// the value of this Rational is bigger than f, /// the values of this Rational and f are equal or /// the value of this Rational is smaller than f. public int CompareTo(Rational f) { return num*f.denom - denom*f.num; //numerator of this - f }
// Add chunk /// Return a new Rational which is the sum of this Rational and f. public Rational Add(Rational f) { return new Rational(num*f.denom + denom*f.num, denom*f.denom); }
/// Return a new Rational which is the difference of this Rational and f. public Rational Subtract(Rational f) { return new Rational(num*f.denom - denom*f.num, denom*f.denom); }
// still useful as seen later in Interfaces for IComparable interface: /// Return a number that is positive, zero or negative, respectively, if /// the value of this Rational is bigger than f, /// the values of this Rational and f are equal or /// the value of this Rational is smaller than f. public int CompareTo(Rational f) { return num*f.denom - denom*f.num; }