Probleme de informatică
Clasa a IX-a
Elementele de bază C++ (46)
Subprograme predefinite (1)
Fişiere text (2)
Algoritmi elementari (111)
Tablouri unidimensionale (83)
Tablouri bidimensionale (64)
Probleme diverse (13)
Clasa a X-a
Subprograme (funcții) (87)
Şiruri de caractere (50)
Tipul înregistrare (26)
Recursivitate (57)
Alocarea dinamică a memoriei (2)
Liste înlănţuite (25)
Algoritmul lui Lee (1)
Clasa a XI-a
Metoda "Divide et impera" (12)
Metoda Backtracking (86)
Metoda Greedy (6)
Programare dinamică (18)
Grafuri neorientate (40)
Grafuri orientate (38)
Arbori (33)
Clasa a XII-a
Elemente de bază C# (32)
POO în C# (14)
Programare vizuală în C# (19)
Examen de bacalaureat
Competențe digitale
Examen de atestat
Admitere UBB (18)
Sa se scrie o clasa rational care sa implementeze operatiile cu numere rationale.

class rational
    {
        private int nr, nu;

        public rational() { nr = 0; nu = 1; }
        public rational(int a, int b)
        { nr = a; nu = b; simplificare(); }

        public void afisare()
        {
            if (nu == 1) Console.Write(nr);
            else Console.Write(nr + "/" + nu);
        }

        public override string ToString()
        {
            if (nu == 1) return nr.ToString();
            else return nr + "/" + nu;
        }

        public static rational operator +(rational a, rational b)
        {return new rational(a.nr*b.nu+a.nu*b.nr,a.nu*b.nu);}

        public static rational operator -(rational a, rational b)
        { return new rational(a.nr * b.nu - a.nu * b.nr, a.nu * b.nu); }

        public static rational operator *(rational a, rational b)
        { return new rational(a.nr * b.nr , a.nu * b.nu); }

        public static rational operator /(rational a, rational b)
        { return new rational(a.nr * b.nu , a.nu * b.nr); }

        public static bool operator <(rational a, rational b)
        { return a.nr * b.nu < a.nu * b.nr; }

        public static bool operator >(rational a, rational b)
        { return a.nr * b.nu > a.nu * b.nr; }

        public static bool operator <=(rational a, rational b)
        { return !(a > b); }

        public static bool operator >=(rational a, rational b)
        { return !(a < b); }

        public static bool operator ==(rational a, rational b)
        { return a.nr * b.nu == a.nu * b.nr; }

        public static bool operator !=(rational a, rational b)
        { return !(a == b); }

        public static int comparare(rational a, rational b)
        {
            if (a < b) return -1;
            else if (a == b) return 0;
            else return 1;
        }
        
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        private int cmmdc(int a, int b)
        {
            while (b != 0)
            {
                int r = a % b; a = b; b = r;
            }
            return a;
        }

        private void simplificare()
        {
            int c = cmmdc(Math.Abs(nr), Math.Abs(nu));
            nr = nr / c; nu = nu / c;
            if (nr * nu < 0)
            {
                nr = -Math.Abs(nr); nu = Math.Abs(nu);
            }
        }
    }

21 nov 2024
Site-ul conține 884 de probleme rezolvate
Copyright © 2009-2018 Muresan Vasile Ciprian