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

segunda-feira, 18 de janeiro de 2016

Ficha nº21

1.

Código:

Public Function Par_Impar(x As Integer) As Integer
    If x Mod 2 = 0 Then
        Par_Impar = 0
    Else
        Par_Impar = 1
    End If
End Function

Private Sub Command1_Click()
    If Val(Text1.Text) = 0 Then
        End
    ElseIf Val(Text1.Text) > 0 Then
        MsgBox "O valor devolvido é: " & Par_Impar(Val(Text1.Text)), vbInformation, "Valor"
        Text1.Text = ""
    Else
        MsgBox "Tem de inserir um número inteiro positivo", vbCritical, "Aviso"
        Text1.Text = ""
        Label2.Caption = ""
    End If
End Sub

Private Sub Form_Load()
    MsgBox "Quando o valor devolvido for 0 o número é par, quando for 1 o número é ímpar. Quando quiser encerrar a aplicação introduza o valor 0.", vbInformation, "Informação"
End Sub

2.

Código:

Public Function Area(x As Integer) As Integer
    Area = x ^ 2
End Function

Public Function Perimetro(x As Integer) As Integer
    Perimetro = x * 4
End Function

Private Sub Command1_Click()
    If Val(Text1.Text) <= 0 Then
        MsgBox "O valor introduzido é inválido", vbCritical, "Aviso"
        Text1.Text = ""
    Else
        MsgBox "A área do quadrado é " & Area(Val(Text1.Text)) & " e o perímetro é " & Perimetro(Val(Text1.Text)) & ".", 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

3.

Código:

Public Function Divisores(x As Integer) As Integer
    Dim i As Integer, cont As Integer
    
    cont = 0
    
    For i = 1 To x
        If x Mod i = 0 Then
            cont = cont + 1
        End If
    Next i
    Divisores = cont
End Function

Private Sub Command1_Click()
    If Val(Text1.Text) < 0 Then
        MsgBox "O valor é inválido", vbCritical, "Aviso"
        Text1.Text = ""
    Else
        MsgBox "O número tem " & Divisores(Val(Text1.Text)) & " divisores.", vbInformation, "Divisores"
        Text1.Text = ""
    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

4.

Código:

Dim notas As Single

Public Sub IntroduzirNotas()
    Dim i As Integer, num As Single
    
    For i = 1 To 3
        Do
            num = Val(InputBox("Introduza a " & i & " classificação:", "Classificações"))
            If num < 0 Or num > 20 Then
                MsgBox "A nota introduzida é inválida", vbCritical, "Aviso"
            End If
        Loop While num < 0 Or num > 20
        notas = notas + num
        num = 0
    Next i
End Sub

Public Function Med(x As Single) As Single
    Med = Round(x / 3, 2)
End Function

Private Sub Command1_Click()
    IntroduzirNotas
    
    MsgBox "A média das notas introduzidas é " & Med(notas), vbInformation, "Média"
    
    notas = 0
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