Exercice VB: Manipulation de Tableau à une seul dimension

Ecrire le code VB qui permet de faire :

1-      Créer un tableau avec la méthode Randomize

2-      Tri à Bulle

3-      Tri par permutation

4-      Tri par Insertion

5-      Tri par Sélection

6-      Affichage du contenu de tableau

7-      Ajouter un nouveau Elément au tableau

8-      La Recherche dichotomique

9-      Un Menu de Choix

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181Module module1 Dim tab(9) As Integer Dim pos As Integer Dim element As Integer Sub menu() Console.WriteLine("*MENU DE CHOIX ((((Exercice Tableau))))*") Console.WriteLine(" 1. ***RANDOMIZER*** ") Console.WriteLine(" 2.***TRI_PAR_BULLE*** ") Console.WriteLine(" 3.***AFFICHAGE*** ") Console.WriteLine(" 4.***TRI_PAR_PERMUTATION***") Console.WriteLine(" 5.***TRI_PAR_INSERTION***") Console.WriteLine(" 6.***TRI_PAR_SELECTION***") Console.WriteLine(" 7.***AJOUTER NOUVEAU ELEMENT***") Console.WriteLine(" 8.***RECHERCHE_ DICHOTOMIQUE***") End Sub Sub Main() menu() Dim rep As Boolean Do Dim choise As Integer Console.WriteLine(" QUEL EST VOTRE CHOIX ?") choise = Console.ReadLine Select Case choise Case 1 randomize() Case 2 tri_par_bulle() Case 3 affichage() Case 4 tri_par_permutation() Case 5 tri_par_insertion() Case 6 tri_selection() Case 7 ajout(tab, element, pos) Case 8 recherche_dichotomique() Case Else Console.WriteLine(" CE CHOIX N'EST PAS TRAITé ") End Select Loop While (rep = False) Console.ReadLine() End Sub Sub tri_par_bulle() Dim inversion As Boolean Dim tampon As String Do inversion = False For i As Integer = 0 To 8 If tab(i + 1) tab(i) Then tampon = tab(i) tab(i) = tab(i + 1) tab(i + 1) = tampon inversion = True End If Next Loop Until inversion = False affichage() End Sub Sub affichage() For i = 0 To 9 Console.Write(tab(i) & " ") Next Console.WriteLine() End Sub Sub randomize() For i As Integer = 0 To 9 tab(i) = Rnd() * 100 Next For Each cellule In tab Console.Write(cellule & vbTab) Next End Sub Sub tri_par_permutation() Dim j As Integer Dim tampon As Integer Dim K As Integer For i = 0 To 7 If tab(i + 1) tab(i) Then tampon = tab(i + 1) j = 0 While (j ) And (tab(j) tab(i + 1)) j = j + 1 End While For K = (i + j) To (j + 1) tab(K) = tab(K - 1) Next tab(j) = tampon End If Next affichage() End Sub Sub tri_par_insertion() Dim tampon As Integer For i As Integer = 0 To 8 For j As Integer = i + 1 To 9 If tab(i) > tab(j) Then tampon = tab(i) tab(i) = tab(j) tab(j) = tampon End If Next Next affichage() End Sub Sub recherche_dichotomique() Dim val, mil, debut, fin As Integer Dim find As Boolean find = False debut = 0 fin = 9 Console.WriteLine("entrez l'element recherchez") val = Console.ReadLine While debut = fin And find = False mil = debut + ((fin - debut) / 2) If tab(mil) = val Then find = True Console.WriteLine() ElseIf tab(mil) val Then debut = debut + 1 Else fin = mil - 1 End If End While If find = True Then Console.WriteLine("existe") Else Console.WriteLine("n'existe pas") End If End Sub Sub tri_selection() Dim petit As Integer Dim position As Integer Dim j As Integer Dim i As Integer For i = 0 To 8 petit = tab(i) For j = i To 9 If tab(j) Then petit = tab(j) position = j End If Next For j = position To i + 1 Step -1 tab(j) = tab(j - 1) Next tab(i) = petit Next affichage() End Sub Sub ajout(ByRef tab() As Integer, ByVal pos As Integer, ByVal element As Integer) Dim rep As String Dim i As Integer Console.WriteLine("voulez vous ajouter un nombre o/n ") rep = Console.ReadLine() If rep = "n" Then Console.WriteLine(" merci de votre participation") ElseIf rep = "o" Then Console.WriteLine("entrez l'élement que vous voulez") element = Console.ReadLine ReDim Preserve tab(i + 1) Console.WriteLine(" entrez la possiton que vous voulez") pos = Console.ReadLine pos = element End If affichage() End SubEnd Module
Article publié le 31 Décembre 2011 Mise à jour le Jeudi, 15 Décembre 2022 23:02 par GC Team