Skip to content

Solutions possibles aux exercices de POO (1 à 3)

1. Classe Eleve

public class Eleve
{
    public string nom;
    public string prenom;
    public string da;
    public int[] notes;
    public int position;

    public Eleve(string nom, string prenom, string da, int nbNotes)
    {
        this.nom = nom;
        this.prenom = prenom;
        this.da = da;
        this.notes = new int[nbNotes];
        this.position = 0;
    }

    public void ajouterNote(int note)
    {
        if (this.position < this.notes.Length)
        {
            this.notes[this.position] = note;
            this.position++;
        }
    }

    // Assume que le tableau de notes est rempli
    public double calculerMoyenne()
    {
        int total = 0;

        foreach (int note in this.notes)
        {
            total += note;
        }

        return (double)total / this.notes.Length;
    }

    /* Défi - permet d'ajouter une note même si le tableau est plein. 
    public void ajouterNote(int note)
    {
        if (this.position >= this.notes.Length)
        {
            int[] nouvTableau = new int[this.notes.Length + 1];
            for(int i = 0; i < this.notes.Length; i++)
            {
                nouvTableau[i] = this.notes[i];
                this.notes = nouvTableau;
            }
        }
        this.notes[this.position] = note;
        this.position++;
    }
    */

}

2. Classe Fraction

public class Fraction
{
    public int numerateur;
    public int denominateur;

    public Fraction(int numerateur, int denominateur)
    {
        this.numerateur = numerateur;
        if(denominateur == 0)
        {
            this.denominateur = 1;
        }
        else 
        {
            this.denominateur = denominateur;
        } 
    }
}

3. Classe Arme

public class Arme
{
    public string nom;
    public int dommageMax;
    public int bonusMagique;

    public Arme(string nom, int dommageMax)
    {
        Random rng = new Random();
        this.nom = nom;
        this.dommageMax = dommageMax;

        // L'arme a 5% de chances d'être magique
        // 1 à 5 = magique, 6 à 100 = pas magique
        if (rng.Next(1, 101) <= 5)
        {
            // 0.01% -> 1 sur 10000 (nombre 1 tiré)
            int rngBonus = rng.Next(1, 10001);
            if (rngBonus == 1)
            {
                this.bonusMagique = 5;
            }
            // 0.05% -> 5 sur 10000 (2 à 6)
            else if (rngBonus <= 1 + 5)
            {
                this.bonusMagique = 4;
            }
            // 0.5% -> 50 sur 10000 (7 à 56)
            else if (rngBonus <= 1 + 5 + 50)
            {
                this.bonusMagique = 3;
            }
            // 1% -> 100 sur 10000 (57 à 156)
            else if (rngBonus <= 1 + 5 + 50 + 100)
            {
                this.bonusMagique = 2;
            }
            // 2% -> 200 sur 10000 (157 à 356)
            else if (rngBonus <= 1 + 5 + 50 + 100 + 200)
            {
                this.bonusMagique = 1;
            }
            else
            {
                this.bonusMagique = 0;
            }
        }
    }
}

4. Classe Caracteristique

public class Caracteristique
{
    public string nom;
    public int score;

    public Caracteristique(string nom, int score)
    {
        this.nom = nom;
        this.score = score;
    }

    public int calculerModificateur()
    {
        return this.score / 2 - 5;
    }

    public bool testerReussite(int difficulte)
    {
        Random rng = new Random();
        // Tirage entier 1 à 20
        int resultat = rng.Next(1, 21) + this.calculerModificateur();
        return resultat > difficulte;
    }
}

5. Classe Tableau dynamique et programme

5.1 TableauDynamique

public class TableauDynamique
{
    double[] tab;
    int cnt;
    public TableauDynamique()
    {
        this.tab = new double[10];
        this.cnt = 0;
    }

    public void ajouterNombre(double nombre)
    {
        // Si le nombre de donné
        if (this.cnt < this.tab.Length)
        {
            this.tab[this.cnt] = nombre;
            this.cnt++;
        }
        else 
        {
            // Nouveau tableau de 1.5 fois la longueur
            double[] nouvTab = new double[this.tab.Length + this.tab.Length/2];
            // Copier les données du tableau actuel dans le nouveau tableau
            for (int i = 0; i < this.tab.Length; i++)
            {
                nouvTab[i] = this.tab[i];
            }
            // Remplacer le tableau actuel par le nouveau tableau
            this.tab = nouvTab;

            this.tab[this.cnt] = nombre;
            this.cnt++;
        }
    }

    public double calculerTotal()
    {
        double total = 0.0;

        for (int i = 0; i < this.cnt; i++)
        {
            total += this.tab[i];
        }

        return total;
    }

    public double calculerMoyenne()
    { 
        return this.calculerTotal()/ this.cnt;
    }

    public double calculerMediane()
    {
        double mediane = 0;

        this.trierAsc();

        int position = this.cnt / 2;

        if (this.cnt % 2 == 0)
        {


            mediane = (this.tab[position] + this.tab[position - 1])/2;
        }
        else
        {
            mediane = this.tab[position];
        }

        return mediane;
    }

    public double calculerEcartType()
    {
        double moyenne = this.calculerMoyenne();
        double sommeCarreEcart = 0.0;
        double ecartType = 0.0;

        for (int i = 0; i < this.cnt; i++)
        {
            sommeCarreEcart += Math.Pow(this.tab[i] - moyenne, 2);
        }
        sommeCarreEcart /= this.cnt - 1;

        ecartType = Math.Sqrt(sommeCarreEcart);

        return ecartType;
    }

    public void trierAsc()
    {
        // Classer mon tableau du plus petit au plus grand
        // pour chaque case du tableau
        for (int i = 0; i < this.cnt; i++)
        {
            // trouver l'index(la position) de la note minimum du tableau restant
            int positionMin = trouverMin(i);
            // échange la note à la position minimum avec la note à la position actuelle
            double temp = this.tab[positionMin];
            this.tab[positionMin] = this.tab[i];
            this.tab[i] = temp;
        }
    }

    public int trouverMin(int depart)
    {
        int positionMin = depart;

        for (int i = depart; i < this.cnt; i++)
        {
            if (this.tab[i] < this.tab[positionMin])
            {
                positionMin = i;
            }
        }

        return positionMin;
    }
}

5.2 Programme principale

static void Main(string[] args)
{
    double note = 0.0;
    TableauDynamique tabNotes = new TableauDynamique();

    int option = 1;

    while (note >= 0.0)
    {
        Console.Clear();
        Console.WriteLine("Entrer les notes de votre groupe, note négative pour arrêter");

        double.TryParse(Console.ReadLine(), out note);

        if (note >= 0.0)
        {
            tabNotes.ajouterNombre(note);
        }
    }


    while (option > 0 && option < 5)
    {
        Console.Clear();
        Console.Write("Choisir une option 1-4\n 1. Moyenne\n 2. Mediane\n 3. Ecart type\n 4. Quitter");
        int.TryParse(Console.ReadLine(), out option);
        switch (option)
        {
            case 1:
                Console.WriteLine("Moyenne : {0}", tabNotes.calculerMoyenne());
                break;
            case 2:
                Console.WriteLine("Mediane : {0}", tabNotes.calculerMediane());
                break;
            case 3:
                Console.WriteLine("Ecart type {0}", tabNotes.calculerEcartType());
                break;
            case 4:
                Console.WriteLine("Fin");
                break;
            default:
                Console.WriteLine("Option invalide");
                option = 1;
                break;
        }
        Console.WriteLine("Appuyer sur une touche pour continuer");
        Console.ReadKey();
    }
}