Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void ChargerLaPopulation(string filePath) // Charge la population en mémoire
- {
- try
- {
- Excel.Application xlApp = new Excel.Application();
- xlWorkBook = xlApp.Workbooks.Open(filePath);
- Excel._Worksheet xlWorksheet = xlWorkBook.Sheets[1];
- Excel.Range xlRange = xlWorksheet.UsedRange;
- Nb_Colonnes_Pop = xlRange.Columns.Count;
- Nb_Lignes_Pop = xlRange.Rows.Count;
- InitialiserTableauPopulation(Nb_Lignes_Pop, Nb_Colonnes_Pop);
- for (int y = 0; y < Nb_Lignes_Pop; y++)
- {
- for (int x = 0; x < Nb_Colonnes_Pop; x++)
- {
- tableauPopulation[y, x] = xlRange.Cells[y + 1, x + 1].Value2.ToString();
- }
- }
- xlApp.Workbooks.Close();
- cheminFichier = filePath;
- }
- catch (Exception ex)
- {
- MessageBox.Show("Une erreur s'est produite lors de la lecture du fichier !");
- }
- }
- private string[,] GénérerÉchantillonAléatoire() // À partir de la population chargée.
- {
- // 1. Initialiser les attributs.
- List<int> listeIndexTirés = new List<int>();
- Random générateur = new Random();
- int unNombre = 0;
- int grosseurÉchantillon = CalculerTailleÉchantillon();
- // 2. Déclarer un tableau qui contiendra notre échantillonnage de notre population + une ligne avec le nom des colonnes.
- string[,] tableauÉchantillon = new string[grosseurÉchantillon + 1, Nb_Colonnes_Pop];
- // 3. Ajouter les noms de colones dans le tableau (facile à ajouter, première ligne de notre tableau population.)
- for (int Colonne = 0; Colonne < Nb_Colonnes_Pop; Colonne++)
- {
- tableauÉchantillon[0, Colonne] = tableauPopulation[0, Colonne];
- }
- // 4. Faire une liste contenant les index de lignes à ajouter à notre tableau.
- for (int i = 0; i < grosseurÉchantillon; i++)
- {
- bool indexAjouté = false;
- do
- {
- 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.
- if (!listeIndexTirés.Contains(unNombre))
- {
- listeIndexTirés.Add(unNombre);
- indexAjouté = true;
- }
- } while (!indexAjouté);
- }
- // 5. Transferer les bonnes lignes de notre tableau population à notre tableau échantillon
- for (int ligne = 1; ligne <= listeIndexTirés.Count; ligne++)
- {
- for (int colonne = 0; colonne < Nb_Colonnes_Pop; colonne++)
- {
- tableauÉchantillon[ligne, colonne] = tableauPopulation[listeIndexTirés[ligne - 1], colonne];
- }
- }
- return tableauÉchantillon;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement