示例#1
0
        static void Main(string[] args)
        {
            Console.WriteLine("CS4 John Gardiner \n");

            Rational x = new Rational();         //calls default ructor
            x.Numerator = 2;    //use properties to set values
            x.Denominator = 3;
            Console.WriteLine("Rational x;           //calls default constructor");
            Console.WriteLine("  x = " + x.Fraction + " \n");

            Rational y = new Rational(3,5);   //calls overloaded ructor
            Console.WriteLine("Rational y(3,5);      //calls overloaded constructor ");
            Console.WriteLine("  y = " + y.Fraction + "\n");

            Rational result = new Rational();
            Console.WriteLine("Testing arithmetic operator overloading:");

            result = y + x;
            Console.WriteLine("  " + y.Fraction + " + " + x.Fraction + " = " + result.Fraction);

            result = y * x;
            Console.WriteLine("  " + y.Fraction + " * " + x.Fraction + " = " + result.Fraction);

            result = y - x;
            Console.WriteLine("  " + y.Fraction + " - " + x.Fraction + " = " + result.Fraction);

            result = y / x;
            Console.WriteLine("  " + y.Fraction + " / " + x.Fraction + " = " + result.Fraction);

            Console.WriteLine("\nTesting relational operator overloading (9/15 < 10/15) ");

            Console.WriteLine("  " + y.Fraction + " == " + x.Fraction + " = " + (y == x));
            Console.WriteLine("  " + y.Fraction + " != " + x.Fraction + " = " + (y != x));

            Console.WriteLine("  " + y.Fraction + " <  " + x.Fraction + " = " + (y < x));
            Console.WriteLine("  " + x.Fraction + " <  " + y.Fraction + " = " + (x < y));

            Console.WriteLine("  " + y.Fraction + " >  " + x.Fraction + " = " + (y > x));
            Console.WriteLine("  " + x.Fraction + " >  " + y.Fraction + " = " + (x > y));
            Console.WriteLine(" ");

            Console.ReadLine();
            return;
        }
示例#2
0
        //override Equals and GetHashCode (provided)
        public override bool Equals(Object obj)
        {
            Rational right = (Rational)obj;  // cast the obj to Rational

            return(this.Numerator * right.Denominator == right.Numerator * this.Denominator);
        }