"A arte de programar consiste na arte de organizar e dominar a complexidade."

terça-feira, 16 de fevereiro de 2016

Teste Módulo 11

1.

Código:

Private Function Maior(x As Integer, y As Integer, z As Integer) As Integer
    If x > y And x > z Then
        Maior = x
    ElseIf y > x And y > z Then
        Maior = y
    Else
        Maior = z
    End If
End Function

Private Sub Command1_Click()
    Dim a As Integer, b As Integer, c As Integer
    
    Do
        a = Val(InputBox("Introduza o 1º valor:", "1º valor"))
        If a < 0 Then
            MsgBox "O valor introduzido é inválido", vbCritical, "Aviso"
        End If
    Loop While a < 0
    
    Do
        b = Val(InputBox("Introduza o 2º valor:", "2º valor"))
        If b < 0 Then
            MsgBox "O valor introduzido é inválido", vbCritical, "Aviso"
        End If
    Loop While b < 0
    
    Do
        c = Val(InputBox("Introduza o 3º valor:", "3º valor"))
        If c < 0 Then
            MsgBox "O valor introduzido é inválido", vbCritical, "Aviso"
        End If
    Loop While c < 0
    
    MsgBox "O maior valor é " & Maior(a, b, c) & ".", vbInformation, "Resultado"
End Sub

2.

Código:

Private Function Multiplos(x As Integer) As Integer
    Dim i As Integer, cont As Integer, mult As Integer
    
    cont = 0
    
    For i = 1 To 50
        mult = x * i
        If mult <= 50 Then
            cont = cont + 1
        Else
            Exit For
        End If
    Next i
    Multiplos = cont
End Function

Private Sub Command1_Click()
    Dim num As Integer
    
    Do
        num = Val(InputBox("Introduza um número", "Número"))
        If num <= 0 Then
            MsgBox "O número é inválido", vbCritical, "Aviso"
        End If
    Loop While num <= 0
    
    MsgBox "O número " & num & " tem " & Multiplos(num) & " múltiplos inferiores a 50.", vbInformation, "Resultado"
End Sub

3.

Código:

Private Function SomaN(x As Integer) As Integer
    Dim i As Integer, soma As Integer
    
    soma = 0
    
    For i = 1 To x
        soma = soma + i
    Next i
    
    SomaN = soma
End Function

Private Sub Command1_Click()
    Dim num As Integer
    
    Do
        num = Val(InputBox("Introduza um valor:", "Valor"))
        If num < 0 Then
            MsgBox "O valor introduzido é inválido", vbCritical, "Aviso"
        End If
    Loop While num < 0
    
    MsgBox "A soma dos números é " & SomaN(num), vbInformation, "Resultado"
End Sub

4.

Código:

Private Function Exponencial(x As Integer, y As Integer) As Integer
    Dim i As Integer, res As Integer
    
    res = 1
    
    If y <> 0 Then
        For i = 1 To y
            res = res * x
        Next i
    End If
    
    Exponencial = res
End Function

Private Sub Command1_Click()
    Dim a As Integer, b As Integer
    
    Do
        a = Val(InputBox("Introduza o 1º valor:", "1º valor"))
        b = Val(InputBox("Introduza o 2º valor:", "2º valor"))
        If a <= 0 Or b < 0 Then
            MsgBox "O valor introduzido é inválido", vbCritical, "Aviso"
        End If
    Loop While a <= 0 Or b < 0
    
    MsgBox "O valor de " & a & " elevado a " & b & " é " & Exponencial(a, b), vbInformation, "Resultado"
End Sub

Sem comentários:

Enviar um comentário