Exercice VB: Tableau à une seul dimension

Objectif :

Travailler avec les Tableaux

Travail à Faire :

Ecrire le code en VB qui permet de :

  1. Créer un Tableau de 7 colonnes tab(7)
  2. Faire l’affichage du contenu de Tableau
  3. Faire la suppression le contenu d’une colonne du Tableau
  4. Faire l’ajout d’une nouvelle colonne
  5. Quitter le Programme
  6. Créer 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
Module Module1
    Dim O As String
    Dim rep As Char
    Dim nb, x, elem As Integer
    Public tab(7) As Integer
    Public i, j As Integer
    Sub menu()
        Console.WriteLine("1.tableau")
        Console.WriteLine("2.affichage")
        Console.WriteLine("3-suppression")
        Console.WriteLine("4-ajout")
        Console.WriteLine("5-quiter")
    End Sub
    Sub Main()
        Dim choix As String
        Dim rep As String
        Do
            menu()
            Console.WriteLine(" tapez votre choix")
            choix = Console.ReadLine
            Select Case choix
                Case 1
                    ReDim Preserve tab(7)
                    tableau()
                Case 2
                    affichage(tab)
                Case 3
                    Console.WriteLine("saisir l'élément supprimer")
                    Dim elem = Console.ReadLine
                    tab = suppression(tab, elem)
                Case 4
                    ReDim Preserve tab(10)
                    tab = ajout(tab)
                Case 5
                    quiter()
                Case Else
                    Console.WriteLine("le choix que vous avez tapez est introuvable")
            End Select
            Console.WriteLine("voulez-vous contunier ?o/n")
            rep = Console.ReadLine
        Loop Until (rep = "n")
    End Sub
    Sub affichage(ByRef t() As Integer)
        For Each cellule In t
            Console.Write(cellule & vbTab)
        Next
    End Sub
    Function recherche(ByRef t() As Integer, ByVal ele As Integer) As Integer
        For i As Integer = 0 To t.GetUpperBound(0)
            If t(i) = ele Then
                Return i
                Exit For
            Else
            End If
        Next
        Return -1
 
    End Function
 
    Public Function suppression(ByRef t() As Integer, ByVal ele As Integer) As Integer()
        Dim pos = recherche(t, ele)
        If pos  -1 Then
            'For i As Integer = 0 To t.GetUpperBound(0)
            i = 0
            If pos  t.GetUpperBound(0) Then
                Do
                    t(pos + i) = t(pos + i + 1)
                    i += 1
                Loop Until t(pos + i) = t(t.GetUpperBound(0))
            Else
                ReDim Preserve t(t.Length - 2)
                Return t
            End If
        End If
        ReDim Preserve t(t.Length - 2)
        Return t
    End Function
    Public Function ajout(ByRef t() As Integer) As Integer()
        ReDim Preserve tab(10)
        Console.Write("entrez la valeur de l'èlèment à ajoutè")
        nb = Console.ReadLine.ToUpper()
        t(t.GetUpperBound(0)) = nb
        Return t
    End Function
 
    Sub tableau()
        Dim tab(7) As Integer
        For i As Integer = 0 To UBound(tab)
            tab(i) = i
        Next
        For i As Integer = 0 To UBound(tab)
            Console.Out.WriteLine("tab(" + i.ToString + ")=" + tab(i).ToString)
        Next
    End Sub
 
    Public Sub quiter()
        Exit Sub
    End Sub
End Module