Rational-Bojidara

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.9 kB
1
Indexable
Never
using System;

namespace Rational
{
    class Rational
    {
        int num;
        int den;
        public Rational(Rational r)
        {
            this.num = r.num;
            this.den = r.den;
        }
        public Rational(int n, int d)
        {
            this.num = n;
            this.den = d;
            normalize();
        }
        public double getDec()
        {
            return (double)this.num / this.den;
        }
        
        public Rational multi(Rational r)
        {
            return new Rational(this.num * r.num, this.den * r.den);
        }
        public string print(){
            return string.Format("{0}/{1}", this.num, this.den);
        }
        
        public Rational dev(Rational r)
        {
            return new Rational(this.num * r.den, this.den * r.num);
        }
        
        public Rational add(Rational r)
        {
         int n = r.num*this.den + r.den*this.num;
         int d = this.den *= r.den;
            return new Rational(n, d);
           
        }
        public Rational sub(Rational r)
        {
            int t = (r.den * this.num) - (r.num * this.den);
            int p = r.den*this.den;
            return new Rational(t, p);
        }
        void normalize()
        {
            int a = Math.Abs(this.num);
            int b = Math.Abs(this.den);
           while (a != b)
            {
                if (a > b) a -= b;
                else b -= a;
            }
            this.num /= a;
            this.den /= a;
        }
    }

    class HelloWorld 
    {
        static void Main(string[] args)
    {
        Rational a = new Rational(2,5);
        Rational b = new Rational(3,4);
        Rational c = a.sub(b);
        Console.WriteLine(c.print());
        Console.WriteLine(c.getDec());  
    }

    }
}