Exemplo n.º 1
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            Rational firstRational = this;

            if (!(obj is Rational))
            {
                return(false);
            }
            Rational secondRational = (Rational)obj;

            firstRational.Reduce();
            secondRational.Reduce();
            if (firstRational.Numerator == secondRational.Numerator &&
                firstRational.Denominator == secondRational.Denominator)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///  multiplies Rational objects
        /// </summary>
        /// <param name="r1">first instace for the multiplieng</param>
        /// <param name="r2">second instace for the multiplieng</param>
        /// <returns> new Rational instance which his value is the inputs multiply </returns>
        public Rational Mul(ref Rational r1, ref Rational r2)
        {
            Rational rMul = new Rational(r1.Numerator * r2.Numerator, r1.Denominator * r2.Denominator);

            rMul.Reduce(ref rMul);
            return(rMul);
        }
Exemplo n.º 3
0
        //This isn't object.Equals
        public bool Equals(Rational num)
        {
            this.Reduce();
            num.Reduce();

            return(this.Numerator == num.Numerator && this.Denominator == num.Denominator ? true : false);
        }
Exemplo n.º 4
0
        public Rational Mul(Rational firstNumber)
        {
            Rational newRational = new Rational(firstNumber.Numerator * this.Numerator, firstNumber.Denominator * this.Denominator);

            newRational.Reduce();
            return(newRational);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            //// Lab 3 test:
            Rational number  = new Rational(4, 5);
            Rational number2 = new Rational(16, 20);
            Rational number3 = new Rational(5);

            Console.WriteLine("numbe1: {0}, number2: {1} , number3: {2}", number.ToString(), number2.ToString(), number3.ToString());
            number2.Reduce();
            Console.WriteLine("reduced number2: " + number2.ToString());
            number.Mul(number);
            Console.WriteLine("number1 * number2: " + number.Mul(number).ToString());
            Console.WriteLine("Is equal? " + number.Equals(number2));
            Console.WriteLine("Is number equals to 7? " + number.Equals(7));
            Console.WriteLine("number1 + number2: " + number.Add(number2).ToString());

            //Lab appendix test:
            Console.WriteLine("operator + : number+number2= " + (number + number2).ToString());
            Console.WriteLine("operator * : number*number2= " + (number * number2).ToString());
            Console.WriteLine("operator / : number/number2= " + (number / number2).ToString());
            Console.WriteLine("operator - : number-number2= " + (number - number2).ToString());
            Console.WriteLine("(double) casting : (double)(number2)= " + ((double)(number2)).ToString());
            Console.WriteLine("casting : (Rational)number3= " + ((Rational)(number3)).ToString());

            Console.Read();
        }/*********************/
Exemplo n.º 6
0
        public static void Main()//testing the rational struct
        {
            Rational rat1 = new Rational(3, 4);
            Rational rat2 = new Rational(5, 6);
            Rational rat3 = new Rational(6, 7);
            Rational rat4 = new Rational(5, 6);
            Rational rat5 = new Rational(5);

            Console.WriteLine(rat1.ToString());
            Console.WriteLine(rat2.ToString());
            Console.WriteLine(rat3.ToString());
            Console.WriteLine(rat5.ToString());
            Console.WriteLine("{0},{1},{2},{3}\n", rat1.GetAsDouble, rat2.GetAsDouble, rat3.GetAsDouble, rat5.GetAsDouble);
            Rational newRat = rat1.Add(rat2);

            Console.WriteLine(newRat.ToString());
            newRat.Reduce();
            Console.WriteLine("rational number after reduction {0}", newRat.ToString());
            Rational newRat2 = rat2.Mul(rat3);

            Console.WriteLine(newRat2.ToString());
            newRat2.Reduce();
            Console.WriteLine("rational number after reduction {0}", newRat2.ToString());

            if (rat2.Equals(rat4))
            {
                Console.WriteLine("the numbers are equal");
            }
            else
            {
                Console.WriteLine("not equal");
            }
        }
Exemplo n.º 7
0
        /// <summary>
        ///  adds two Rational objects
        /// </summary>
        /// <param name="r1">first instace for the adding</param>
        /// <param name="r2">second instace for the adding</param>
        /// <returns>new Rational instance which his value is the inputs sum</returns>
        public Rational Add(ref Rational r1, ref Rational r2)
        {
            Rational rSum = new Rational(r1.Numerator * r2.Denominator + r2.Numerator * r1.Denominator, r1.Denominator * r2.Denominator);

            rSum.Reduce(ref rSum);
            return(rSum);
        }
        public Rational Multiply(Rational rational)
        {
            int      newNumerator   = Numerator * rational.Numerator;
            int      newDenominator = Denominator * rational.Denominator;
            Rational result         = new Rational(newNumerator, newDenominator);

            result.Reduce();
            return(result);
        }
        public Rational Substract(Rational rational)
        {
            int      newNumerator   = Numerator * rational.Denominator - rational.Numerator * Denominator;
            int      newDenominator = Denominator * rational.Denominator;
            Rational result         = new Rational(newNumerator, newDenominator);

            result.Reduce();
            return(result);
        }
Exemplo n.º 10
0
        public Rational Mul(Rational num2)
        {
            int      num = this.Numerator * num2.Numerator;
            int      den = this.Denominator * num2.Denominator;
            Rational res = new Rational(num, den);

            res.Reduce();
            return(res);
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            Rational first = new Rational(6, 12);
            Rational sec   = new Rational(12, 6);
            Rational third = new Rational(5);

            Console.WriteLine("{0} object {1}", "first", first);
            Console.WriteLine("{0} object {1}", "sec", sec);
            Console.WriteLine("{0} object {1}", "third", third);

            Console.WriteLine("reducing all objects");
            first.Reduce();
            sec.Reduce();
            third.Reduce();

            Console.WriteLine("Adding...");
            Console.WriteLine("Add first object and second object...");
            Console.WriteLine("Result is {0}", first.Add(sec));
            Console.WriteLine("Add second object and third object...");
            Console.WriteLine("Result is {0}", sec.Add(third));

            Console.WriteLine("Multiplying...");
            Console.WriteLine("Multiplying first object and second object...");
            Console.WriteLine("Result is {0}", first.Mul(sec));
            Console.WriteLine("Multiplying second object and third object...");
            Console.WriteLine("Result is {0}", sec.Mul(third));

            if (new Rational(5, 5).Equals(new Rational(5, 5)) && !new Rational(5, 5).Equals(new Rational(5, 3)))
            {
                Console.WriteLine("Equals working perfect...");
            }
            try
            {
                Rational rationalWithException = new Rational(5, 0);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine("Not valid Rational was caught by the test...");
            }

            Console.WriteLine("---------------------------- lab 10 Appendix: ----------------------------");
            Rational test = new Rational(2, 5);

            Console.WriteLine($"multiply test object {test} with itself equals to {test*test}");
            Console.WriteLine($"Dividing test object {test} with itself equals to {test / test}");
            Console.WriteLine($"Adding test object {test} with itself equals to {test + test}");
            Console.WriteLine($"Subtraction test object {test} with Rational of 1 equals to {test - new Rational(1)}");

            Console.WriteLine($"Cast int 10 to Rational object {(Rational) 10}");
            Console.WriteLine($"Cast test object {test} to double {(double) test}");

            Console.ReadKey();
        }
Exemplo n.º 12
0
        //override
        public override bool Equals(object obj)
        {
            if (obj.GetType() != this.GetType())
            {
                return(false);
            }
            Rational other = (Rational)obj;
            Rational temp1 = new Rational(Numerator, Denominator);
            Rational temp2 = new Rational(other.Numerator, other.Denominator);

            temp1.Reduce(); temp2.Reduce();
            return(temp1.Numerator == temp2.Numerator && temp1.Denominator == temp2.Denominator);
        }
        public Rational Divide(Rational rational)
        {
            if (rational.Numerator == 0)
            {
                throw new DivideByZeroException();
            }
            int      newNumerator   = Numerator * rational.Denominator;
            int      newDenominator = Denominator * rational.Numerator;
            Rational result         = new Rational(newNumerator, newDenominator);

            result.Reduce();
            return(result);
        }
Exemplo n.º 14
0
 public override bool Equals(object other)//overriding Equals
 {
     if (other is Rational)
     {
         Rational newRational = (Rational)other;
         this.Reduce();
         newRational.Reduce();
         return(this.Denominator == newRational.GetDenumenator && this.Numerator == newRational.GetNumerator);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 15
0
        public Rational Add(Rational rt)
        {
            // Adding two rational object
            this.Reduce();
            rt.Reduce();
            if (rt.denominator == this.denominator)
            {
                return(new Rational(this.numerator + rt.numerator, this.denominator));
            }

            int numerator   = this.numerator * rt.denominator + rt.numerator * this.denominator;
            int denominator = this.denominator * rt.denominator;

            return(new Rational(numerator, denominator));
        }
Exemplo n.º 16
0
        static void Main(string[] args)
        {
            Rational r1 = new Rational(7, 8);
            Rational r2 = new Rational(5, 10);

            // add and mult
            Rational r3 = Rational.Add(r1, r2);
            Rational r4 = Rational.Mul(r1, r2);

            Console.WriteLine(r1 + " + " + r2 + " = " + r3);
            Console.WriteLine(r1 + " * " + r2 + " = " + r4);

            // reduce
            Console.Write(r1 + " = ");
            r1.Reduce();
            Console.WriteLine(r1);
            Console.Write(r2 + " = ");
            r2.Reduce();
            Console.WriteLine(r2);
            Console.Write(r3 + " = ");
            r3.Reduce();
            Console.WriteLine(r3);
            Console.Write(r4 + " = ");
            r4.Reduce();
            Console.WriteLine(r4);

            // equlas
            if (r3.Equals(r4))
            {
                Console.WriteLine(r3 + "=" + r4);
            }
            else
            {
                Console.WriteLine(r3 + "!=" + r4);
            }
            r1 = new Rational(7, 8);
            r2 = new Rational(5, 10);
            if (Rational.Mul(r1, r2).Equals(r4))
            {
                Console.WriteLine(Rational.Mul(r1, r2) + " = " + r4);
            }
            else
            {
                Console.WriteLine(Rational.Mul(r1, r2) + " != " + r4);
            }
        }
Exemplo n.º 17
0
        static void Main(string[] args)
        {
            Rational num1 = new Rational(1, 2);
            Rational num2 = new Rational(1, 2);

            Rational num3 = num1.Add(num2);

            Rational num4 = num2.Mul(num2);

            Rational num6 = new Rational(2, 4);
            Rational num7 = new Rational(2, 4);

            num7.Reduce();


            Console.WriteLine($"{num1} + {num2} = {num3}");
            Console.WriteLine($"{num2} * {num2} = {num4}");
            Console.WriteLine($"{num6} reduced {num7}");
        }
Exemplo n.º 18
0
        internal static void Main()
        {
            var num1 = new Rational(1, 2);
            var num2 = new Rational(1, 2);

            var num3 = num1.Add(num2);

            var num4 = num2.Mul(num2);

            var num6 = new Rational(2, 4);
            var num7 = new Rational(2, 4);

            num7.Reduce();


            Console.WriteLine($"{num1} + {num2} = {num3}");
            Console.WriteLine($"{num2} * {num2} = {num4}");
            Console.WriteLine($"{num6} reduced {num7}");
        }
Exemplo n.º 19
0
        static void Main(string[] args)
        {
            Rational r1 = new Rational(48, 64);
            Rational r2 = new Rational(72, 48);

            r1.Reduce();
            Rational r3 = r1 + r2;
            Rational r4 = new Rational(6, 8);
            Rational r5 = r4 - r1;
            Rational r6 = r4 / r1;
            Rational r7 = (Rational)8;
            double   d1 = (double)r1;

            Console.WriteLine(r1.ToString());
            Console.WriteLine(r3.ToString());
            Console.WriteLine(r5.ToString());
            Console.WriteLine(r6.ToString());
            Console.WriteLine(r7.ToString());
            Console.WriteLine(d1);
            Console.WriteLine(r4.Equals(r1).ToString());
        }
Exemplo n.º 20
0
        static void Main(string[] args)
        {
            try
            {
                Rational rationalA = new Rational(4, 8);
                Rational rationalB = new Rational(1, 3);

                Console.WriteLine(" rationalA Value : {0}", rationalA);
                Console.WriteLine(" rationalB Value : {0}", rationalB);

                Console.WriteLine(" The sum of rationalA and rationalB is : {0}", rationalA + rationalB);
                Console.WriteLine(" The result of multiplying rationalA and rationalB is : {0}", rationalA * rationalB);
                Console.WriteLine(" The result after subtract rationalB from rationalA is : {0}", rationalA - rationalB);
                Console.WriteLine(" The result after Divide rationalA by rationalB is : {0}", rationalA / rationalB);

                rationalA.Reduce();
                Console.WriteLine(" rationalA Value after reduce : {0}", rationalA);
                Console.WriteLine(" Is rationalA and rationalB are Equals: {0}", rationalA.Equals(rationalB));
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
        }