Exercice VB: Manipulation de Tableau à une seul dimension
Rédigé par GC Team, Publié le 31 Décembre 2011, Mise à jour le Jeudi, 15 Décembre 2022 23:02Participez au vote ☆☆☆☆☆★★★★★
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
Module 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 < i) 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) < petit 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 Sub End Module |