Advertisement
Guest User

mp

a guest
May 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp3
  8. {
  9.     class Ensemble<type>
  10.     {
  11.         public static int N = 0;
  12.         type[] tab = null;
  13.  
  14.         public Ensemble(type[] vec)
  15.         {
  16.             for (int i = 0; i < vec.Length; i++)
  17.             {
  18.                 if (!Ifexists(this, vec[i]))
  19.                 {
  20.                     AjouterElement(this);
  21.                     tab[i] = vec[i];
  22.                     N++;
  23.                 }
  24.             }
  25.         }
  26.         private static void AjouterElement(Ensemble<type> a)
  27.         {
  28.             type[] tab1 = new type[N + 1];
  29.             for (int i = 0; i < N; i++)
  30.             {
  31.                 tab1[i] = a.tab[i];
  32.             }
  33.             a.tab = tab1;
  34.         }
  35.         private static void SupprimerElement(Ensemble<type> a, type nb)
  36.         {
  37.             type[] tab1 = new type[N - 1];
  38.             for (int i = 0; i < N - 1; i++)
  39.             {
  40.                 if (a.tab[i] == (dynamic)nb)
  41.                 {
  42.                     a.tab[i] = a.tab[i + 1];
  43.                     a.tab[i + 1] = nb;
  44.                 }
  45.             }
  46.             for (int i = 0; i < N - 1; i++)
  47.             {
  48.                 tab1[i] = a.tab[i];
  49.             }
  50.             a.tab = tab1;
  51.         }
  52.         public static bool Ifexists(Ensemble<type> a, type nb)
  53.         {
  54.             for (int i = 0; i < N; i++)
  55.             {
  56.                 if ((dynamic)nb == a.tab[i])
  57.                 {
  58.                     return true;
  59.                 }
  60.  
  61.  
  62.             }
  63.             return false;
  64.         }
  65.         public static Ensemble<type> operator +(Ensemble<type> ensemble, type nb)
  66.         {
  67.             if (Ifexists(ensemble, nb) == false) {
  68.                 AjouterElement(ensemble);
  69.                 ensemble.tab[N] = nb;
  70.                 N++;
  71.             }
  72.             return ensemble;
  73.         }
  74.         public static Ensemble<type> operator -(Ensemble<type> ensemble, type nb)
  75.         {
  76.             if (Ifexists(ensemble, nb) == true)
  77.             {
  78.                 SupprimerElement(ensemble, nb);
  79.                 N--;
  80.             }
  81.             return ensemble;
  82.         }
  83.         public static Ensemble<type> operator ++(Ensemble<type> ensemble)
  84.         {
  85.             int nb = 1;
  86.             for (int i = 0; i < N; i++)
  87.             {
  88.                 ensemble.tab[i] += (dynamic)nb;
  89.             }
  90.             return ensemble;
  91.         }
  92.         public type this[int  i]
  93.             {
  94.             get
  95.             {
  96.                 return tab[i];
  97.             }
  98.             set
  99.             {
  100.                 tab[i] = value;
  101.             }
  102.             }
  103.          public void Afficher()
  104.         {
  105.             Console.Write("votre ensemble contient :");
  106.             for (int i = 0; i < N; i++)
  107.             {
  108.                 Console.Write(tab[i]+",");
  109.             }
  110.             Console.WriteLine(" le nombre d'element de l'ensemble est :"+N);
  111.         }
  112.          public float Moyenne
  113.         {
  114.             get
  115.             {
  116.                 float a = 0;
  117.                  for (int i=0;i<N;i++)
  118.                 {
  119.                     a += (dynamic)this.tab[i];                  
  120.                 }
  121.                 a /= (dynamic)N;
  122.                
  123.                 return a;
  124.             }
  125.         }
  126.         public void Sort()
  127.         {
  128.             for (int i = 1; i < N; i++)
  129.             {
  130.                 for (int j = 0; j < (N - i); j++)
  131.                 {
  132.                     if (tab[j] > (dynamic)tab[j + 1] && (dynamic)tab[j + 1] > 0)
  133.                     {
  134.                         type temp = tab[j];
  135.                         tab[j] = tab[j + 1];
  136.                         tab[j + 1] = temp;
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.     }
  142.     class Program
  143.     {
  144.         static void Main(string[] args)
  145.         {
  146.            
  147.             int[] vect = new int[] {2,3,1,4,7};
  148.             Ensemble<int> test = new Ensemble<int>(vect);
  149.             test.Afficher();
  150.             test += 8;
  151.             test.Afficher();
  152.             test += 4;
  153.             test.Afficher();
  154.             ++test;
  155.             test.Afficher();
  156.             test -= 3;
  157.             test.Afficher();
  158.             test.Sort();
  159.             test.Afficher();
  160.             Console.WriteLine(test.Moyenne);
  161.             Console.WriteLine(test[1]);
  162.             Console.ReadKey();
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement