Exercice VB: les Instructions Len, Left, Right, Mid

Objectif :
Travaillez avec les Instructions :
- Len()
- Left()
- Right()
- Mid()
Travail à Faire :
Ecrire le code en VB qui permet de :
- Donner le nombre de lettre dans un mot
- Donner le nombre de mots dans une phrase
- Donnez le nombre de voyelles dans une phrase
- D’entrer une nouvelle phrase avec le rangement selon le choix de l'utilisateur
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 |
1. Donner le nombre de lettre dans un mot Module Module1 Sub Main() Dim mot As String Dim nbr As Integer Console.WriteLine("entrer un mot") mot = Console.ReadLine() nbr = Len(mot) Console.WriteLine("le nombre de lettre du mot est=" & Len(mot)) nbr = Console.ReadLine() End Sub End Module 2. Donner le nombre de mots dans une phrase Module phrase Sub Main() Dim ph As String Dim nbr, i As Integer Console.WriteLine("saisir une phrase") ph = Console.ReadLine() nbr = 0 For i = 1 To Len(ph) If Mid(ph, i, 1) = " " Then nbr = nbr + 1 End If Next Console.WriteLine("le nombre de mots de cette fhrase est =" & nbr & " " & "+" & " " & 1 & "=" & nbr + 1) nbr = Console.ReadLine() End Sub End Module 3. Donnez le nombre de voyelles dans une phrase Module voyelle Sub Main() Dim ph As String Dim nb, k As Integer Console.WriteLine("saisir une phrase") ph = Console.ReadLine() nb = 0 For k = 1 To Len(ph) If Mid(ph, k, 1) = "a" Or "i" Or "o" Or "u" Or "y" Or "e" Then nb = nb + 1 End If Next Console.WriteLine("le nombre de voyelles dans cette phrase est=" & nb & "+" & 1 & "=" & nb + 1) nb = Console.ReadLine() Console.ReadLine() End Sub End Module 4. D’entrer une nouvelle phrase avec le rangement selon le choix Module supprimer_char Sub Main() Dim phr As String Dim rang, x, l As Integer Console.WriteLine("saisir une phrase") phr = Console.ReadLine() Console.WriteLine("entrer le rang") rang = Console.ReadLine() l = Len(phr) phr = Left(phr, rang - 1) & Right(phr, l - rang) Console.WriteLine("la nouvelle phrase est=" & Left(phr, rang - 1) & Right(phr, l - rang)) phr = Console.ReadLine() End Sub End Module |
Exercice VB: Tp Gestion Stagiaires Simplifié
Ecrire le Programme qui permet de réaliser l'interfac suivante:
Avant de commancer la réalisation du programme vous devez créer une Base de Donnée nomée " Gestion Stagiaire " qui contient les champs suivant:
Table Stagiaire:

- Numéro :(Numérique)
- Nom : (Chaine de caractères)
- Prénom : (Chaine de caractères)
- Adresse : (Chaine de caractères)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Imports System.Data.SqlClient.SqlDataReader Imports System.Data.SqlClient Module Module1 Public cn As New SqlConnection("data source=Exercicesgratuit\SQLEXPRESS;initial catalog=Exercicesgratuit;integrated security=true") Public cmd As New SqlCommand Public da As SqlDataAdapter Public ds As New DataSet Public dt As New DataTable Public bind As New BindingSource Public dtr As DataRelation Public dr As DataRow Public cm As CurrencyManager Public dr1 As SqlDataReader Sub inni() da = New SqlDataAdapter("select * from sta", cn) da.Fill(ds, "sta") dt = ds.Tables("sta") dt.PrimaryKey = New DataColumn() {dt.Columns.Item(0)} End Sub End Module |
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 |
Imports System.Data.SqlClient Imports System.Data.Common Imports System.Data Imports System Public Class Form1 Dim pos As Integer Dim d As DataRow Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click End End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load inni() ds.Clear() cn.Open() da = New SqlDataAdapter("select * from sta", cn) da.TableMappings.Add("sta", "sta") da.Fill(ds, "sta") bind.DataSource = ds.Tables("sta") DataGridView1.DataSource = bind cn.Close() End Sub Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click txtname.Text = "" txtnom.Text = "" txtpre.Text = "" txtad.Text = "" txtname.Focus() End Sub Sub vider() txtname.Text = "" txtnom.Text = "" txtpre.Text = "" txtad.Text = "" txtname.Focus() End Sub Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click If MsgBox("Voulez vous vraiment quitter l'application", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then End End If End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click Try da.InsertCommand = New SqlCommand("insert into sta (num,nom,prenom,adress) values(@a,@b,@c,@d)", cn) da.InsertCommand.Parameters.Add("@a", SqlDbType.Int, 10, "num") da.InsertCommand.Parameters.Add("@b", SqlDbType.VarChar, 50, "nom") da.InsertCommand.Parameters.Add("@c", SqlDbType.VarChar, 50, "prenom") da.InsertCommand.Parameters.Add("@d", SqlDbType.Text, 50, "adress") d = dt.NewRow d("num") = txtname.Text d("nom") = txtnom.Text d("prenom") = txtpre.Text d("adress") = txtpre.Text Try dt.PrimaryKey = New DataColumn() {dt.Columns.Item(0)} dt.Rows.Add(d) Catch ex As Exception MsgBox("Se Num existe déjà", MsgBoxStyle.Information) Exit Sub End Try da.Update(ds, "sta") MsgBox("Ajouter avec Succée") Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Try da.UpdateCommand = New SqlCommand("update sta set nom=@b,prenom=@c,adress=@d where num=@a", cn) da.UpdateCommand.Parameters.AddWithValue("@a", txtname.Text) da.UpdateCommand.Parameters.AddWithValue("@b", txtnom.Text) da.UpdateCommand.Parameters.AddWithValue("@c", txtpre.Text) da.UpdateCommand.Parameters.AddWithValue("@d", txtad.Text) Try cn.Open() da.UpdateCommand.ExecuteNonQuery() da.Update(ds, "sta") MsgBox("Modification avec succée", MsgBoxStyle.Information) vider() Catch ex As Exception MsgBox(ex.Message) Finally cn.Close() End Try Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click If MsgBox("Voulez vous vraiment supprimer cette Personne de la liste", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then Try Dim dra As DataRow() da.DeleteCommand = New SqlCommand("Delete from sta where num=@a", cn) da.DeleteCommand.Parameters.Add("@a", SqlDbType.Int, 10, "num") dt = ds.Tables("sta") dra = dt.Select("num=" & txtname.Text) Try dt.PrimaryKey = New DataColumn() {dt.Columns.Item(0)} dra(0).Delete() Catch ex As Exception MsgBox("Cette personne n'existe pas", MsgBoxStyle.Information) Exit Sub End Try da.Update(ds, "sta") MsgBox("Suppression avec succéé", MsgBoxStyle.Information) vider() Catch ex As Exception MsgBox(ex.Message) End Try End If End Sub Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click Try Dim str As String = InputBox("Entrer le numéro du Persone à chercher") dr = dt.Rows.Find(str) If dr Is Nothing Then MsgBox("Se personne n'existe pas") Else With dr txtname.Text = .Item(0) txtnom.Text = .Item(1) txtpre.Text = .Item(2) txtad.Text = .Item(3) End With End If Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Sub afficher(ByVal i As Integer) With dt.Rows(i) txtname.Text = .Item(0) txtnom.Text = .Item(1) txtpre.Text = .Item(2) txtad.Text = .Item(3) End With End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If dt.Rows.Count > 0 Then pos = 0 afficher(pos) TextBox5.Text = pos + 1 End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click If pos > 0 Then pos -= 1 afficher(pos) TextBox5.Text = pos + 1 End If End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click If pos < dt.Rows.Count - 1 Then pos += 1 afficher(pos) TextBox5.Text = pos + 1 End If End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click If dt.Rows.Count > 0 Then pos = dt.Rows.Count - 1 afficher(pos) TextBox5.Text = pos + 1 End If End Sub Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click Me.Hide() Form2.Show() End Sub End Class |
Exercice VB: Interface MS Word
Ecrire le programme qui permet de réaliser l'interface suivante:

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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
Imports System.IO Public Class Form1 Public lblrs As Label Private Sub NouveauToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NouveauToolStripMenuItem.Click End Sub Private Sub OuvrirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OuvrirToolStripMenuItem.Click 'Dim opendlg As New OpenFileDialog 'With opendlg ' .InitialDirectory = IO.Directory.GetCurrentDirectory ' If .ShowDialog() = DialogResult.OK Then ' lblrs.Text = "filename = " & .FileName ' Else ' lblrs.Text = "annulé" ' End If 'End With If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then Dim nomfichier As String = OpenFileDialog1.FileName Dim str As StreamReader = New StreamReader(nomfichier) RichTextBox1.Text = str.ReadToEnd str.Close() End If End Sub Private Sub FermerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FermerToolStripMenuItem.Click End End Sub Private Sub EnregestrerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnregestrerToolStripMenuItem.Click FileOpen(1, "stagiaire.txt", OpenMode.Random, OpenAccess.Write, Len(RichTextBox1)) End Sub Private Sub EnregestrerSousToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnregestrerSousToolStripMenuItem.Click SaveFileDialog1.InitialDirectory = Application.ExecutablePath SaveFileDialog1.Filter = "Enregestrer Sous (*.doc)|*.doc|All files (*.*)|*.*" SaveFileDialog1.FilterIndex = 0 If SaveFileDialog1.ShowDialog() = DialogResult.OK Then Dim nomFichier As String = SaveFileDialog1.FileName Dim fichier As StreamWriter = Nothing Try fichier = New StreamWriter(nomFichier) Catch ex As Exception MessageBox.Show("Problème à l'écriture du fichier (" + ex.Message + ")", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error) Return Finally Try fichier.Close() Catch End Try End Try End If End Sub Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click If ColorDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then ' choix d'une couleur de texte RichTextBox1.ForeColor = ColorDialog1.Color ' on change la propriété forecolor du TextBox End If End Sub Private Sub ToolStripButton11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton11.Click If FontDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then RichTextBox1.Font = FontDialog1.Font End If End Sub Private Sub MiseEnPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MiseEnPageToolStripMenuItem.Click Dim pagedlg As New PageSetupDialog With pagedlg Dim pd As New Drawing.Printing.PrintDocument() .Document = pd If .ShowDialog = DialogResult.OK Then With .PageSettings.PaperSize lblrs.Text = String.Format("papier : {0] * {1]", .Width, .Height) End With With .PageSettings.Margins lblrs.Text += String.Format("-marges {0],{1],{2],{3]", .Left, .Top, .Right, .Bottom) End With Else lblrs.Text = "annulé" End If End With End Sub Private Sub CouperToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CouperToolStripMenuItem.Click RichTextBox1.Cut() End Sub Private Sub CopierToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopierToolStripMenuItem.Click RichTextBox1.Copy() End Sub Private Sub CollerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CollerToolStripMenuItem.Click RichTextBox1.Paste() End Sub Private Sub SelectionnerToutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectionnerToutToolStripMenuItem.Click RichTextBox1.SelectAll() End Sub Private Sub EffacerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EffacerToolStripMenuItem.Click RichTextBox1.Clear() End Sub 'Private Sub RechercherToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RechercherToolStripMenuItem.Click ' Dim dlgrecherche As New DialogResult ' Dim i As Integer = RichTextBox1.Text.IndexOf(Text, RichTextBox1.selectionstatr + RichTextBox1.selectionlenght) 'End Sub Private Sub ImprimerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImprimerToolStripMenuItem.Click Dim printdlg As New PrintDialog With printdlg Dim pd As New Drawing.Printing.PrintDocument() .Document = pd .AllowSelection = True .AllowSomePages = True pd.PrinterSettings.MaximumPage = 5 If .ShowDialog = DialogResult.OK Then With pd.PrinterSettings lblrs.Text = String.Format("imprimante : {0]-copier : {1] - page : {2] -{3]", .PrinterName, .Copies, .FromPage, .ToPage) End With Else lblrs.Text = "annuler" End If End With End Sub Private Sub PoliceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PoliceToolStripMenuItem.Click If FontDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then RichTextBox1.Font = FontDialog1.Font End If End Sub Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click Dim opendlg As New OpenFileDialog With opendlg .InitialDirectory = IO.Directory.GetCurrentDirectory If .ShowDialog() = DialogResult.OK Then lblrs.Text = "filename = " & .FileName Else lblrs.Text = "annulé" End If End With End Sub Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click Dim printdlg As New PrintDialog With printdlg Dim pd As New Drawing.Printing.PrintDocument() .Document = pd .AllowSelection = True .AllowSomePages = True pd.PrinterSettings.MaximumPage = 5 If .ShowDialog = DialogResult.OK Then With pd.PrinterSettings lblrs.Text = String.Format("imprimante : {0]-copier : {1] - page : {2] -{3]", .PrinterName, .Copies, .FromPage, .ToPage) End With Else lblrs.Text = "annuler" End If End With End Sub Private Sub ToolStripButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton8.Click RichTextBox1.Cut() End Sub Private Sub ToolStripButton9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton9.Click RichTextBox1.Copy() End Sub Private Sub ToolStripButton10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton10.Click RichTextBox1.Paste() End Sub Private Sub ToolStripButton12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton12.Click RichTextBox1.Undo() End Sub Private Sub ToolStripButton13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton13.Click RichTextBox1.Update() End Sub Private Sub ToolStripButton14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button14.Click Try Dim nouveaustyle As FontStyle = RichTextBox1.SelectionFont.Style If sender.button14.pushed Then nouveaustyle = nouveaustyle Or FontStyle.Bold Else nouveaustyle = nouveaustyle And Not FontStyle.Bold End If RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, nouveaustyle) Catch ex As Exception 'button14.pushed = RichTextBox1.SelectionFont.Bold End Try End Sub Private Sub ToolStripComboBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripComboBox1.Click Dim t As Integer Dim fnt As New Font("arial", 20, FontStyle.Bold) sender.richtextbox1.drawstring(fnt.Style.ToString, fnt, Brushes.Blue, 10, t) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ToolStripComboBox1.Items.Add("Arial") End Sub End Class |
Exercice VB: StreamReader et StreamWriter
Objectif :
Travailler avec :
- StreamReader
- StreamWriter
Travail à Faire :
Ecrire le code en VB qui permet de :
- Chercher si un Fichier existe ou pas S’il n'existe pas. On le crée
- Ouverture du fichier et Ecriture du contenu du fichier sur la console
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 |
Imports System.IO Module Module1 Sub maine() End Sub Sub FichierTexte(ByVal mixt As String) Dim sr As StreamReader Dim sw As StreamWriter Dim sLine As String Try If Not File.Exists(mixt) Then 'Le fichier n'existe pas. On le crée sw = New StreamWriter(mixt) sw.WriteLine("Bonjour. Il nous sommes le {0} et il est {1} ", DateTime.Now.ToLongDateString, DateTime.Now.ToLongTimeString) sw.Close() sw = Nothing End If 'Ouverture du fichier et Ecriture du contenu du fichier sur la console sr = New StreamReader(mixt) Console.WriteLine("Debut du fichier") sLine = sr.ReadLine() While Not sLine Is Nothing Console.WriteLine(sLine) sLine = sr.ReadLine() End While Console.WriteLine("Fin du fichier") Finally 'Fermeture streamreader If Not IsNothing(sr) Then sr.Close() End Try Console.ReadLine() End Sub End Module |
Exercice VB: Carré Magique
Objectif :
Travailler avec les Tableaux à deux dimensions.
Travail à Faire :
1. Ecrire le programme qui détermine le plus grand élément et le petit élément ainsi, la position de plus grand élément et le petit élément d'une matrice.
2. Un carré magique d'ordre n est une matrice carrée n x n telle que la somme des entiers de chaque ligne, chaque colonne et des deux diagonales sont identiques.
Exemple de carré magique d'ordre 3 :
2 |
9 |
4 |
7 |
5 |
3 |
6 |
1 |
8 |
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 |
1. Module Module1 Dim i, j, n, m, Max, Min, Pmax_x, Pmax_y, Pmin_x, Pmin_y As Integer Sub Main() Console.Write("Entrez le nombre de lignes : ") n = Console.ReadLine Console.Write("Entrez le nombre de colonnes : ") m = Console.ReadLine Dim T(n, m) As Integer For i = 0 To n - 1 For j = 0 To m - 1 Console.Write("t(" & i + 1 & " , " & j + 1 & ") = ") T(i, j) = Console.ReadLine Next Next Max = T(0, 0) Min = T(0, 0) Pmax_x = 1 Pmax_y = 1 Pmin_x = 1 Pmin_y = 1 For i = 0 To n - 1 For j = 0 To m - 1 If T(i, j) > Max Then Max = T(i, j) Pmax_x = i + 1 Pmax_y = j + 1 End If If T(i, j) < Min Then Min = T(i, j) Pmin_x = i + 1 Pmin_y = j + 1 End If Next Next Console.WriteLine("Le plus grand élément est : " & Max) Console.WriteLine("Sa postion est : " & "T(" & Pmax_x & ";" & Pmax_y & ")") Console.WriteLine("Le petit élément est : " & Min) Console.WriteLine("Sa postion est : " & "T(" & Pmin_x & ";" & Pmin_y & ")") Console.ReadLine() End Sub End Module 2. Module Module1 Dim n, i, j, s, c, c1, s1, Cmpt As Integer Sub Main() Cmpt = 0 Console.Write("Entrez le nombre de lignes : ") n = Console.ReadLine Dim T(n, n) As Integer For i = 0 To n - 1 For j = 0 To n - 1 Console.Write("T(" & i + 1 & " , " & j + 1 & ") = ") T(i, j) = Console.ReadLine Next Next c1 = 0 s1 = 0 For j = 0 To n - 1 s1 = s1 + T(j, j) c1 = c1 + T(j, n - 1 - j) Next If s1 = c1 Then Cmpt = Cmpt + 2 End If For i = 0 To n - 1 c = 0 s = 0 For j = 0 To n - 1 s = s + T(i, j) c = c + T(j, i) Next If (s = c) And (s = s1) Then Cmpt = Cmpt + 2 End If Next If Cmpt = (2 * n) + 2 Then Console.WriteLine("carré magique") Else Console.WriteLine("carré pas magique") End If Console.ReadLine() End Sub End Module |
Exercice VB: Transférer une matrice à deux dimension
Objectif :
Travailler avec les Tableaux à deux dimensions.
Travail à Faire :
Ecrire un programme qui transfère une matrice M à deux dimension L et C (dimensions maximales : 10 lignes et 10 colonnes) dans un tableau V à une dimension L * C.
Exemple :
/ \
| a b c d | / \
| e f g h | =======> | a b c d e f g h i j k l |
| i j k l | \ /
\ /
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 |
Module Module1 Dim i, j, L, C, x, y As Integer Sub Main() Console.Write(" L : ") L = Console.ReadLine Console.Write(" C : ") C = Console.ReadLine While (L > 10 Or C > 10) Or (L < 0 Or C < 0) Console.WriteLine("SVP, L et C devent être compris entre 0 et 10") Console.Write(" L : ") L = Console.ReadLine Console.Write(" C : ") C = Console.ReadLine End While Dim M(L, C) As String Dim V(L * C) As String x = 0 y = L * C For i = 0 To L - 1 For j = 0 To C - 1 Console.Write("M(" & i + 1 & " ; " & j + 1 & ") = ") M(i, j) = Console.ReadLine Next Next For i = 0 To L - 1 For j = 0 To C - 1 V(x) = M(i, j) x += 1 Next Next For i = 0 To y - 1 Console.Write(V(i) & " ") Next Console.ReadLine() End Sub End Module |
Exercice VB: Gestion d'Article
Ecrire le code VB qui permet de :
1- Faire l’écriture des données d'un Article dans un Fichier
2- Faire la lecture des données d'un Article dans un Fichier
3- Faire l’ajout d’un article
4- Faire la suppression d’un article
5- Copier des données d'un Article dans un Fichier
6- Quitter
7- Un menu de Choix
Un Produit est connu par :
- Le numéro de référence
- La désignation de produit
- Le type de produit (A, B ou C)
- Le prix unitaire
- La Quantité
- Le prix totale en TTC
a. Si le type est A alors la TVA est 20%
b. Si le type est B alors la TVA est 14%
c. Si le type est C alors la TVA est 7%
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 182 183 184 185 186 |
Imports System Module Module1 Public tcl() As article Public O As Char Public nb As Integer Public Structure article (4)> Public ref As Integer (12)> Public desig As String (2)> Public prix As Double (10)> Public typ As String (12)> Public tva As Double (5)> Public qut As Integer (10)> Public ttc As Double End Structure Sub menu() Console.WriteLine("1.ecriture") Console.WriteLine("2.lecture") Console.WriteLine("3.ajout") Console.WriteLine("4.suppression") Console.WriteLine("5.copier") Console.WriteLine("6.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 ecriture() Case 2 lecture(tcl) Case 5 copier() Case 6 quiter() Case 3 'ReDim Preserve tab(tab.GetUpperBound(0) + 1) ajout() Case 4 Console.WriteLine("saisir l'élément supprimer") Dim elem = Console.ReadLine suppression(tcl, elem) 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 Public Sub lecture(ByRef tcl() As article) For i = 0 To tcl.GetUpperBound(0) With tcl(i) Console.WriteLine(tcl(i).ref & vbTab & tcl(i).desig & vbTab & tcl(i).typ & vbTab & tcl(i).prix & vbTab & tcl(i).qut & vbTab & tcl(i).tva & vbTab & tcl(i).ttc) End With Next End Sub Public Sub ecriture() Console.WriteLine("nombre d'article") nb = Console.ReadLine ReDim tcl(nb - 1) For i = 0 To tcl.GetUpperBound(0) With tcl(i) Console.WriteLine("entrez le numero de reference") .ref = Console.ReadLine Console.WriteLine("entrez la designation de produit") .desig = Console.ReadLine Console.WriteLine("entrez le type(A,B ou C) de produit ") .typ = Console.ReadLine Console.WriteLine("entrez le prix unitaire du produit") .prix = Console.ReadLine Console.WriteLine("entrez la Quantité du produit") .qut = Console.ReadLine If .typ = "A" Then .tva = 20% ElseIf .typ = "B" Then .tva = 14% ElseIf .typ = "C" Then .tva = 7% End If .ttc = (CDbl(.prix) * CDbl(.qut)) + (CDbl(.prix) * (CDbl(.qut) * 0.07)) End With Next End Sub Sub ajout() ReDim Preserve tcl(tcl.GetUpperBound(0) + 1) With tcl(tcl.GetUpperBound(0)) Console.WriteLine("entrez le numero de reference") .ref = Console.ReadLine Console.WriteLine("entrez la designation de produit") .desig = Console.ReadLine Console.WriteLine("entrez le type(A,B ou C) de produit ") .typ = Console.ReadLine Console.WriteLine("entrez le prix unitaire du produit") .prix = Console.ReadLine Console.WriteLine("entrez la Quantité du produit") .qut = Console.ReadLine If .typ = "A" Then .tva = 20% ElseIf .typ = "B" Then .tva = 14% ElseIf .typ = "C" Then .tva = 7% End If .ttc = (CDbl(.prix) * CDbl(.qut)) + (CDbl(.prix) * (CDbl(.qut) * 0.07)) End With End Sub Function recherche_par_ref(ByVal tab() As article, ByVal ref As Integer) As Integer For i = 0 To tab.GetUpperBound(0) Next End Function Public Function suppression(ByRef t() As article, ByVal ele As Integer) Dim i As Integer Dim pos = recherche(t, ele) If pos -1 Then i = 0 If pos t.GetUpperBound(0) Then Do t(pos + i) = t(pos + i + 1) i += 1 Loop Until t(pos + i).ref = t(t.GetUpperBound(0)).ref Else ReDim Preserve t(t.Length - 1) Return t End If End If ReDim Preserve t(t.Length - 1) Return t With (i) Console.WriteLine("entrez le numero de reference") tcl(i).ref = Console.ReadLine Console.WriteLine("entrez la designation de produit") tcl(i).desig = Console.ReadLine Console.WriteLine("entrez le type(A,B ou C) de produit ") tcl(i).typ = Console.ReadLine Console.WriteLine("entrez le prix unitaire du produit") tcl(i).prix = Console.ReadLine Console.WriteLine("entrez la Quantité du produit") tcl(i).qut = Console.ReadLine If tcl(i).typ = "A" Then tcl(i).tva = 20% tcl(i).ttc = (CDbl(tcl(i).prix) * CDbl(tcl(i).qut)) + (CDbl(tcl(i).prix) * ((CDbl(tcl(i).qut)) * 0.2)) ElseIf tcl(i).typ = "B" Then tcl(i).tva = 14% tcl(i).ttc = (CDbl(tcl(i).prix) * CDbl(tcl(i).qut)) + (CDbl(tcl(i).prix) * (CDbl(tcl(i).qut) * 0.14)) ElseIf tcl(i).typ = "c" Then tcl(i).tva = 7% tcl(i).ttc = (CDbl(tcl(i).prix) * CDbl(tcl(i).qut)) + (CDbl(tcl(i).prix) * (CDbl(tcl(i).qut) * 0.07)) End If End With End Function Function recherche(ByRef t() As article, ByVal ele As Integer) As Integer For i As Integer = 0 To t.GetUpperBound(0) If t(i).ref = ele Then Return i Exit Function Else End If Next Return -1 End Function Sub copier() FileOpen(1, "fichier.txt", OpenMode.Output) For i = 0 To nb - 1 PrintLine(1, tcl(i).ref & "/" & tcl(i).desig & "/" & tcl(i).typ & "/" & tcl(i).prix & "/" & tcl(i).tva & "/" & tcl(i).ttc) Next FileClose(1) FileOpen(1, "fichier.txt", OpenMode.Input) While Not EOF(1) Console.WriteLine(LineInput(1)) End While FileClose(1) End Sub Public Sub quiter() Exit Sub End Sub End Module |
Exercice VB: TP Distances inter-atomiques
On se propose d'écrire un programme permettant le calcul de la longueur d'une liaison entre deux atomes dans un matériau, à partir des paramètres de maille (a, b, c, a , b , g ) et des coordonnées atomiques (x, y, z). Pour cela, on transforme les coordonnées (x,y,z) de chaque atome en coordonnées cartésiennes (Xt, Yt, Zt) de la façon suivante:

Une fois la transformation faite, l'expression pour la distance entre les atomes A1 et A2 est simplement:

Programme demandé :
- Interface (voir exemple ci-dessous):
- Six zones de texte pour saisir les paramètres de maille (a, b, c, a , b , g )
- Six autres pour la saisie des coordonnées (x, y, z) des atomes A1 et A2
- Un bouton Calcul
- Six zones de texte pour afficher les coordonnées pour A1 et A2 après transformation – repère cartésien
- Une zone de texte pour l'affichage de la distance calculée entre A1 et A2 dans le nouveau repère
- Vous devrez écrire trois fonctions à placer de préférence dans un module (Public Function) :
- DegRad, la première, aura pour simple objectif de convertir des degrés en radians. Elle recevra donc un seul argument de type double pour retourner une valeur du même type.
- MTransform recevra les paramètres de la maille et les coordonnées d'un atome exprimées dans ce système. Le but de cette fonction est de retourner les coordonnées de cet atome transformées selon la formule expliquée plus haut.
- Distance, enfin, retournera la distance entre les deux atomes dont les coordonnées respectives lui auront été passées en arguments. Le résultat de ce calcul sera de type double.
- Le bouton Calcul ordonnera successivement les trois taches suivantes :
- Lecture des valeurs des paramètre de la maille avec conversion des angles (a , b et g ) en radians à l'aide de DegRad. Lecture des coordonnées (x, y, z) des atomes A1 et A2.
- Transformation des coordonnées de A1 et A2 dans le nouveau repère – à l'aide de MTransform. Affichage des nouvelles coordonnées.
- Calcul de la distance et affichage

Interface graphique :
Voici les étapes à suivre pour construire l’interface

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 |
Code de la feuille Form1 : Private Sub Command1_Click() 'Lecture des valeurs M.a = Val(Ta.Text) M.b = Val(Tb.Text) M.c = Val(Tc.Text) M.alpha = degrad(Val(Talpha.Text)) M.beta = degrad(Val(Tbeta.Text)) M.gamma = degrad(Val(Tgamma.Text)) A1.x = Val(TA1x.Text) A1.y = Val(TA1y.Text) A1.z = Val(TA1z.Text) A2.x = Val(TA2x.Text) A2.y = Val(TA2y.Text) A2.z = Val(TA2z.Text) 'Transformation des coordonnées At1 = MTransform(M, A1) At2 = MTransform(M, A2) 'Affichage des coordonnées transformées TTA1x.Text = Str(At1.x) TTA1y.Text = Str(At1.y) TTA1z.Text = Str(At1.z) TTA2x.Text = Str(At2.x) TTA2y.Text = Str(At2.y) TTA2z.Text = Str(At2.z) 'calcul de la distance et affichage D = Distance(At1, At2) Tdistance = Str(D) End Sub Code à placer dans un module : Public Type XYZ x As Double y As Double z As Double End Type Public Type Maille_type a As Double b As Double c As Double alpha As Double beta As Double gamma As Double End Type Public A1 As XYZ, A2 As XYZ Public At1 As XYZ, At2 As XYZ Public D As Double Public M As Maille_type Public Const Pi = 3.14159265358979 Public Function MTransform(M As Maille_type, Atom As XYZ) As XYZ Dim V As Double V = M.a * M.b * M.c * _ Sqr(1 - Cos(M.alpha) ^ 2 - Cos(M.beta) ^ 2 - Cos(M.gamma) ^ 2 + 2 * Cos(M.alpha) * Cos(M.beta) * Cos(M.gamma)) MTransform.x = M.a * Atom.x + _ M.b * Cos(M.gamma) * Atom.y + _ M.c * Cos(M.beta) * Atom.z MTransform.y = M.b * Sin(M.gamma) * Atom.y + _ M.c * (Cos(M.alpha) - Cos(M.beta) * Cos(M.gamma)) * Atom.z / Sin(M.gamma) MTransform.z = V * Atom.z / (M.a * M.b * Sin(M.gamma)) End Function Public Function Distance(A1 As XYZ, A2 As XYZ) As Double Distance = Sqr((A1.x - A2.x) ^ 2 + (A1.y - A2.y) ^ 2 + (A1.z - A2.z) ^ 2) End Function Public Function degrad(D As Double) As Double 'Convertion degrés >>> radians degrad = D * Pi / 180# End Function |
Exercice VB: Examen Fin de Formation TSDI 2008 variante 5
La société Télé-Commerce souhaite mettre en place un système de vente sur Internet, vous étés maintenant chargé de développer cette application en respectant le modèle relationnel suivant :
CLIENTS (NumClient, Nom, Prenom, Ville, Tele).
COMMANDES (NumCommande, DateCommande, NumProduit,, Quantité).
ETAT _COMMANDE(NumCommand, NumClient , EtatCommande).
PRODUITS (N°Produit, Libellé, PrixUnitaire, TVA).
Une fois le client fait le règlement de la commande, les produits commandés seront livrés dans un délais ne dépassant pas 48h.
EtatCommande : décrit l‘état de la commande (En cours, valider, annuler)
TVA : 20%
Travail à faire :
- Créer la base de données sous SQL SERVER (2 Pts)
- Faire quelques enregistrements pour le test
- Créer une interface de mise à jour des Client (4 Pts)
- Ajout (le contrôle de saisie est obligatoire et confirmation d’ajout)
- Modification
- Suppression
- Boutons de navigation
- Créer une interface de mise à jour des Produit (2 Pts)
- Créer une interface de mise à jour des Commande et Etat_Commande (4 Pts)
- Créer une feuille de recherche :
- Commandes passés par un client (1 Pt)
- Recherche les commandes validées entre deux dates (1 Pt)
- Sachant qu’une remise de 3% sur tous les produits est accordée aux clients ayant passé plus de 5 commandes. Faire les modification nécessaires sur l’interface Commande (2 Pts)
- Créer un état pour afficher les commandes passées entre deux dates et l’état de chaque commande (3 Pts)
- Créer un état permettant de lister les commandes passée durant le mois en cours et le montant total des commandes (3Pts)
- Créer une page web permettant d’afficher la liste des produits (2 Pt)
- Créer une page permettant de mettre à jour la table Produits (2 Pts)
- Créer une page permettant aux clients de passer des commandes via Internet en choisissant des produit , l’état de chaque commande passée depuis le site web doit être en cours de traitement. L’affichage du montant total de la commande est obligatoire (3 Pts)
- Créer une page permettant d’afficher la liste des commandes en cours de traitement avec leurs montants (3 Pts)
- Créer une interface pour mettre à jour la table Produits (JAVA/JDBC) :
- Ajout (2 Pts)
- Modification (2 Pts)
- Suppression (2 Pts)
- Recherche (2 Pts)
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 |
Imports System.Data.SqlClient Public Class Client Private Sub Client_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim f As String = "select * from client" adp = New SqlDataAdapter(f, con) adp.Fill(dset, "client") tabcl = dset.Tables("client") tabcl.PrimaryKey = New DataColumn() {tabcl.Columns.Item(0)} DataGridView1.DataSource = tabcl End Sub Private Sub affichage(ByVal pos As Integer) With tabcl.Rows(pos) Numclient.Text = .Item(0) nomcl.Text = .Item(1) prenomcl.Text = .Item(2) villecl.Text = .Item(3) telephonecl.Text = .Item(4) End With End Sub Private Sub mise() Dim sqcmbui As New SqlCommandBuilder(adp) adp.Update(tabcl) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Numclient.Clear() nomcl.Clear() prenomcl.Clear() villecl.Clear() telephonecl.Clear() Numclient.Focus() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Try Dim r As DataRow r = tabcl.NewRow() With r .Item(0) = Numclient.Text .Item(1) = nomcl.Text .Item(2) = prenomcl.Text .Item(3) = villecl.Text .Item(4) = telephonecl.Text End With tabcl.Rows.Add(r) mise() MsgBox("ajout avec succ") Button1_Click(sender, e) Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Try Dim r As DataRow r = tabcl.Rows.Find(Numclient.Text) r.Delete() mise() MsgBox("supprimer aver succé") Button1_Click(sender, e) Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Try Dim r As DataRow r = tabcl.Rows.Find(Numclient.Text) With r .Item(0) = Numclient.Text .Item(1) = nomcl.Text .Item(2) = prenomcl.Text .Item(3) = villecl.Text .Item(4) = telephonecl.Text End With mise() MsgBox("modification avec succé") Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click pos = 0 affichage(pos) End Sub Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Try If pos > 0 Then pos -= 1 affichage(pos) Else MsgBox("c'est le premier") End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click Try If pos < tabcl.Rows.Count Then pos += 1 affichage(pos) Else MsgBox("c'est le dérnier") End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click pos = tabcl.Rows.Count - 1 affichage(pos) End Sub Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click Me.Hide() f1.Show() End Sub End Class --------------------------------------------------------------------------- Imports System.Data.SqlClient Public Class Commande Private Sub Commande_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO : cette ligne de code charge les données dans la table 'Gestion_commandeDataSet.Produits'. Vous pouvez la déplacer ou la supprimer selon vos besoins. Me.ProduitsTableAdapter.Fill(Me.Gestion_commandeDataSet.Produits) Dim f As String = "select * from commandes" adpCm = New SqlDataAdapter(f, con) adpCm.Fill(dset, "commandes") tabcom = dset.Tables("commandes") tabcom.PrimaryKey = New DataColumn() {tabcom.Columns.Item(0)} DataGridView1.DataSource = tabcom End Sub Private Sub affichage(ByVal pos As Integer) With tabcom.Rows(pos) Numcom.Text = .Item(0) DateCom.Text = .Item(1) NumPr.Text = .Item(2) Qunt.Text = .Item(3) End With End Sub Private Sub mise() Dim sqcmbui As New SqlCommandBuilder(adpCm) adpCm.Update(tabcom) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Numcom.Clear() DateCom.Clear() 'NumPr.Clear() Qunt.Clear() Numcom.Focus() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Try Dim r As DataRow r = tabcom.NewRow() With r .Item(0) = Numcom.Text .Item(1) = DateCom.Text .Item(2) = NumPr.Text .Item(3) = Qunt.Text End With tabcom.Rows.Add(r) mise() MsgBox("ajout avec succ") Button1_Click(sender, e) Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Try Dim r As DataRow r = tabcom.Rows.Find(Numcom.Text) r.Delete() mise() MsgBox("supprimer aver succé") Button1_Click(sender, e) Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Try Dim r As DataRow r = tabcom.Rows.Find(Numcom.Text) With r .Item(0) = Numcom.Text .Item(1) = DateCom.Text .Item(2) = NumPr.Text .Item(3) = Qunt.Text End With mise() MsgBox("modification avec succé") Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click pos = 0 affichage(pos) End Sub Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Try If pos > 0 Then pos -= 1 affichage(pos) Else MsgBox("c'est le premier") End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click Try If pos < tabcom.Rows.Count Then pos += 1 affichage(pos) Else MsgBox("c'est le dérnier") End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click pos = tabcom.Rows.Count - 1 affichage(pos) End Sub Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click Me.Hide() f1.Show() End Sub End Class ---------------------------------------------------------------------------- Imports System.Data.SqlClient Public Class commande_entre_2_dates Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim h As String Dim o, b As Date o = TextBox1.Text b = TextBox2.Text h = " select * from Commandes where DateCommande Between '" & o & "' and '" & b & "'" Dim command1 As New SqlCommand(h, con) Try con.Open() Dim k As SqlDataReader = command1.ExecuteReader Dim j As String Do While k.Read() j = Str(k.GetValue(0)) + " " + k.GetValue(1) + " " + Str(k.GetValue(2)) + " " + Str(k.GetValue(3)) L1.Items.Add(j) Loop k.Close() con.Close() Catch ex As SqlException MsgBox(ex.Message) End Try End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click TextBox1.Text = "" TextBox2.Text = "" TextBox1.Focus() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click f1.Show() Me.Hide() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click End End Sub End Class ------------------------------------------------------------------------------ Imports System.Data.SqlClient Public Class EtatCommande Private Sub EtatCommande_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO : cette ligne de code charge les données dans la table 'Gestion_commandeDataSet3.Client'. Vous pouvez la déplacer ou la supprimer selon vos besoins. Me.ClientTableAdapter.Fill(Me.Gestion_commandeDataSet3.Client) 'TODO : cette ligne de code charge les données dans la table 'Gestion_commandeDataSet3.Commandes'. Vous pouvez la déplacer ou la supprimer selon vos besoins. Me.CommandesTableAdapter.Fill(Me.Gestion_commandeDataSet3.Commandes) Dim f As String = "select * from Etatcommande" adpEcm = New SqlDataAdapter(f, con) adpEcm.Fill(dset, "Etatcommande") tabecom = dset.Tables("Etatcommande") tabecom.PrimaryKey = New DataColumn() {tabecom.Columns.Item(0)} DataGridView1.DataSource = tabecom End Sub Private Sub affichage(ByVal pos As Integer) With tabecom.Rows(pos) Numcom.Text = .Item(0) NumCl.Text = .Item(1) EtatCom.Text = .Item(2) End With End Sub Private Sub mise() Dim sqcmbui As New SqlCommandBuilder(adpEcm) adpEcm.Update(tabcom) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Me.Numcom.Clear() 'NumCl.Clear() 'EtatCom.Clear() Numcom.Focus() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Try Dim r As DataRow r = tabecom.NewRow() With r .Item(0) = Numcom.Text .Item(1) = NumCl.Text .Item(2) = EtatCom.Text End With tabecom.Rows.Add(r) mise() MsgBox("ajout avec succ") Button1_Click(sender, e) Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Try Dim r As DataRow r = tabecom.Rows.Find(Numcom.Text) r.Delete() mise() MsgBox("supprimer aver succé") Button1_Click(sender, e) Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Try Dim r As DataRow r = tabecom.Rows.Find(Numcom.Text) With r .Item(0) = Numcom.Text .Item(1) = NumCl.Text .Item(2) = EtatCom.Text End With mise() MsgBox("modification avec succé") Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click pos = 0 affichage(pos) End Sub Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Try If pos > 0 Then pos -= 1 affichage(pos) Else MsgBox("c'est le premier") End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click Try If pos < tabecom.Rows.Count Then pos += 1 affichage(pos) Else MsgBox("c'est le dérnier") End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click pos = tabecom.Rows.Count - 1 affichage(pos) End Sub Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click Me.Hide() f1.Show() End Sub End Class -------------------------------------------------------------------------------- Imports System.Data.SqlClient Public Class Produit Private Sub Produit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim f As String = "select * from produits" adpPr = New SqlDataAdapter(f, con) 'pour inisialiser adpter Produit adpPr.Fill(dset, "produits") tabpr = dset.Tables("produits") tabpr.PrimaryKey = New DataColumn() {tabpr.Columns.Item(0)} DataGridView1.DataSource = tabpr End Sub Private Sub mise() Dim sqcom As New SqlCommandBuilder(adpPr) adpPr.Update(tabpr) End Sub Private Sub affichage(ByVal pos As Integer) With tabpr.Rows(pos) N_pr.Text = .Item(0) Libelli.Text = .Item(1) PrixU.Text = .Item(2) Tva.Text = .Item(3) End With End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click N_pr.Clear() Libelli.Clear() PrixU.Clear() Tva.Clear() N_pr.Focus() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Try Dim r As DataRow r = tabpr.NewRow With r .Item(0) = N_pr.Text .Item(1) = Libelli.Text .Item(2) = PrixU.Text .Item(3) = Tva.Text End With tabpr.Rows.Add(r) mise() MsgBox("ajout avec succée") Button1_Click(sender, e) Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Try Dim r As DataRow r = tabpr.Rows.Find(N_pr.Text) With r .Item(0) = N_pr.Text .Item(1) = Libelli.Text .Item(2) = PrixU.Text .Item(3) = Tva.Text End With mise() Button1_Click(sender, e) MsgBox("modifier avec succée") Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Try Dim r As DataRow r = tabpr.Rows.Find(N_pr.Text) r.Delete() mise() Button1_Click(sender, e) MsgBox("suppression avec succée") Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click 'If pos > 0 Then pos = 0 affichage(pos) 'End If End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click pos = tabpr.Rows.Count - 1 affichage(pos) End Sub Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Try If pos > 0 Then pos -= 1 affichage(pos) Else MsgBox("c'est le premier") End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click Try If pos < tabpr.Rows.Count - 1 Then pos += 1 affichage(pos) Else MsgBox("c'est le dérnier") End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click Me.Hide() f1.Show() End Sub End Class ----------------------------------------------------------------------------- Imports System.Data.SqlClient Public Class Recherche_Com_cli Private Sub Recherche_Com_cli_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO : cette ligne de code charge les données dans la table 'Gestion_commandeDataSet3.Client'. Vous pouvez la déplacer ou la supprimer selon vos besoins. Me.ClientTableAdapter.Fill(Me.Gestion_commandeDataSet3.Client) End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.Hide() f1.Show() End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click 'con.Close() Dim z As String Dim test As Integer test = 0 z = "select NumClient from Client where NumClient=" & Val(T1.Text) & "" Dim command As New SqlCommand(z, con) Try con.Open() Dim s As SqlDataReader = command.ExecuteReader Do While s.Read() Dim p As Integer p = Str(s.GetValue(0)) If T1.Text = p Then test = 1 End If Loop s.Close() con.Close() Catch ex As Exception MsgBox(ex.Message) End Try If test = 1 Then Dim l As String l = "select * from etatCommande where NumClient =" & Val(T1.Text) & "" Dim command1 As New SqlCommand(l, con) Try con.Open() Dim f As SqlDataReader = command1.ExecuteReader Do While f.Read() Dim p As String p = Str(f.GetValue(0)) + " " + f.GetValue(1) + " " + Str(f.GetValue(2)) L1.Items.Add(p) Loop f.Close() con.Close() Catch ex As Exception MsgBox(ex.Message) End Try Else MsgBox("le numéro de ce client n'existe pas") End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Hide() f1.Show() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click End End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click t1.Text = "" L1.Items.Clear() t1.Focus() End Sub End Class ---------------------------------------------------------------------------- Imports System.Data.SqlClient Module tous Public con As New SqlConnection("server=(local);integrated security=SSPI;database=gestion commande") Public dset As New DataSet Public adp, adpPr, adpCm, adpEcm As New SqlDataAdapter Public tabcl, tabpr, tabcom, tabecom As New DataTable Public pos As Integer Public f1 As New Form1 Public f2 As New Client Public f3 As New Produit Public f4 As New Commande Public f5 As New EtatCommande Public f6 As New Recherche_Com_cli Public f7 As New commande_entre_2_dates End Module |
Exercice VB: Les suites Mathématique addition
Objectif :
Travailler avec les Suites et Fonctions.
Travail à Faire :
1. Donnez un programme pour calculer :
S = 1 – 1/2 + 1/3 – 1/4 + …… –1/2n + 1/2n + 1
2. Donnez un programme pour calculer :
S = 1 + 1/2 + 2/3 + 3/4 + ………
3. Donnez un programme pour calculer :
S = 1 – 1/3 + 1/4 – 1/6 + 1/7 + ………
4. Donnez un programme pour calculer :
S = X1 + X3 /3 + X5 /5 + ……. + X2n+1/2n+1
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 |
1. Module Module1 Dim N, i, S As Double Sub Main() Console.Write("Entrez la valeur de N : ") N = Console.ReadLine S = 1 For i = 1 To N S = S - (1 / (2 * i)) + (1 / (2 * i + 1)) Next Console.Write("S est : " & S) Console.ReadLine() End Sub End Module 2. Module Module1 Dim N, i, S As Double Sub Main() Console.Write("Entrez la valeur de N : ") N = Console.ReadLine S = 1 For i = 1 To N S = S + (i / (i + 1)) Next Console.Write("S est : " & S) Console.ReadLine() End Sub End Module 3. Module Module1 Dim N, i, S As Double Sub Main() Console.Write("Entrez la valeur de N : ") N = Console.ReadLine S = 1 For i = 1 To N S = S - (1 / (3 * i)) + (1 / (3 * i + 1)) Next Console.Write("S est : " & S) Console.ReadLine() End Sub End Module 4. Module Module1 Dim P, i, S, X As Double Sub Main() Console.Write("Entrez la valeur de X : ") X = Console.ReadLine Console.Write("Entrez la puissance : ") P = Console.ReadLine S = 0 For i = 0 To P S = S + (X ^ (2 * i + 1)) / (2 * i + 1) Next Console.Write("S est : " & S) Console.ReadLine() End Sub End Module |