Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp3
- {
- class Ensemble<type>
- {
- public static int N = 0;
- type[] tab = null;
- public Ensemble(type[] vec)
- {
- for (int i = 0; i < vec.Length; i++)
- {
- if (!Ifexists(this, vec[i]))
- {
- AjouterElement(this);
- tab[i] = vec[i];
- N++;
- }
- }
- }
- private static void AjouterElement(Ensemble<type> a)
- {
- type[] tab1 = new type[N + 1];
- for (int i = 0; i < N; i++)
- {
- tab1[i] = a.tab[i];
- }
- a.tab = tab1;
- }
- private static void SupprimerElement(Ensemble<type> a, type nb)
- {
- type[] tab1 = new type[N - 1];
- for (int i = 0; i < N - 1; i++)
- {
- if (a.tab[i] == (dynamic)nb)
- {
- a.tab[i] = a.tab[i + 1];
- a.tab[i + 1] = nb;
- }
- }
- for (int i = 0; i < N - 1; i++)
- {
- tab1[i] = a.tab[i];
- }
- a.tab = tab1;
- }
- public static bool Ifexists(Ensemble<type> a, type nb)
- {
- for (int i = 0; i < N; i++)
- {
- if ((dynamic)nb == a.tab[i])
- {
- return true;
- }
- }
- return false;
- }
- public static Ensemble<type> operator +(Ensemble<type> ensemble, type nb)
- {
- if (Ifexists(ensemble, nb) == false) {
- AjouterElement(ensemble);
- ensemble.tab[N] = nb;
- N++;
- }
- return ensemble;
- }
- public static Ensemble<type> operator -(Ensemble<type> ensemble, type nb)
- {
- if (Ifexists(ensemble, nb) == true)
- {
- SupprimerElement(ensemble, nb);
- N--;
- }
- return ensemble;
- }
- public static Ensemble<type> operator ++(Ensemble<type> ensemble)
- {
- int nb = 1;
- for (int i = 0; i < N; i++)
- {
- ensemble.tab[i] += (dynamic)nb;
- }
- return ensemble;
- }
- public type this[int i]
- {
- get
- {
- return tab[i];
- }
- set
- {
- tab[i] = value;
- }
- }
- public void Afficher()
- {
- Console.Write("votre ensemble contient :");
- for (int i = 0; i < N; i++)
- {
- Console.Write(tab[i]+",");
- }
- Console.WriteLine(" le nombre d'element de l'ensemble est :"+N);
- }
- public float Moyenne
- {
- get
- {
- float a = 0;
- for (int i=0;i<N;i++)
- {
- a += (dynamic)this.tab[i];
- }
- a /= (dynamic)N;
- return a;
- }
- }
- public void Sort()
- {
- for (int i = 1; i < N; i++)
- {
- for (int j = 0; j < (N - i); j++)
- {
- if (tab[j] > (dynamic)tab[j + 1] && (dynamic)tab[j + 1] > 0)
- {
- type temp = tab[j];
- tab[j] = tab[j + 1];
- tab[j + 1] = temp;
- }
- }
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- int[] vect = new int[] {2,3,1,4,7};
- Ensemble<int> test = new Ensemble<int>(vect);
- test.Afficher();
- test += 8;
- test.Afficher();
- test += 4;
- test.Afficher();
- ++test;
- test.Afficher();
- test -= 3;
- test.Afficher();
- test.Sort();
- test.Afficher();
- Console.WriteLine(test.Moyenne);
- Console.WriteLine(test[1]);
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement