12
Correction TP info 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 Else montantTot = 0.1 * 1000 + 0.3 * 3999 + 0.5 * (montant - 5000) End Select End Sub 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 End Sub 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" ElseIf s < r Then MsgBox "Entrez un nombre plus grand" End If Loop MsgBox "Le nombre est bien" & r & "Vous l'avez trouvé en" & c & "coups" End Sub 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

correction tp info - phinedo.com€¦ · Correction TP info TP 2 Exercice 3 : Impôts Sub impot() Dim montant As Integer montant = Cells(1, 1).Value Dim montantTot As Integer Select

  • Upload
    hanhan

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

Page 1: correction tp info - phinedo.com€¦ · Correction TP info TP 2 Exercice 3 : Impôts Sub impot() Dim montant As Integer montant = Cells(1, 1).Value Dim montantTot As Integer Select

CorrectionTPinfo

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 Else montantTot = 0.1 * 1000 + 0.3 * 3999 + 0.5 * (montant - 5000) End Select End Sub 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(“entrezla valeur de a”)Loop Until a < prec MsgBox “Lasomme est”& s End Sub 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" ElseIf s < r Then MsgBox "Entrez un nombre plus grand" End If Loop MsgBox "Le nombre est bien" & r & "Vous l'avez trouvé en" & c & "coups" End Sub 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

Page 2: correction tp info - phinedo.com€¦ · Correction TP info TP 2 Exercice 3 : Impôts Sub impot() Dim montant As Integer montant = Cells(1, 1).Value Dim montantTot As Integer Select

CorrectionTPinfo

Exit For End If Next i If palindrom Then MsgBox ("C'est un palindrome") Else MsgBox ("Ce n'est pas un palindrome") End If End Sub : 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’estun palindrome ») else Msgbox(« ce n’estpas 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 End If nbJ = nbJ \ 10 Next j nb = nb \ 10 Next i nb = n1: nbJ = n2 For i = 1 To 3 If nb Mod 10 = nbJ Mod 10 Then nbBienPlaces = nbBienPlaces + 1 End If nb = nb \ 10: nbJ = nbJ \ 10 Next i nbBons = nbBons - nbBienPlaces

Page 3: correction tp info - phinedo.com€¦ · Correction TP info TP 2 Exercice 3 : Impôts Sub impot() Dim montant As Integer montant = Cells(1, 1).Value Dim montantTot As Integer Select

CorrectionTPinfo

If (nbBienPlaces <> 3) Then MsgBox ("Il y a " & nbBienPlaces & " chiffres au bon endroit " & nbBons & "chiffres mal placés") End If Loop While (nbBienPlaces <> 3) MsgBox ("Bravo ! Vous avez trouvé, le nombre est " & n1) End Sub

TD 2 Exercice 1 : Type Date et entrées-sorties Sub dateDuJour( ) Dim x As Date x = Date MsgBox (x) End Sub 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") End If End Sub 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 ElseIf Month(d) = Month(naissance) And Day(d) < Day(naissance) Then age1 = age1 - 1 End If Msgbox ("Votre âge est" & age1 & « ans") End Sub afficher le message « vous êtes majeur » si l’utilisateura 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 » Else MsgBox « vous êtes mineurs » end if end Sub deviner l’agede 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

Page 4: correction tp info - phinedo.com€¦ · Correction TP info TP 2 Exercice 3 : Impôts Sub impot() Dim montant As Integer montant = Cells(1, 1).Value Dim montantTot As Integer Select

CorrectionTPinfo

If ageu > aged Then Rep= MsgBox (« il est plus vieux »,5 « devineur ») Else MsgBox(« Bravo vous avez gagné ») Exit Sub End if if rep=2 Then Eixtsub End If 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 Next i End Sub 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" Next i For i = 1 To 100 For j = 2 To 100 Cells(i, j).FormulaR1C1Local = "LC(-1)+1" Next j Next i End Sub Exercice 3 : Entrées-Sorties Sub carre() Dim nombre As Integer nombre = Application.InputBox("Entrez un nombre", Type:=1) MsgBox nombre * nombre End Sub

TP 3 Exercice 1 : Volume d’unesphè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 End Function Sub testSphère() Dim r1 As Single r1 = Application.InputBox("Entrez le rayon de la sphère", Type:=2)

Page 5: correction tp info - phinedo.com€¦ · Correction TP info TP 2 Exercice 3 : Impôts Sub impot() Dim montant As Integer montant = Cells(1, 1).Value Dim montantTot As Integer Select

CorrectionTPinfo

MsgBox "Le volume est" & volume(r1) End Sub 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 End Function Sub affichesomme() Dim n As Integer n = Application.InputBox("Votre chiffre ?", Type:=1) MsgBox "La somme des chiffres est de " & SommeChiffres(n) End Sub 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) End If Next i End Sub 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) Else Cells(i, j).Interior.Color = RGB(200, 0, 0) End If Next j Next i End Sub 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 End Sub 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) Next i End Sub Sub colorierPyramide(ByVal ligneSommet As Integer, ByVal colSommet As Integer, ByVal n As Integer) Dim i As Integer For i = 1 To n

Page 6: correction tp info - phinedo.com€¦ · Correction TP info TP 2 Exercice 3 : Impôts Sub impot() Dim montant As Integer montant = Cells(1, 1).Value Dim montantTot As Integer Select

CorrectionTPinfo

colorierSegment 2 * i - 1, ligneSommet, colSommet ligneSommet = ligneSommet + 1 colSommet = colSommet - 1 Next i End Sub 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 End Sub

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 End If Next i If LignePays = 0 Then MsgBox ("Le pays n'existe pas") End If End Function 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") Else MsgBox ("La ville se trouve à la ligne" & l) End If End Sub 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 End If Next j s = s + Cells(i, 6).Value If s <> 1 Then Verification = False End If End Function Sub TestVerification() Dim P1 As String

Page 7: correction tp info - phinedo.com€¦ · Correction TP info TP 2 Exercice 3 : Impôts Sub impot() Dim montant As Integer montant = Cells(1, 1).Value Dim montantTot As Integer Select

CorrectionTPinfo

P1 = Application.InputBox("Donnez votre pays") MsgBox (Verification(P1)) End Sub Q3 : Sub remplir_verification(ByVal pays As String) Dim i As Integer i = LignePays(pays) If Verification(pays) Then Cells(i, 7).Value = 1 Else Cells(i, 7).Value = 0 End If End Sub 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 End If Next i LignePays = n End Function 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 End If Next i If b <> 1 Then Verif = False End If Verification = Verif End Function Sub Remplir_Verification(x As String) Dim li As Integer li = LignePays(x) If Verification(x) = False Then Cells(li, 7).Value = 0 Else Cells(li, 7).Value = 1 End If End Sub 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)"

Page 8: correction tp info - phinedo.com€¦ · Correction TP info TP 2 Exercice 3 : Impôts Sub impot() Dim montant As Integer montant = Cells(1, 1).Value Dim montantTot As Integer Select

CorrectionTPinfo

End Sub 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 Next i ind = sous + sur ind = 0.5 * ind ind = 0.5 - ind ind = ind / 0.5 Calcul_Gini = ind End Function 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." Else Remplir_Lorenz x Cells(LignePays(x), 13) = Calcul_Gini(x) End If b = MsgBox("Voulez-vous continuer?", 4) Loop Until b = 7 End Sub

TP –Représentation binaire des nombres Exercice 2 : Conversion d’unentier 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 Next i If Resultat = 0 Then DecToBin = True Else DecToBin = False End If End Function Q 2.2 Sub DecToBin_Test() Dim n As Integer, nBits As Integer

Page 9: correction tp info - phinedo.com€¦ · Correction TP info TP 2 Exercice 3 : Impôts Sub impot() Dim montant As Integer montant = Cells(1, 1).Value Dim montantTot As Integer Select

CorrectionTPinfo

n = Application.InputBox("Quel est votre nombre ?") nBits = Application.InputBox("Nombre de bits") MsgBox (DecToBin(n, nBits)) End Sub 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 Next i BinToDec = n End Function Q 2.4 Sub BinToDec_Test() Dim v As Long v = Application.InputBox("Entrez le nombre de bits") MsgBox (BinToDec(v)) End Sub 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) End If If nObtenu = nDec Then MsgBox "C'est bon !" Else MsgBox "Problème !" End If End Sub 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 End If 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 Next i End Sub

Page 10: correction tp info - phinedo.com€¦ · Correction TP info TP 2 Exercice 3 : Impôts Sub impot() Dim montant As Integer montant = Cells(1, 1).Value Dim montantTot As Integer Select

CorrectionTPinfo

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 Else r = 0 Cells(3, i).Value = somme End Select Next i If r = 1 Then MsgBox "Dépassement de capacité" End If End Sub Sub test_add() additionne (10) End Sub 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 Else r = 0 Cells(3, i).Value = sous End Select Next i If r = 1 Then MsgBox "Dépassement de capacité" End If End Sub Sub test_sous() soustrait (10) End Sub Exercice 4 : Conversion d’unentier relatif Sub compl1() Dim i As Long

Page 11: correction tp info - phinedo.com€¦ · Correction TP info TP 2 Exercice 3 : Impôts Sub impot() Dim montant As Integer montant = Cells(1, 1).Value Dim montantTot As Integer Select

CorrectionTPinfo

For i = 1 To 8 Cells(2, i).Value = 1 - Cells(1, i).Value Next i End Sub 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 Else Cells(li, i).Value = 1 r = 0 End If i = i - 1 Loop plus1 = r = 0 'équivalent à "if r=0, plus1 = true, Else, plus1 = false End Function Function DecToBinR(ByVal n As Long) As Boolean If n >= 0 Then DecToBinR = DecToBin(n, 8) Else If DecToBin(-n, 8) Then compl1 DecToBinR = plus1(2) Else MsgBox ("dépassement de capacité") DecToBinR = False End If End If End Function

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)

Page 12: correction tp info - phinedo.com€¦ · Correction TP info TP 2 Exercice 3 : Impôts Sub impot() Dim montant As Integer montant = Cells(1, 1).Value Dim montantTot As Integer Select

CorrectionTPinfo

End Sub 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) Else : Msgbox (« Impossible ») End if End Sub 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 End Function Subndiv ( ) Dim n as Long n=Application.InputBox ( « saisissez un entier ») MsgBox somdiv ( n ) End Sub 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 Next i SomDiv= S End Function