Exemple #1
0
        public Rational Multiply(Rational x)
        {
            Rational mul = new Rational();

            mul.Numerator   = this.Numerator * x.Numerator;
            mul.Denominator = this.Denominator + x.Denominator;
            mul.Even();

            return(mul);
        }
Exemple #2
0
        public Rational Add(Rational c)
        {
            Rational add = new Rational();

            add.Numerator   = this.Denominator * c.Numerator + c.Denominator * this.Numerator;
            add.Denominator = this.Denominator * c.Denominator;
            add.Even();

            return(add);
        }
Exemple #3
0
        public Rational Sub(Rational c)
        {
            Rational sub = new Rational();

            sub.Numerator   = this.Numerator * c.Denominator - c.Numerator * this.Denominator;
            sub.Denominator = this.Denominator * c.Denominator;
            Console.WriteLine(sub.ToString());
            sub.Even();

            return(sub);
        }
Exemple #4
0
        public static Rational operator +(Rational x, Rational y)
        {
            Rational result = new Rational
            {
                Numerator   = x.Numerator * y.Denominator + y.Numerator * x.Denominator,
                Denominator = x.Denominator * y.Denominator
            };

            result.Even();
            return(result);
        }
Exemple #5
0
        public static Rational operator /(Rational x, Rational y)
        {
            if (y.Numerator == 0)
            {
                throw new DivideByZeroException();
            }
            Rational result = new Rational
            {
                Numerator   = x.Numerator * y.Denominator,
                Denominator = x.Denominator * y.Numerator
            };

            result.Even();

            return(result);
        }
Exemple #6
0
        public Rational DivideBy(Rational x)
        {
            Rational div = new Rational();

            if (x.Numerator < 0)
            {
                div.Numerator   = this.Numerator * x.Denominator;
                div.Denominator = this.Denominator * (-x.Numerator);
                div             = div.Negate();
            }
            else if (x.Numerator > 0)
            {
                div.Numerator   = this.Numerator * x.Denominator;
                div.Denominator = this.Denominator * x.Numerator;
            }
            else
            {
                throw new DivideByZeroException();
            }

            div.Even();
            return(div);
        }