100 lines
2.9 KiB
C#
100 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Programmation_objet_TLESIO21.TD2 {
|
|
public class TabClasse {
|
|
|
|
private int nbMaxElts;
|
|
private int[] tab;
|
|
private int nbElts;
|
|
|
|
public TabClasse() : this(0) { }
|
|
|
|
public TabClasse(int nbMaxElts) {
|
|
this.nbMaxElts = nbMaxElts;
|
|
this.tab = new int[nbMaxElts];
|
|
this.nbElts = 0;
|
|
}
|
|
|
|
public void RemplirTableau() {
|
|
this.nbElts = 0;
|
|
for (int i = 0; i < this.nbMaxElts; ++i) {
|
|
Console.WriteLine("Entrez un entier (" + (this.nbMaxElts - i) + " restants) : ");
|
|
String input = Console.ReadLine();
|
|
Console.Write("");
|
|
|
|
if (String.IsNullOrEmpty(input)) {
|
|
i--;
|
|
Console.WriteLine("Merci de renter une valeur.");
|
|
}
|
|
else {
|
|
try {
|
|
tab[i] = int.Parse(input);
|
|
this.nbElts++;
|
|
}
|
|
catch (Exception FormatException) {
|
|
i--;
|
|
Console.WriteLine("Merci de renter un entier.");
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public void AfficherTableau() {
|
|
String s = "[";
|
|
for (int i = 0; i < this.tab.Length; ++i) {
|
|
s += this.tab[i] + ", ";
|
|
}
|
|
Console.WriteLine(s.Substring(0, s.Length - 2) + "]");
|
|
}
|
|
|
|
public void TrierTableau() { //tri à bulles
|
|
for (int i = (this.tab.Length - 1); i >= 1; --i) {
|
|
for (int j = 2; j <= i; ++j) {
|
|
if (this.tab[j - 1] > this.tab[j]) {
|
|
int temp = this.tab[j - 1];
|
|
this.tab[j - 1] = this.tab[j];
|
|
this.tab[j] = temp;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/*public void SetValue(int index, int value) {
|
|
if (index < this.nbElts) {
|
|
this.tab[index] = value;
|
|
}
|
|
else {
|
|
Console.Error.WriteLine("Votre index dépasse la capacité de ce tableau");
|
|
}
|
|
}*/
|
|
|
|
public int this[int index] {
|
|
get {
|
|
if (index < this.nbElts) {
|
|
return this.tab[index];
|
|
}
|
|
else {
|
|
Console.Error.Write("Votre index dépasse la capacité de ce tableau");
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
set {
|
|
|
|
if (index < this.nbElts) {
|
|
this.tab[index] = value;
|
|
}
|
|
else {
|
|
Console.Error.Write("Votre index dépasse la capacité de ce tableau");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
} |