G - New Page 1
Transcrição
G - New Page 1
Exame 24-06-2014 I 1-a) S X 3 3 Byref X’ Byval y “G” 3 3 “G” “G” 3 3 “G” “G” 3 “G” 3 “G” 1 “” 3 3 “G” “G” 3 3 “G” “G” 1 2 “G” “G” 3 “G” 3 “G” 2 “GG” 3 3 “G” “G” 3 2 “G” “G” 2 2 “GG” “GG” “GG” 2 “G” 2 “G” 2 “GG” 2 1-b) i x Output “BIP” 1 “BIP” 1 “BIP” 2 “BIP” 2 “BIP” 3 “BIP” 3 “BIP” “B” “I” 3 1-c) Dim i as integer, x as string x=”BIP” For i=1 to 2 Call MsgBox(mid(x,i,1)) Next Call MsgBox(i) 2) a) Correcta. Integer.(-1)*3>> -3 b) Correcta. String. Left(“mi ni”, 3-1)>> “mi” c) Correcta. Integer. Inteiro aleatório entre [41,92] d)Correcta. Integer. Len(“BoaSorte”)*3>>8*3>>24 K S3 Output “” II 1) Sub cop() Dim r1 as integer, r2 as integer With Sheet1 r1=1 r2=1 Do While not isempty(.cells(r1,1)) If .cells(r1,1)<100 and .cells(r1,1) mod 3 = 0 then .cells(r2,2)=.cells(r1,1) r2=r2+1 End if r1=r1+1 Loop End with End sub 2) Sub Aleatório() Dim r as integer Randomize For r=1 to 10 Cells(r,1)=int(rnd*16 + 43) Next End sub III 1) Function iguais( byval s1 as string, byval s2 as string, byval n as integer) as Boolean iguais=left(s1,n)=left(s2,n) End function 2) Sub P() Dim s1 as string, s2 as string, n as integer s1=inputbox(“Introduza uma frase”) s2=inputbox(“Introduza outra frase”) n=inputbox(“Introduza um número inteiro”) if Iguais(s1,s2,n) then Call MsgBox(“IGUAIS”) Else Call MsgBox(“DIFERENTES”) End if End sub IV 1) Function MEIO(byval s as string, byval p as integer, byval n as integer) as string Dim x as string, c as integer c=len(s) x=Right(s,c-p+1) x=left(x,n) MEIO=x End Function 2) Funciona mesmo que argumentos sejam strings Function Ordem(ByRef MyRange As Range) As Integer Dim v As Variant, b As Boolean Dim r1 As Integer, r As Integer, nr As Integer nr = MyRange.Rows.Count v = MyRange b = True r=1 Ordem = 0 ' assumir não ordenado Do While b And r < nr b = v(r, 1) = v(r + 1, 1) r=r+1 Loop If b Then ' são todos iguais ? Ordem = 1 ' assumir ordem crescente Else b = True If v(r, 1) > v(r - 1, 1) Then ' tentar crescente Do While b And r < nr b = v(r, 1) <= v(r + 1, 1) r=r+1 Loop If b Then Ordem = 1 End If Else ' tentar decrescente Do While b And r < nr b = v(r, 1) >= v(r + 1, 1) r=r+1 Loop If b Then Ordem = -1 End If End If End If End Function Só funciona com argumentos numéricos (A eficiencia é semelhante) Function Ordered2(ByRef MyRange As Range) As Integer Dim v As Variant, b As Boolean, sinal As Integer Dim r1 As Integer, r As Integer, nr As Integer Dim d As Integer nr = MyRange.Rows.Count v = MyRange b = True r=1 Do While b And r < nr b = v(r, 1) = v(r + 1, 1) r=r+1 Loop If b Then ' são todos iguais ? Ordered2 = 1 Else b = True sinal = Sgn(v(r, 1) - v(r - 1, 1)) Do While b And r < nr d = Sgn(v(r + 1, 1) - v(r, 1)) b = d = 0 Or sinal = d r=r+1 Loop If b Then Ordered2 = sinal Else Ordered2 = 0 End If End If End Function Nota: Sgn devolve 1 se arg >0, -1 se arg<0 e 0 se arg = 0