IFT 1179 : Programmation en C#
A) Tri avec Sort et Recherche avec BinarySearch:
En classe, on a déjà montré le tri des tableaux d’entiers ou de chaînes de caractères en utilisant Array.Sort(…) ;
Cette méthode a fonctionné parfaitement car System.Int32 (associée à int) et System.String (associée à string) ont implémenté l’interface IComparable.
On a aussi trié un tableau des stations de métro (classe Station) avec Array.Sort(. . .) Cette méthode fonctionne car Station a implémenté l’interface IComparable.
Une fois que le tableau est trié, on peut utiliser Array.BinarySearch pour la recherche binaire (recherche dichotomique) selon la même clé de tri.
Quelques réflexions s’imposent :
1. Peut-on trier selon une clé combinée (exemple : trier un tableau des personnes selon d’abord le nom et prénom puis selon leurs numéros) ?
La solution se trouve dans l’exemple Sort0.cs
2. Peut-on forcer la méthode Array.Sort d’utiliser une autre méthode de comparaison au lieu de CompareTo de IComparable ?
La réponse se trouve dans plusieurs exemples : Sort1.cs, Sort2.cs, Sort3.cs Exemple Sort0.cs : (attention au saut de ligne de Word)
/* Fichier Sort0.cs
* Clé de tri combinée : selon d'abord les noms et prénoms puis numéros *
* Préparé par LVN pour IFT 1179 */
using System;
using System.IO;
class Personne : IComparable {
private string nomPre;
private char sexe;
private double taille, poids;
private int numero;
public Personne(string nomPre, char sexe,double taille, double poids,int numero)
{
this.nomPre = nomPre.ToUpper();
this.numero = numero;
this.sexe = sexe;
this.taille = taille;
this.poids = poids;
}
public Personne() {
}
public override string ToString() {
return string.Format("{0, 30:S} {1, 5:D} {2, 7:F2} {3, 8:F1}
{4, 15:S}", nomPre, numero, taille, poids, (sexe == 'F' ? "feminin":"masculin"));
}
public int CompareTo(object obj) { Personne autre = (Personne) obj;
string chaineCourante = string.Format("{0,30:S} {1,4:D}", nomPre, numero),
chaineAutre = string.Format("{0,30:S} {1,4:D}", autre.nomPre, autre.numero);
return chaineCourante.CompareTo(chaineAutre);
} }
class Sort0 {
static void LireRemplir(string nomFichier, Personne[] pers, out int n)
{
n = 0;
StreamReader aLire = File.OpenText(nomFichier);
string ligneLue = null;
while ( (ligneLue = aLire.ReadLine()) != null) {
string nom = ligneLue.Substring(0, 30);
char sexe = ligneLue[30];
double taille = double.Parse(ligneLue.Substring(36, 8));
double poids = double.Parse(ligneLue.Substring(50, 8));
int num = int.Parse(ligneLue.Substring(60));
pers[n++] = new Personne(nom, sexe, taille, poids, num);
}
aLire.Close();
}
static void Afficher(Personne[] pers, int nbPers, string mess) {
Console.WriteLine("Contenu du tableau des personnes " + mess + ":\n");
for(int i = 0; i < nbPers; i++)
Console.WriteLine("{0,3}) {1}", i, pers[i]);
Console.WriteLine();
}
static void Main(string[] args) {
const int MAX_PERS = 20; // au maximum 20 personnes Personne[] pers = new Personne[MAX_PERS];
int nbPers; // le nombre effectif de personnes lues
LireRemplir("R:\\Met_A05.txt", pers, out nbPers);
Afficher(pers, nbPers, "avant le tri");
Array.Sort(pers, 0, nbPers);
Afficher(pers, nbPers, "apres le tri selon d'abord les noms et prenoms puis les numeros");
}
}
/* Exécution:
Contenu du tableau des personnes avant le tri:
0) ROY CHANTAL 2754 1,63 54,9 feminin
1) MOLAISON CLAUDE 1848 1,57 62,2 masculin
2) BEDARD MARC-ANDRE 2636 1,43 80,5 masculin
3) ROBITAILLE SUZANNE 4119 1,58 60,2 feminin
4) MONAST STEPHANE 1750 1,65 61,7 masculin
5) JALBERT LYNE 2168 1,63 52,6 feminin
6) DUBE FRANCOISE 4612 1,68 67,5 feminin
7) ROBITAILLE SUZANNE 2325 1,72 65,4 feminin
8) LABELLE LISE 1512 1,79 68,0 feminin
9) RIVERIN HELENE 2340 1,71 60,8 feminin
10) MICHAUD NORMAND 3428 1,73 103,7 masculin
11) RICHER AGATHE 3563 1,65 53,1 feminin
12) BEGIN MARIE-LUCE 4101 1,62 49,0 feminin
13) ROBITAILLE SUZANNE 4371 1,48 61,5 feminin
14) DUMITRU PIERRE 3629 1,92 99,4 masculin
15) FILLION ERIC 2630 1,78 75,7 masculin
16) DESMARAIS DENISE 3215 1,75 58,7 feminin
17) TREMBLAY MARC 3529 1,79 64,9 masculin
18) TREMBLAY SYLVAIN 1538 1,83 86,2 masculin
Contenu du tableau des personnes apres le tri selon d'abord les noms et prenoms
puis les numeros:
0) BEDARD MARC-ANDRE 2636 1,43 80,5 masculin
1) BEGIN MARIE-LUCE 4101 1,62 49,0 feminin
2) DESMARAIS DENISE 3215 1,75 58,7 feminin
3) DUBE FRANCOISE 4612 1,68 67,5 feminin
4) DUMITRU PIERRE 3629 1,92 99,4 masculin
5) FILLION ERIC 2630 1,78 75,7 masculin
6) JALBERT LYNE 2168 1,63 52,6 feminin
7) LABELLE LISE 1512 1,79 68,0 feminin
8) MICHAUD NORMAND 3428 1,73 103,7 masculin
9) MOLAISON CLAUDE 1848 1,57 62,2 masculin
10) MONAST STEPHANE 1750 1,65 61,7 masculin
11) RICHER AGATHE 3563 1,65 53,1 feminin
12) RIVERIN HELENE 2340 1,71 60,8 feminin
13) ROBITAILLE SUZANNE 2325 1,72 65,4 feminin
14) ROBITAILLE SUZANNE 4119 1,58 60,2 feminin
15) ROBITAILLE SUZANNE 4371 1,48 61,5 feminin
16) ROY CHANTAL 2754 1,63 54,9 feminin
17) TREMBLAY MARC 3529 1,79 64,9 masculin
18) TREMBLAY SYLVAIN 1538 1,83 86,2 masculin
Press any key to continue
*/
Exemple Sort1.cs : (attention au saut de ligne de Word)
/* Fichier Sort1.cs
* Peut-on ADAPTER CompareTo pour trier selon plusieurs clés ? * (Cette version est un peu "trichée". On s'en parle en classe *
* Préparé par LVN pour IFT 1179 */
using System;
using System.IO;
class Personne : IComparable {
private string nomPre;
private char sexe;
private double taille, poids;
private int numero;
// petit truc pour trier selon une clé voulue public static int codeTri;
public Personne(string nomPre, char sexe,double taille, double poids,int numero)
{
this.nomPre = nomPre.ToUpper();
this.numero = numero;
this.sexe = sexe;
this.taille = taille;
this.poids = poids;
}
public Personne() {
}
public int Numero {
get { return numero; } }
public override string ToString() {
return string.Format("{0, 30:S} {1, 5:D} {2, 7:F2} {3, 8:F1}
{4, 15:S}",
nomPre, numero, taille, poids, (sexe == 'F' ? "feminin":"masculin"));
}
public int CompareTo(object obj) { Personne autre = (Personne) obj;
if (codeTri == 1) // selon nom et prenom
return nomPre.CompareTo(autre.nomPre) ; else
// selon les numéros des personnes return numero - autre.numero;
} }
class Sort1 {
static void LireRemplir(string nomFichier, Personne[] pers, out int n)
{
n = 0;
StreamReader aLire = File.OpenText(nomFichier);
string ligneLue = null;
while ( (ligneLue = aLire.ReadLine()) != null) {
string nom = ligneLue.Substring(0, 30);
char sexe = ligneLue[30];
double taille = double.Parse(ligneLue.Substring(36, 8));
double poids = double.Parse(ligneLue.Substring(50, 8));
int num = int.Parse(ligneLue.Substring(60));
pers[n++] = new Personne(nom, sexe, taille, poids, num);
}
aLire.Close();
}
static void Afficher(Personne[] pers, int nbPers, string mess) {
Console.WriteLine("Contenu du tableau des personnes " + mess + ":\n");
for(int i = 0; i < nbPers; i++)
Console.WriteLine("{0,3}) {1}", i, pers[i]);
Console.WriteLine();
}
// clé de recherche : un nom et prénom => il faut trier selon nom et prénom
static void ChercherDicho(Personne[] pers, int nbPers, string nomRecherche)
{
nomRecherche = nomRecherche.ToUpper();
for (int i = nomRecherche.Length ; i < 30; i++) nomRecherche += " ";
Personne aChercher = new Personne(nomRecherche, ' ', 0.0, 0.0, 0);
Personne.codeTri = 1;
Array.Sort(pers, 0, nbPers);
Afficher(pers, nbPers, "apres le tri selon le nom et prenom");
int indice = Array.BinarySearch(pers, 0, nbPers, aChercher);
if (indice >= 0)
Console.WriteLine("On trouve " + nomRecherche + " a l'indice " +
indice + " :\n" + pers[indice]);
else
Console.WriteLine("On ne le trouve pas");
Console.WriteLine();
}
// clé de recherche : un numéro => il faut trier selon numéro static void ChercherDicho(int numRecherche, Personne[] pers, int nbPers)
{
Personne aChercher = new Personne("", ' ', 0.0, 0.0, numRecherche);
Personne.codeTri = 0;
Array.Sort(pers, 0, nbPers);
Afficher(pers, nbPers, "apres le tri selon les numeros des personnes");
int indice = Array.BinarySearch(pers, 0, nbPers, aChercher);
if (indice >= 0)
Console.WriteLine("On trouve " + numRecherche + " a l'indice " +
indice + " :\n" + pers[indice]);
else
Console.WriteLine("On ne le trouve pas");
Console.WriteLine();
}
static void Main(string[] args) {
const int MAX_PERS = 20; // au maximum 20 personnes Personne[] pers = new Personne[MAX_PERS];
int nbPers; // le nombre effectif de personnes lues
LireRemplir("R:\\Met_A05.txt", pers, out nbPers);
Afficher(pers, nbPers, "avant le tri");
ChercherDicho(pers, nbPers, "Roy Chantal");
ChercherDicho(3215, pers, nbPers);
} }
/* Exécution:
Contenu du tableau des personnes avant le tri:
0) ROY CHANTAL 2754 1,63 54,9 feminin
1) MOLAISON CLAUDE 1848 1,57 62,2 masculin
2) BEDARD MARC-ANDRE 2636 1,43 80,5 masculin
3) MONAST STEPHANE 1750 1,65 61,7 masculin
4) JALBERT LYNE 2168 1,63 52,6 feminin
5) DUBE FRANCOISE 4612 1,68 67,5 feminin
6) ROBITAILLE SUZANNE 2325 1,72 65,4 feminin
7) LABELLE LISE 1512 1,79 68,0 feminin
8) RIVERIN HELENE 2340 1,71 60,8 feminin
9) MICHAUD NORMAND 3428 1,73 103,7 masculin
10) RICHER AGATHE 3563 1,65 53,1 feminin
11) BEGIN MARIE-LUCE 4101 1,62 49,0 feminin
12) ROBITAILLE SUZANNE 4371 1,48 61,5 feminin
13) DUMITRU PIERRE 3629 1,92 99,4 masculin
14) FILLION ERIC 2630 1,78 75,7 masculin
15) DESMARAIS DENISE 3215 1,75 58,7 feminin
16) TREMBLAY MARC 3529 1,79 64,9 masculin
17) TREMBLAY SYLVAIN 1538 1,83 86,2 masculin
18) ROBITAILLE SUZANNE 4119 1,58 60,2 feminin
Contenu du tableau des personnes apres le tri selon le nom et prenom:
0) BEDARD MARC-ANDRE 2636 1,43 80,5 masculin
1) BEGIN MARIE-LUCE 4101 1,62 49,0 feminin
2) DESMARAIS DENISE 3215 1,75 58,7 feminin
3) DUBE FRANCOISE 4612 1,68 67,5 feminin
4) DUMITRU PIERRE 3629 1,92 99,4 masculin
5) FILLION ERIC 2630 1,78 75,7 masculin
6) JALBERT LYNE 2168 1,63 52,6 feminin
7) LABELLE LISE 1512 1,79 68,0 feminin
8) MICHAUD NORMAND 3428 1,73 103,7 masculin
9) MOLAISON CLAUDE 1848 1,57 62,2 masculin
10) MONAST STEPHANE 1750 1,65 61,7 masculin
11) RICHER AGATHE 3563 1,65 53,1 feminin
12) RIVERIN HELENE 2340 1,71 60,8 feminin
13) ROBITAILLE SUZANNE 4119 1,58 60,2 feminin
14) ROBITAILLE SUZANNE 2325 1,72 65,4 feminin
15) ROBITAILLE SUZANNE 4371 1,48 61,5 feminin
16) ROY CHANTAL 2754 1,63 54,9 feminin
17) TREMBLAY MARC 3529 1,79 64,9 masculin
18) TREMBLAY SYLVAIN 1538 1,83 86,2 masculin
On trouve ROY CHANTAL a l'indice 16 :
ROY CHANTAL 2754 1,63 54,9 feminin
Contenu du tableau des personnes apres le tri selon les numeros des personnes:
0) LABELLE LISE 1512 1,79 68,0 feminin
1) TREMBLAY SYLVAIN 1538 1,83 86,2 masculin
2) MONAST STEPHANE 1750 1,65 61,7 masculin
3) MOLAISON CLAUDE 1848 1,57 62,2 masculin
4) JALBERT LYNE 2168 1,63 52,6 feminin
5) ROBITAILLE SUZANNE 2325 1,72 65,4 feminin
6) RIVERIN HELENE 2340 1,71 60,8 feminin
7) FILLION ERIC 2630 1,78 75,7 masculin
8) BEDARD MARC-ANDRE 2636 1,43 80,5 masculin
9) ROY CHANTAL 2754 1,63 54,9 feminin
10) DESMARAIS DENISE 3215 1,75 58,7 feminin
11) MICHAUD NORMAND 3428 1,73 103,7 masculin
12) TREMBLAY MARC 3529 1,79 64,9 masculin
13) RICHER AGATHE 3563 1,65 53,1 feminin
14) DUMITRU PIERRE 3629 1,92 99,4 masculin
15) BEGIN MARIE-LUCE 4101 1,62 49,0 feminin
16) ROBITAILLE SUZANNE 4119 1,58 60,2 feminin
17) ROBITAILLE SUZANNE 4371 1,48 61,5 feminin
18) DUBE FRANCOISE 4612 1,68 67,5 feminin
On trouve 3215 a l'indice 10 :
DESMARAIS DENISE 3215 1,75 58,7 feminin
Press any key to continue
*/
Exemple Sort2.cs : (attention au saut de ligne de Word)
/* Fichier Sort2.cs
* Comparaison des chaines de caracteres : ordre Ascii vs culture ...
*
* Observez l'interface IComparer qui force le Sort d'exécuter
* la "comparaison voulue" au lieu de CompareTo implémentée (ici dans string)
*/
using System;
using System.Collections;
class Sort2 {
class PourTrier : IComparer {
int IComparer.Compare( Object x, Object y) {
return String.CompareOrdinal( (string) x, (string) y);
} }
static void Afficher(string[] tab, string mess) {
Console.WriteLine("Contenu du tableau " + mess);
for (int i = 0; i < tab.Length ; i++)
Console.WriteLine("{0,3:D}) {1:5D}", i, tab[i]);
Console.WriteLine();
}
static void AfficherAscii(char lettre1, char lettre2) {
for (char c = lettre1 ; c <= lettre2 ; c++) Console.Write("{0, 3:C}", c);
Console.WriteLine();
for (char c = lettre1 ; c <= lettre2 ; c++) Console.Write("{0, 3:D}", (int) c);
Console.WriteLine("\n");
}
static void AfficherAscii() {
Console.WriteLine("Caracteres et ses ordres: ");
AfficherAscii('0', '9');
AfficherAscii('A', 'Z');
AfficherAscii('a', 'z');
}
static void Main(string[] args) {
string[] tableau = { "abc", "DEF", "ABC", "XYZ", "def", "123", "765" };
Array.Sort(tableau);
AfficherAscii();
Afficher(tableau, "apres une sorte de tri (culture americaine)");
IComparer monTri = new PourTrier();
Array.Sort(tableau, monTri);
Afficher(tableau, "apres le tri qui respecte l'ordre ASCII");
}
}
/* Exécution:
Caracteres et ses ordres:
0 1 2 3 4 5 6 7 8 9 48 49 50 51 52 53 54 55 56 57
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
a b c d e f g h i j k l m n o p q r s t u v w x y z
97 98
99100101102103104105106107108109110111112113114115116117118119120121122
Contenu du tableau apres une sorte de tri (culture americaine) 0) 123
1) 765 2) abc 3) ABC 4) def 5) DEF 6) XYZ
Contenu du tableau apres le tri qui respecte l'ordre ASCII 0) 123
1) 765 2) ABC 3) DEF 4) XYZ 5) abc 6) def
Press any key to continue
*/
Exemple Sort3.cs : (attention au saut de ligne de Word)
/* Fichier Sort3.cs
* On force le Sort d'utiliser une autre méthode de comparaison */
using System;
using System.IO;
using System.Collections;
class Personne : IComparable {
private string nomPre;
private char sexe;
private double taille, poids;
private int numero;
public static int codeTri;
public Personne(string nomPre, char sexe,double taille, double poids,int numero)
{
this.nomPre = nomPre.ToUpper();
this.numero = numero;
this.sexe = sexe;
this.taille = taille;
this.poids = poids;
}
public Personne() {
}
public int Numero {
get { return numero; } }
public override string ToString() {
return string.Format("{0, 30:S} {1, 5:D} {2, 7:F2} {3, 8:F1}
{4, 15:S}", nomPre, numero, taille, poids, (sexe == 'F' ? "feminin":"masculin"));
}
public int CompareTo(object obj) { Personne autre = (Personne) obj;
return nomPre.CompareTo(autre.nomPre) ; }
}
class Sort3 {
class PourTrier : IComparer {
int IComparer.Compare( Object x, Object y) {
return ((Personne) x).Numero - ((Personne) y).Numero;
} }
static void LireRemplir(string nomFichier, Personne[] pers, out int n)
{
n = 0;
StreamReader aLire = File.OpenText(nomFichier);
string ligneLue = null;
while ( (ligneLue = aLire.ReadLine()) != null) {
string nom = ligneLue.Substring(0, 30);
char sexe = ligneLue[30];
double taille = double.Parse(ligneLue.Substring(36, 8));
double poids = double.Parse(ligneLue.Substring(50, 8));
int num = int.Parse(ligneLue.Substring(60));
pers[n++] = new Personne(nom, sexe, taille, poids, num);
}
aLire.Close();
}
static void Afficher(Personne[] pers, int nbPers, string mess) {
Console.WriteLine("Contenu du tableau des personnes " + mess + ":\n");
for(int i = 0; i < nbPers; i++)
Console.WriteLine("{0,3}) {1}", i, pers[i]);
Console.WriteLine();
}
static void ChercherDicho(Personne[] pers, int nbPers, string nomRecherche)
{
nomRecherche = nomRecherche.ToUpper();
for (int i = nomRecherche.Length ; i < 30; i++) nomRecherche += " ";
Personne aChercher = new Personne(nomRecherche, ' ', 0.0, 0.0, 0);
Personne.codeTri = 1;
Array.Sort(pers, 0, nbPers);
Afficher(pers, nbPers, "apres le tri selon le nom et prenom");
int indice = Array.BinarySearch(pers, 0, nbPers, aChercher);
if (indice >= 0)
Console.WriteLine("On trouve " + nomRecherche + " a l'indice " +
indice + " :\n" + pers[indice]);
else
Console.WriteLine("On ne le trouve pas");
Console.WriteLine();
}
static void ChercherDicho(int numRecherche, Personne[] pers, int nbPers)
{
Personne aChercher = new Personne("", ' ', 0.0, 0.0, numRecherche);
Personne.codeTri = 0;
Array.Sort(pers, 0, nbPers);
Afficher(pers, nbPers, "apres le tri selon les numeros des personnes");
int indice = Array.BinarySearch(pers, 0, nbPers, aChercher);
if (indice >= 0)
Console.WriteLine("On trouve " + numRecherche + " a l'indice " +
indice + " :\n" + pers[indice]);
else
Console.WriteLine("On ne le trouve pas");
Console.WriteLine();
}
static void Main(string[] args) {
const int MAX_PERS = 20; // au maximum 20 personnes Personne[] pers = new Personne[MAX_PERS];
int nbPers; // le nombre effectif de personnes lues
LireRemplir("R:\\Met_A05.txt", pers, out nbPers);
Afficher(pers, nbPers, "avant le tri");
Array.Sort(pers, 0, nbPers);
Afficher(pers, nbPers, "apres le tri (avec CompareTo)");
IComparer monTri = new PourTrier();
Array.Sort(pers, 0, nbPers, monTri);
Afficher(pers, nbPers, "apres le tri selon les numeros des personnes");
} }
/* Exécution:
Contenu du tableau des personnes avant le tri:
0) ROY CHANTAL 2754 1,63 54,9 feminin
1) MOLAISON CLAUDE 1848 1,57 62,2 masculin
2) BEDARD MARC-ANDRE 2636 1,43 80,5 masculin
3) ROBITAILLE SUZANNE 4119 1,58 60,2 feminin
4) MONAST STEPHANE 1750 1,65 61,7 masculin
5) JALBERT LYNE 2168 1,63 52,6 feminin
6) DUBE FRANCOISE 4612 1,68 67,5 feminin
7) ROBITAILLE SUZANNE 2325 1,72 65,4 feminin
8) LABELLE LISE 1512 1,79 68,0 feminin
9) RIVERIN HELENE 2340 1,71 60,8 feminin
10) MICHAUD NORMAND 3428 1,73 103,7 masculin
11) RICHER AGATHE 3563 1,65 53,1 feminin
12) BEGIN MARIE-LUCE 4101 1,62 49,0 feminin
13) ROBITAILLE SUZANNE 4371 1,48 61,5 feminin
14) DUMITRU PIERRE 3629 1,92 99,4 masculin
15) FILLION ERIC 2630 1,78 75,7 masculin
16) DESMARAIS DENISE 3215 1,75 58,7 feminin
17) TREMBLAY MARC 3529 1,79 64,9 masculin
18) TREMBLAY SYLVAIN 1538 1,83 86,2 masculin
Contenu du tableau des personnes apres le tri (avec CompareTo):
0) BEDARD MARC-ANDRE 2636 1,43 80,5 masculin
1) BEGIN MARIE-LUCE 4101 1,62 49,0 feminin
2) DESMARAIS DENISE 3215 1,75 58,7 feminin
3) DUBE FRANCOISE 4612 1,68 67,5 feminin
4) DUMITRU PIERRE 3629 1,92 99,4 masculin
5) FILLION ERIC 2630 1,78 75,7 masculin
6) JALBERT LYNE 2168 1,63 52,6 feminin
7) LABELLE LISE 1512 1,79 68,0 feminin
8) MICHAUD NORMAND 3428 1,73 103,7 masculin
9) MOLAISON CLAUDE 1848 1,57 62,2 masculin
10) MONAST STEPHANE 1750 1,65 61,7 masculin
11) RICHER AGATHE 3563 1,65 53,1 feminin
12) RIVERIN HELENE 2340 1,71 60,8 feminin
13) ROBITAILLE SUZANNE 4119 1,58 60,2 feminin
14) ROBITAILLE SUZANNE 2325 1,72 65,4 feminin
15) ROBITAILLE SUZANNE 4371 1,48 61,5 feminin
16) ROY CHANTAL 2754 1,63 54,9 feminin
17) TREMBLAY MARC 3529 1,79 64,9 masculin
18) TREMBLAY SYLVAIN 1538 1,83 86,2 masculin
Contenu du tableau des personnes apres le tri selon les numeros des personnes:
0) LABELLE LISE 1512 1,79 68,0 feminin
1) TREMBLAY SYLVAIN 1538 1,83 86,2 masculin
2) MONAST STEPHANE 1750 1,65 61,7 masculin
3) MOLAISON CLAUDE 1848 1,57 62,2 masculin
4) JALBERT LYNE 2168 1,63 52,6 feminin
5) ROBITAILLE SUZANNE 2325 1,72 65,4 feminin
6) RIVERIN HELENE 2340 1,71 60,8 feminin
7) FILLION ERIC 2630 1,78 75,7 masculin
8) BEDARD MARC-ANDRE 2636 1,43 80,5 masculin
9) ROY CHANTAL 2754 1,63 54,9 feminin
10) DESMARAIS DENISE 3215 1,75 58,7 feminin
11) MICHAUD NORMAND 3428 1,73 103,7 masculin
12) TREMBLAY MARC 3529 1,79 64,9 masculin
13) RICHER AGATHE 3563 1,65 53,1 feminin
14) DUMITRU PIERRE 3629 1,92 99,4 masculin
15) BEGIN MARIE-LUCE 4101 1,62 49,0 feminin
16) ROBITAILLE SUZANNE 4119 1,58 60,2 feminin
17) ROBITAILLE SUZANNE 4371 1,48 61,5 feminin
18) DUBE FRANCOISE 4612 1,68 67,5 feminin
Press any key to continue
*/
B) Classe ArrayList:
http://msdn.microsoft.com/library/fre/default.asp?ur l=/library/FRE/cpref/html/frlrfsystemcollectionsarra ylistclasstopic.asp
Une liste tableau (ArrayList) est une liste doublement chaînée dont l’abstraction de données se fait avec un tableau. La capacité (le nombre maximum d’éléments) et sa taille (son nombre d’éléments) sont dynamiques.
En classe, on montrera comment trouver les informations sur cette classe.
Les notes de cours s’en viennent.
Exemple List1.cs : (attention au saut de ligne de Word)
/* Fichier List1.cs (1er exemple de ArrayList) */
using System;
using System.Collections;
class List1
{
static ArrayList creerListe(int nombre) { ArrayList liste = new ArrayList();
Console.WriteLine("\nCapacite de cette liste : {0, 3:D}", liste.Capacity);
for (int candi = 1; candi <= nombre; candi++) if (nombre % candi == 0)
liste.Add(candi);
Console.WriteLine("\nCapacite maintenant de cette liste : {0, 3:D}", liste.Capacity);
return liste;
}
static void Afficher(ArrayList liste, string mess) {
if (mess.Equals("au debut"))
Console.WriteLine("\nContenu de la liste des diviseurs de " + liste[liste.Count-1]);
else
Console.WriteLine("\nContenu de la liste " + mess);
for (int i = 0; i < liste.Count; i++)
Console.WriteLine("{0, 3:D}) {1, 10:D}", i, liste[i]);
}
static void Demo(ArrayList commune) {
Afficher(commune, "des diviseurs communs de 100 et 720");
commune.RemoveAt(1);
commune.Remove(20);
Afficher(commune, "de la liste commune APRES 2 SUPPRESSIONS");
commune.Insert(0, 999);
commune.Insert(3, 777);
commune[5] = 666;
Afficher(commune, "de la liste commune APRES 2 AJOUTS et 1 MODIFICATION");
commune.Sort();
Afficher(commune, "des diviseurs communs de 100 et 720 apres le tri");
int indice = commune.BinarySearch(4);
Console.WriteLine("On trouve 4 a l'indice " + indice);
}
static ArrayList creerListe(ArrayList liste1, ArrayList liste2) {
ArrayList liste = new ArrayList();
for (int i = liste1.Count-1; i >= 0; i--) if (liste2.Contains(liste1[i]))
liste.Add(liste1[i]);
return liste;
}
static void Main(string[] args) {
const int NOMBRE1 = 100, NOMBRE2 = 720;
ArrayList divi100 = creerListe(NOMBRE1);
Afficher(divi100, "au debut");
ArrayList divi720 = creerListe(NOMBRE2);
Afficher(divi720, "au debut");
ArrayList commune = creerListe(divi100, divi720);
Demo(commune);
} }
/* Exécution:
Capacite de cette liste : 16
Capacite maintenant de cette liste : 16
Contenu de la liste des diviseurs de 100
0) 1
1) 2
2) 4
3) 5
4) 10
5) 20
6) 25
7) 50
8) 100
Capacite de cette liste : 16 Capacite maintenant de cette liste : 32 Contenu de la liste des diviseurs de 720 0) 1
1) 2
2) 3
3) 4
4) 5
5) 6
6) 8
7) 9
8) 10
9) 12
10) 15
11) 16
12) 18
13) 20
14) 24
15) 30
16) 36
17) 40
18) 45
19) 48
20) 60
21) 72
22) 80
23) 90
24) 120
25) 144
26) 180
27) 240
28) 360
29) 720
Contenu de la liste des diviseurs communs de 100 et 720 0) 20
1) 10
2) 5
3) 4
4) 2
5) 1
Contenu de la liste de la liste commune APRES 2 SUPPRESSIONS 0) 5
1) 4
2) 2
3) 1
Contenu de la liste de la liste commune APRES 2 AJOUTS et 1 MODIFICATION 0) 999
1) 5
2) 4
3) 777
4) 2
5) 666
Contenu de la liste des diviseurs communs de 100 et 720 apres le tri 0) 2
1) 4
2) 5
3) 666
4) 777
5) 999
On trouve 4 a l'indice 1 Press any key to continue*/