Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.20 KB | None | 0 0
  1. public void ChargerLaPopulation(string filePath) // Charge la population en mémoire
  2.         {
  3.             try
  4.             {
  5.                 Excel.Application xlApp = new Excel.Application();
  6.                 xlWorkBook = xlApp.Workbooks.Open(filePath);
  7.                 Excel._Worksheet xlWorksheet = xlWorkBook.Sheets[1];
  8.                 Excel.Range xlRange = xlWorksheet.UsedRange;
  9.                 Nb_Colonnes_Pop = xlRange.Columns.Count;
  10.                 Nb_Lignes_Pop = xlRange.Rows.Count;
  11.  
  12.                 InitialiserTableauPopulation(Nb_Lignes_Pop, Nb_Colonnes_Pop);
  13.                 for (int y = 0; y < Nb_Lignes_Pop; y++)
  14.                 {
  15.                     for (int x = 0; x < Nb_Colonnes_Pop; x++)
  16.                     {
  17.                         tableauPopulation[y, x] = xlRange.Cells[y + 1, x + 1].Value2.ToString();
  18.                     }
  19.                 }
  20.                 xlApp.Workbooks.Close();
  21.                 cheminFichier = filePath;
  22.             }
  23.             catch (Exception ex)
  24.             {
  25.                 MessageBox.Show("Une erreur s'est produite lors de la lecture du fichier !");
  26.             }
  27.         }
  28.  
  29. private string[,] GénérerÉchantillonAléatoire() // À partir de la population chargée.
  30.         {
  31.             // 1. Initialiser les attributs.
  32.             List<int> listeIndexTirés = new List<int>();
  33.             Random générateur = new Random();
  34.             int unNombre = 0;
  35.             int grosseurÉchantillon = CalculerTailleÉchantillon();
  36.  
  37.             // 2. Déclarer un tableau qui contiendra notre échantillonnage de notre population + une ligne avec le nom des colonnes.
  38.             string[,] tableauÉchantillon = new string[grosseurÉchantillon + 1, Nb_Colonnes_Pop];
  39.  
  40.             // 3. Ajouter les noms de colones dans le tableau (facile à ajouter, première ligne de notre tableau population.)
  41.             for (int Colonne = 0; Colonne < Nb_Colonnes_Pop; Colonne++)
  42.             {
  43.                 tableauÉchantillon[0, Colonne] = tableauPopulation[0, Colonne];
  44.             }
  45.             // 4. Faire une liste contenant les index de lignes à ajouter à notre tableau.
  46.             for (int i = 0; i < grosseurÉchantillon; i++)
  47.             {
  48.                 bool indexAjouté = false;
  49.                 do
  50.                 {
  51.                     unNombre = générateur.Next(1, Nb_Lignes_Pop); // Le nombre de ligne inclut les noms de colonnes, mais le générateur ne prendra pas la valeur maximale de toute façon.
  52.                     if (!listeIndexTirés.Contains(unNombre))
  53.                     {
  54.                         listeIndexTirés.Add(unNombre);
  55.                         indexAjouté = true;
  56.                     }
  57.                 } while (!indexAjouté);
  58.             }
  59.             // 5. Transferer les bonnes lignes de notre tableau population à notre tableau échantillon
  60.             for (int ligne = 1; ligne <= listeIndexTirés.Count; ligne++)
  61.             {
  62.                 for (int colonne = 0; colonne < Nb_Colonnes_Pop; colonne++)
  63.                 {
  64.                     tableauÉchantillon[ligne, colonne] = tableauPopulation[listeIndexTirés[ligne - 1], colonne];
  65.                 }
  66.             }
  67.  
  68.             return tableauÉchantillon;
  69.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement