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

sexta-feira, 22 de janeiro de 2016

Ficha nº22

1.

Código:

Public Function Quadrados(x As Integer, y As Integer, z As Integer) As Integer
    Quadrados = (x ^ 2) + (y ^ 2) + (z ^ 2)
End Function

Private Sub Command1_Click()
    Dim valores(1 To 3) As Integer, i As Integer
    
    For i = 1 To 3
        Do
            valores(i) = Val(InputBox("Introduza o " & i & " valor:", "Valores"))
            If valores(i) < 0 Then
                MsgBox "O valor introduzido é inválido", vbCritical, "Aviso"
            End If
        Loop While valores(i) < 0
    Next i
    
    MsgBox "A soma dos quadrados dos valores introduzidos é " & Quadrados(valores(1), valores(2), valores(3)), vbInformation, "Resultado"
End Sub

Private Sub Command2_Click()
    Dim opcao As Integer
    
    opcao = MsgBox("Deseja sair?", vbQuestion + vbYesNo, "Sair")
    If opcao = vbYes Then
        End
    End If
End Sub

2.

Código:

Public Function Soma(N As Integer) As Integer
    Dim i As Integer
    
    For i = 1 To N
        Soma = Soma + i
    Next i
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 introduzido é inválido", vbCritical, "Aviso"
        End If
    Loop While num < 0
    
    MsgBox "A soma dos números inteiros de 1 até " & num & " são: " & Soma(num), vbInformation, "Resultado"
End Sub

Private Sub Command2_Click()
    Dim opcao As Integer
    
    opcao = MsgBox("Deseja sair?", vbQuestion + vbYesNo, "Sair")
    If opcao = vbYes Then
        End
    End If
End Sub

3.

Código:

Public Function Divisores(N As Integer) As Integer
    Dim i As Integer
    
    For i = 1 To N
        If N Mod i = 0 Then
            Divisores = Divisores + 1
        End If
    Next i
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 introduzido é inválido", vbCritical, "Aviso"
        End If
    Loop While num < 0
    
    MsgBox "O número que introduziu tem " & Divisores(num) & " divisores.", vbInformation, "Resultado"
End Sub

Private Sub Command2_Click()
    Dim opcao As Integer
    
    opcao = MsgBox("Deseja sair?", vbQuestion + vbYesNo, "Sair")
    If opcao = vbYes Then
        End
    End If
End Sub

4.

Código:

Public 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 num(1 To 3) As Integer, i As Integer
    
    For i = 1 To 3
        num(i) = Val(InputBox("Introduza o " & i & " valor:", "Valores"))
    Next i
    
    MsgBox "O maior valor é " & Maior(num(1), num(2), num(3)), vbInformation, "Maior"
End Sub

Private Sub Command2_Click()
    Dim opcao As Integer
    
    opcao = MsgBox("Deseja sair?", vbQuestion + vbYesNo, "Sair")
    If opcao = vbYes Then
        End
    End If
End Sub

5.

Código:

Public Function Graus(x As Integer) As Integer
    Graus = (5 / 9) * (x - 32)
End Function

Private Sub Command1_Click()
    Dim fah As Integer
    
    fah = Val(InputBox("Introduza a temperatura em graus Fahrenheit:", "Fahrenheit"))
    
    MsgBox "A temperatura " & fah & " em celsius é " & Graus(fah), vbInformation, "Celsius"
End Sub

Private Sub Command2_Click()
    Dim opcao As Integer
    
    opcao = MsgBox("Deseja sair?", vbQuestion + vbYesNo, "Sair")
    If opcao = vbYes Then
        End
    End If
End Sub

6.

Código:

Public Function Funcao(x As Integer, y As Integer) As Integer
    If x Mod y = 0 Then
        Funcao = 1
    Else
        Funcao = 0
    End If
End Function

Private Sub Command1_Click()
    Dim y As Integer, x As Integer, i As Integer, valor As Integer
    
    Do
        x = Val(InputBox("Introduza o x:", "X"))
        If x < 0 Then
            MsgBox "O valor é inválido", vbCritical, "Aviso"
        End If
    Loop While x < 0
    
    Do
        y = Val(InputBox("Introduza o y:", "Y"))
        If y < 0 Then
            MsgBox "O valor é inválido", vbCritical, "Aviso"
        End If
    Loop While y < 0
    
    valor = Funcao(x, y)
    
    If valor = 1 Then
        MsgBox "O x é divisivel por y", vbInformation, "Resultado"
    Else
        MsgBox "O x não é divisivel por y", vbInformation, "Resultado"
    End If
End Sub

Private Sub Command2_Click()
    Dim opcao As Integer
    
    opcao = MsgBox("Deseja sair?", vbQuestion + vbYesNo, "Sair")
    If opcao = vbYes Then
        End
    End If
End Sub

7.

Código:

Public Function Devolve(a As Integer, b As Integer, c As Integer) As Integer
    If a = 0 Then
        Devolve = 0
    ElseIf a > 0 Then
        Devolve = b
    Else
        Devolve = -c
    End If
End Function


Private Sub Command1_Click()
    Dim num(1 To 3) As Integer, i As Integer
    
    For i = 1 To 3
        num(i) = Val(InputBox("Introduza o " & i & " valor:", "Entrada de dados"))
    Next i
    
    MsgBox "O valor devolvido foi " & Devolve(num(1), num(2), num(3)), vbInformation, "Resultado"
End Sub

Private Sub Command2_Click()
    Dim opcao As Integer
    
    opcao = MsgBox("Deseja sair?", vbQuestion + vbYesNo, "Sair")
    If opcao = vbYes Then
        End
    End If
End Sub

Sem comentários:

Enviar um comentário