TP 2 Exercice 3 : Impôts Sub impot() Dim montant As Integer montant = Cells(1, 1).Value Dim montanttot As Integer Select Case montant Case 0 To 1000 montanttot = 0.1 * montant Case 1001 To 5000 montanttot = 0.1 * 1000 + 0.3 * (montant - 1000) Case montanttot = 0.1 * 1000 + 0.3 * 3999 + 0.5 * (montant - 5000) End Select Exercice 4 : Somme Des Nombres Sub SommeN() Dim a As Integer, s As Integer, prec As Integer s = 0 a = 0 prec = 0 Do prec = a s = s + a a = Application.InputBox( entrez la valeur de a ) Loop Until a < prec MsgBox La somme est & s Exercice 5 : Sphinx Sub Sphinx() Dim r As Integer, s As Integer, c As Integer r = Application.InputBox("Entrez un nombre") c = 0 Do Until r = a s = Application.InputBox("Entrez un nombre positif") c = c + 1 If s > r Then MsgBox "Entrez un nombre plus petit" If s < r Then MsgBox "Entrez un nombre plus grand" Loop MsgBox "Le nombre est bien" & r & "Vous l'avez trouvé en" & c & "coups" Exercice 6 : Palindrome Sub Palindrome() Dim x As String, nb As Integer Dim palindrom As Boolean Dim i As Integer x = Application.InputBox("Entrez un mot") nb = Len(x) palindrom = True For i = 1 To nb / 2 If Mid(x, i, 1) < > Mid(x, nb + 1 - i, 1) Then palindrom = False
Exit For If palindrom Then MsgBox ("C'est un palindrome") MsgBox ("Ce n'est pas un palindrome") : autre version Public Sub Lepalindrome( ) Dim motu as string, moti as string, taille as byte, cont as byte Motu = application.imputbox(«entrez un mot», «le jeu du palindrome», type:=2) taille= len(motu) cont= taille do moti= moti +mid(motu,cont,1) cont= cont-1 loop until compt = 0 if motu= motto then Msgbox(«c est un palindrome») else Msgbox(«ce n est pas un palindrome») end if End sub Exercice 7 : Devinez mon nombre Sub Devinette_Correction() Dim n1 As Integer, n2 As Integer Dim nbbons As Integer, nbbienplaces As Integer n1 = Application.InputBox("Nombre à deviner?", Type:=1) nbbienplaces = 0 Do n2 = Application.InputBox("Proposition du joueur?", Type:=1) nbbons = 0: nbbienplaces = 0 Dim chiffre As Byte, nb As Integer, nbj As Integer Dim i As Long, j As Long nb = n1 For i = 1 To 3 chiffre = nb Mod 10 nbj = n2 For j = 1 To 3 If nbj Mod 10 = chiffre Then nbbons = nbbons + 1 nbj = nbj \ 10 Next j nb = nb \ 10 nb = n1: nbj = n2 For i = 1 To 3 If nb Mod 10 = nbj Mod 10 Then nbbienplaces = nbbienplaces + 1 nb = nb \ 10: nbj = nbj \ 10 nbbons = nbbons - nbbienplaces
If (nbbienplaces <> 3) Then MsgBox ("Il y a " & nbbienplaces & " chiffres au bon endroit " & nbbons & "chiffres mal placés") Loop While (nbbienplaces <> 3) MsgBox ("Bravo! Vous avez trouvé, le nombre est " & n1) TD 2 Exercice 1 : Type Date et entrées-sorties Sub datedujour( ) Dim x As Date x = Date MsgBox (x) Sub DateAnniversaire() Dim a As Integer, annee As Integer, age As Integer, d As Date d = Date a = MsgBox("La date est :" & d & "Est-ce votre anniversaire?", 4) If a = 6 Then annee = Application.InputBox("Quelle est votre année de naissance?") age = Year(d) - annee MsgBox ("Aujourd'hui, vous avez" & age & "ans") Sub age(naissance As Date) Dim d As Date, age1 As Integer d = Date age1 = Year(d) - Year(naissance) If Month(d) < Month(naissance) Then age1 = age1-1 If Month(d) = Month(naissance) And Day(d) < Day(naissance) Then age1 = age1-1 Msgbox ("Votre âge est" & age1 & «ans") afficher le message «vous êtes majeur» si l utilisateur a plus de 18 ans «vous êtes mineur» sinon Sub MajMin ( ) Dim ag as Byte ag = MsgBox(«Avez vous plus de 18 ans?», 4, «majeur ou mineur» ) If ag= 6 then MsgBox «vous êtes majeur» MsgBox «vous êtes mineurs» end if end Sub deviner l age de l utilisateur -un utilisateur tape son âge -un 2ème essaie de deviner en tapant un âge (5X) Sub DevinerAge( ) Dim ageu as byte, aged As Byte, cont As byte, rep as byte cont=0 ageu= ApplicationInputBox( «tapez votre âge», «utilisateur», type:=1) Do aged= ApplicationInputBox(«devinez l age», «devineur», type :=1) cont=cont+1
If ageu > aged Then Rep= MsgBox («il est plus vieux»,5 «devineur») MsgBox(«Bravo vous avez gagné») Exit Sub End if if rep=2 Then Eixtsub Loopwhile coont <=5 MsgBox («vous avez perdu») End sub Exercice 2 : Plage de nombres Sub remplir() Dim i As Integer, j As Integer, k As Integer k = 0 For i = 1 To 100 For j = 1 To 100 k = k + 1 Cells(i, j).value = k Next j L2C1 : L100C1 --> " =L(-1)C+100 " L1C2 : L11C100 --> " =LC(-1)+1" Sub remplir3() Dim i As Integer Cells(1, 1).Value = 1 For i = 2 To 100 Cells(i, r).formular1c1local = "L(-1)C+100" For i = 1 To 100 For j = 2 To 100 Cells(i, j).formular1c1local = "LC(-1)+1" Next j Exercice 3 : Entrées-Sorties Sub carre() Dim nombre As Integer nombre = Application.InputBox("Entrez un nombre", Type:=1) MsgBox nombre * nombre TP 3 Exercice 1 : Volume d une sphère Function volume(r As Double) As Double Const pi = 3.14 Dim s As Double s = 4 * pi * r * r volume = r * s / 3 Sub testsphère() Dim r1 As Single r1 = Application.InputBox("Entrez le rayon de la sphère", Type:=2)
MsgBox "Le volume est" & volume(r1) Exercice 2 : Fonction SommeChiffres Function SommeChiffres(n As Integer) As Integer Dim s As Integer s = 0 Do Until n = 0 s = s + n Mod 10 n = n \ 10 Loop SommeChiffres = s Sub affichesomme() Dim n As Integer n = Application.InputBox("Votre chiffre?", Type:=1) MsgBox "La somme des chiffres est de " & SommeChiffres(n) Exercice 3 : Sélection de dates sous Excel Const d As Date = #1/1/2010# Sub test() For i = 1 To 5 If Cells(i, 1).Value > d Then Cells(i, 1).Interior.Color = RGB(200, 0, 0) Exercice 4 : procédure, dessiner un échiquier 10x10 Sub Echiquier10x10(x As Integer, y As Integer) Dim i As Integer For i = 1 To 10 For j = 1 To 10 If (i + j) Mod 2 = 0 Then Cells(i, j).interior.color = RGB(0, 0, 0) Cells(i, j).interior.color = RGB(200, 0, 0) Next j Sub Echiquier() Dim x As Integer, y As Integer x = Application.InputBox("Saisissez le numéro de la ligne") y = Application.InputBox("Saisissez le numéro de la colonne") Echiquier10x10 x, y Exercice 5 : Dessiner une pyramide Sub coloriersegment(byval x As Integer, ByVal ligne As Integer, ByVal colonne As Integer) Dim i As Integer For i = 0 To x - 1 Cells(ligne, colonne + i).interior.color = RGB(0, 0, 250) Sub colorierpyramide(byval lignesommet As Integer, ByVal colsommet As Integer, ByVal n As Integer) Dim i As Integer For i = 1 To n
coloriersegment 2 * i - 1, lignesommet, colsommet lignesommet = lignesommet + 1 colsommet = colsommet - 1 Sub ApplicationPyramide() Dim x As Integer, ligne As Integer, colonne As Integer x = Application.InputBox("Taille de la pyramide") ligne = Application.InputBox("Numéro de la ligne") colonne = Application.InputBox("Numéro de la colonne") colorierpyramide x, ligne, colonne TP Indice de Gini Q1 : Function LignePays(ByVal pays As String) As Integer Dim i As Integer: LignePays = 0 For i = 2 To 9 If (Cells(i, 1).Value) = pays Then LignePays = i If LignePays = 0 Then MsgBox ("Le pays n'existe pas") Sub test() Dim P1 As String, l As Integer P1 = Application.InputBox("Donnez votre ville") l = LignePays(P1) If l = 0 Then MsgBox ("La ville n'existe pas") MsgBox ("La ville se trouve à la ligne" & l) Q2 : Function Verification(ByVal pays As String) As String Dim i As Integer, j As Integer, s As Double i = LignePays(pays) Verification = True s = 0 For j = 2 To 5 s = Cells(i, j).value + s If Cells(i, j).value > Cells(i, j + 1).Value Then Verification = False Next j s = s + Cells(i, 6).Value If s <> 1 Then Verification = False Sub TestVerification() Dim P1 As String
P1 = Application.InputBox("Donnez votre pays") MsgBox (Verification(P1)) Q3 : Sub remplir_verification(byval pays As String) Dim i As Integer i = LignePays(pays) If Verification(pays) Then Cells(i, 7).Value = 1 Cells(i, 7).Value = 0 Function LignePays(x As String) As Integer Dim i As Long, n As Long n = 0 For i = 2 To 9 If Cells(i, 1) = x Then n = i LignePays = n Function Verification(x As String) As Boolean Dim b As Double, Verif As Boolean, li As Integer, i As Long li = LignePays(x) b = Cells(li, 2).Value Verif = True For i = 2 To 5 b = b + Cells(li, i + 1).Value If Cells(li, i).value > Cells(li, i + 1) Then Verif = False If b <> 1 Then Verif = False Verification = Verif Sub Remplir_Verification(x As String) Dim li As Integer li = LignePays(x) If Verification(x) = False Then Cells(li, 7).Value = 0 Cells(li, 7).Value = 1 Q4 : Q5 : Q6 : Sub Remplir_Lorenz(x As String) Dim li As Integer li = LignePays(x) Cells(li, 8).Value = Cells(li, 2) Range(Cells(li, 9), Cells(li, 12)).FormulaR1C1Local = "=LC(-1)+LC(-6)"
Function Calcul_Gini(x As String) As Double Dim sous As Double, sur As Double, ind As Double, li As Integer, i As Long li = LignePays(x): sous = 0: sur = 0.2 * Cells(li, 8).Value: For i = 8 To 11 sous = sous + 0.2 * Cells(li, i).value sur = sur + 0.2 * Cells(li, i + 1).Value ind = sous + sur ind = 0.5 * ind ind = 0.5 - ind ind = ind / 0.5 Calcul_Gini = ind Sub Pays() Dim x As String, reponse As Integer, b As Integer Do x = Application.InputBox("Le nom du pays est =") Do While LignePays(x) = 0 MsgBox "Ce pays n'est pas répertorié, essayer un autre pays" x = Application.InputBox("Le nom du pays est =") Loop Verification (x) Remplir_Verification x If Verification(x) = False Then MsgBox "Les pourcentages de revenus de ce pays sont erronés, veuillez les modifier." Remplir_Lorenz x Cells(LignePays(x), 13) = Calcul_Gini(x) b = MsgBox("Voulez-vous continuer?", 4) Loop Until b = 7 TP Représentation binaire des nombres Exercice 2 : Conversion d un entier positif en base 2 Q 2.1 Function DecToBin(n As Integer, nbits As Integer) As Boolean Dim Resultat As Integer, i As Integer Resultat = n For i = 0 To nbits - 1 Cells(1, nbits - i).value = Resultat Mod 2 Resultat = Resultat \ 2 If Resultat = 0 Then DecToBin = True DecToBin = False Q 2.2 Sub DecToBin_Test() Dim n As Integer, nbits As Integer
n = Application.InputBox("Quel est votre nombre?") nbits = Application.InputBox("Nombre de bits") MsgBox (DecToBin(n, nbits)) Q 2.3 Function BinToDec(ByVal nbits As Long) As Long Dim i As Long, n As Long, puiss As Long n = 0 puiss = 1 For i = 0 To nbits - 1 n = n + Cells(1, nbits - i).value * puiss puiss = puiss * 2 BinToDec = n Q 2.4 Sub BinToDec_Test() Dim v As Long v = Application.InputBox("Entrez le nombre de bits") MsgBox (BinToDec(v)) Q 2.5 Sub verification() Dim ndec As Long, nobtenu As Long ndec = Application.InputBox("nombre décimal compris entre...") If DecToBin(nDec, 10) Then nobtenu = BinToDec(10) If nobtenu = ndec Then MsgBox "C'est bon!" MsgBox "Problème!" Q 2.6 Sub affbin(byval n As Long) Dim taille As Long, nb As Long, i As Long nb = n taille = 0 Do While nb > 0 If nb Mod 10 > 1 Then MsgBox "Erreur, ce n'est pas un nombre binaire" Exit Sub taille = taille + 1 nb = nb \ 10 Loop nb = n For i = taille To 1 Step -1 Cells(1, i).value = nb Mod 10 nb = nb \ 10
Exercice 3 : Calcul binaire sur des entiers positifs Q 3.1 Sub additionne(byval nbb As Long) Dim i As Long, r As Long, somme As Long r = 0 For i = nbb To 1 Step -1 somme = Cells(1, i).value + Cells(2, i).value + r Select Case somme Case 2 r = 1 Cells(3, i).value = 0 Case 3 r = 1 Cells(3, i).value = 1 Case r = 0 Cells(3, i).value = somme End Select If r = 1 Then MsgBox "Dépassement de capacité" Sub test_add() additionne (10) Q 3.2 Sub soustrait(byval nbb As Long) Dim i As Long, r As Long, sous As Long r = 0 For i = nbb To 1 Step -1 sous = Cells(1, i).value - Cells(2, i).value - r Select Case sous Case -2 r = 1 Cells(3, i).value = 0 Case -1 r = 1 Cells(3, i).value = 1 Case r = 0 Cells(3, i).value = sous End Select If r = 1 Then MsgBox "Dépassement de capacité" Sub test_sous() soustrait (10) Exercice 4 : Conversion d un entier relatif Sub compl1() Dim i As Long
For i = 1 To 8 Cells(2, i).value = 1 - Cells(1, i).value Function plus1(byval li As Long) As Boolean Dim i As Long, r As Long r = 1: i = 8 Do While i > 0 And r = 1 If Cells(li, i).value = 1 Then Cells(li, i).value = 0 Cells(li, i).value = 1 r = 0 i = i - 1 Loop plus1 = r = 0 'équivalent à "if r=0, plus1 = true,, plus1 = false Function DecToBinR(ByVal n As Long) As Boolean If n >= 0 Then DecToBinR = DecToBin(n, 8) If DecToBin(-n, 8) Then compl1 DecToBinR = plus1(2) MsgBox ("dépassement de capacité") DecToBinR = False Correction TP : 4ème séance voir fiche exercice 1 : 1) Public Function Fact( ByVal n as Long) Dim f As long f= 1 Do while n > 1 f=f*1 n=n-1 Loop Fact = f End function Sub macro (n as integer ) n = Application.Inputbox ( «saisissez un entier positif») MsgBox Fact( ) end Sub correction : Public Sub CalcFact ( ) Dim x as integer Do x=inputbox(«tapez le nbre à calculer la factorielle») Loop Until x>=0 MsgBox «la factorielle «&x&» est &Fact(x)
2) Sub macro ( ) n = Application.InputBox(«Saisissez un entier positif») m=application.inputbox( «saisissez un entier positif») if n >= m and n >0 and m>0 then c= Fact(n)/ (Fact(n)*Fact(n-m)) Msg Box (c) : Msgbox («Impossible») End if exercice 2 1) PublicFunction somdiv (Byval x as Long) as Long Dim i As integer Dim r as integer dim c as integer c= 0 Do r= n Mod i Mod ( reste de la division ) If r =0 then c= c+1 Loop While i< n Somdiv=c Subndiv ( ) Dim n as Long n=application.inputbox ( «saisissez un entier») MsgBox somdiv ( n ) version du prof : PublicFunction somdiv (Byval x as integer) as integer Dim i As integer, s as integer s=0 for i =1 to x/2 if x Mod i=0 then s=s+i End if SomDiv= S