| Outils logiciels pour les cours Paris II 
 Cours Paris II 
 Stages/ Thèses/ Séminaires | TD 10
 On lit des fichiers F1.doc,....Fk.doc et on construit un vecteur pour chacun d'entre eux. On lit des mots clés M1,...,Ml et on cherche les fichiers les plus proches de ces mots. Tous les 4 sous-projets peu être réalisés indépendamment. 
 
 A l'intérieur du rectangle déterminé par les points (1,1) et (m,m). La valeur de m est entrée par le formulaire ainsi que le nombre d’itérations. Attention au tirage qui doit être uniforme. La fonction Rnd() (ou Rnd) crée une valeur uniforme entre 0 et 1. L = CInt((Rnd * 4) + 0.5) crée une valeur 1,2,3,4 avec probabilité 1/4. L = CInt((Rnd * 4) + 0.3) crée une valeur 1,2,3,4 avec probabilité non uniforme. Le programme est décomposé en 2 boutons et un champs texte:  Private Sub CommandButton1_Click()
 ' Bouton d'itération   i: entre 0 et m
 ' Bouton d'itération   j: entre 0 et m
 ' On affiche le point i+1, j+1 dans le carré 1,m-1
 i = Cells(2, 1)
 j = Cells(1, 2)
 k = 1
 m = Cells(2, 2)
 Do While k <= Cells(1, 1) + 1
 ' Tirage aléatoire
 Randomize
 L = CInt((Rnd * 4) + 0.5)
 '  
 If L = 1 Then i = Abs(i - 1)
 If L = 2 Then i = i + 1
 If L = 3 Then j = Abs(j - 1)
 If L = 4 Then j = j + 1
 If i > m Then i = m - 1
 If j > m Then j = m - 1
 Cells(i + 1, j + 1).Select
    With Selection.Interior
        .ColorIndex = 8
        .Pattern = xlSolid
    End With
 k = k + 1
 Loop
 Cells(2, 1) = i
 Cells(1, 2) = j
 End Sub
 Private Sub CommandButton2_Click()
 ' Bouton QUITTER
 ' Masquer Userform1
   UserForm1.Hide
 ' Récupérer la mem. occupée par userform1
   Unload UserForm1
 End Sub
 Private Sub TextBox1_Change()
 Cells(1, 1) = TextBox1.Value
 End Sub
 Private Sub TextBox2_Change()
 Cells(2, 2) = TextBox2.Value
 End Sub
 Cette subroutine est essentielle pour tous les projets qui visent à traiter des mots. Supposons que l’on place dans la colonne A un tableau de N mots triés, à partir de la ligne 2. La valeur de N est dans la cellule B1. Exemple : N=4 et dans la colonne1: ab bb nn zz On souhaite lire un mot à l’aide d’un formulaire. Si ce mot est dans le tableau, on afficher un message MsgBox ("Mot trouvé"). Sinon, on insère le mot dans le tableau, à l’aide de l’instruction Selection.EntireRow.Insert L’insertion se fait au-dessus du mot sélectionné. Le tableau résultant doit TOUJOURS être trié. La recherche se fait en comparant le mot s à insérer au point milieu du tableau (approximativement Cells(N/2,1)), puis en divisant l’intervalle de recherche par 2 à chaque itération. Concevoir un Formulaire avec une zone de texte, et deux boutons (Next, Stop) sur le modèle ci-dessous. Le Bouton 1 est NEXT et parcourt dichotomiquement le tableau. L’état du tableau est défini par le triplet (Bottom, i, N) qui décrit la decomposition possible en deux sous-tableaux. On s’arrête lorque N=i+1. Une difficulté est que soit Bottom+1=i, soit Bottom+2=i. Si s est dans la partie basse, il faut donc comparer s à Cells(Bottom), Cells (Bottom+1) et peut-être Cellls(Bottom+2). Ce qui complique légèrement le programme.  Private Sub CommandButton1_Click()
 s = Cells(1, 3)
 '
 ' Recherche S entre 1 et N de la colonne A2....AN+1.
 ' NT est le nombre d'éléments après la subroutine: NT=NT+1 (insertion) ou NT=N
 '
 '
 N = Cells(1, 2)
 NT = N
 Bottom = 1
 k = 1
 I = Bottom + CInt((N - Bottom) / 2)
 Cells(2, 3) = Bottom
 Cells(2, 4) = I
 Cells(2, 5) = N
 '
 ' Tant que La taille (N-i) du sous-tableau est > 2 on divise '
 '
 Do While (N - I > 1)
    If s < Cells(I + 1, 1) Then
    N = I
    ElseIf s = Cells(I + 1, 1) Then MsgBox ("Egalité")
    GoTo ExitEgalité
    Else: Bottom = I
    End If
 I = Bottom + CInt((N - Bottom) / 2)
 Cells(2 + k, 3) = Bottom
 Cells(2 + k, 4) = I
 Cells(2 + k, 5) = N
 k = k + 1
 Loop
 '
 ' La taille (N-i) du sous-tableau supérieur est maintenant = 2 donc N=i+1 ' Il faut regarder si s est dans le sous tableau inférieur de taille 2 ou 3.
 '
 If s = Cells(Bottom + 1, 1) Or s = Cells(Bottom + 2, 1) Or s = Cells(Bottom + 3, 1) Then
 MsgBox ("Egalitélimite")
 GoTo ExitEgalité
 ElseIf s < Cells(Bottom + 1, 1) Then Cells(Bottom + 1, 1).Select
 Selection.EntireRow.Insert
 Cells(Bottom + 1, 1) = s
 NT = NT + 1
 GoTo ExitEgalité
 ElseIf s < Cells(Bottom + 2, 1) Then Cells(Bottom + 2, 1).Select
 Selection.EntireRow.Insert
 Cells(Bottom + 2, 1) = s
 NT = NT + 1
 GoTo ExitEgalité
 ElseIf s < Cells(Bottom + 3, 1) Then
 Cells(Bottom + 3, 1).Select
 Selection.EntireRow.Insert
 Cells(Bottom + 3, 1) = s
 NT = NT + 1
 GoTo ExitEgalité
 End If
 ' Ou si s est dans le tableau supérieur
 ' On insère une nouvelle ligne si nécessaire et place s '
 ' Attention : il faut un saut de ligne après le Then…..du If   (ce problème créait les difficultés lors du TD du 14
 ' Novembre). Une erreur
  ' de compilation était générée au niveau du ElseIf……
 If s = Cells(I + 1, 1) Or s = Cells(N + 1, 1) Then
 MsgBox ("Egalitélimite")
 GoTo ExitEgalité
 ElseIf s < Cells(I + 1, 1) Then Cells(I + 1, 1).Select
 Selection.EntireRow.Insert
  Cells(I + 1, 1) = s
 NT = NT + 1
 GoTo ExitEgalité
 ElseIf s < Cells(N + 1, 1) Then Cells(N + 1, 1).Select
 Selection.EntireRow.Insert
 Cells(N + 1, 1) = s
 NT = NT + 1
 GoTo ExitEgalité
 Else: Cells(N + 2, 1).Select
 Selection.EntireRow.Insert
  Cells(N + 2, 1) = s
 NT = NT + 1
 End If
 ExitEgalité:
 Cells(1, 2) = NT
 End Sub
 Private Sub CommandButton2_Click()
 ' Bouton QUITTER
 ' Masquer Userform1
   UserForm1.Hide
 ' Récupérer la mem. occupée par userform1
   Unload UserForm1
 End Sub
 Private Sub TextBox1_Change()
 Range("C1").Value = TextBox1.Value
 End Sub
 |