23 de agosto de 2009

Exercício da aula do dia 22/08/2009

Download do projeto aqui.

Exercício – Desenvolva uma interface que calcule a equação do segundo grau, onde seja possível entrar com os valores de "a", "b" e "c" via textbox. Neste mesmo programa desenvolva um programa que calcule o fatorial de um número. Este programa deve conter somente: 3 Textbox, 5 Labels, 1 command e 2 option ou 2 check:

Elementos:

Textbox 1 = "txtCoX2" - A ou - N
Textbox 2 = "txtCoX" - B
Textbox 3 = "txtConst" - C

Label 1 = "lbla" - A
Label 2 = "lblb" - B
Label 3 = "lblc" - C
Label 4 = "lblX1Val" - X1
Label 5 = "lblX2Val" X2

Command 1 = "cmdcalcular" - Calcular

Option 1 = "opt1" - Equação 2° Grau
Option 2 = "opt2" - Fatorial

Código:



Private Sub cmdcalcular_Click()
If opt2.Value = True Then
Dim valor, cont As Integer
Dim total As Double
total = 1
valor = Val(txtCoX2.Text)
If valor = 0 Then
MsgBox "Digite um valor valido", vbExclamation
Else
For cont = 1 To valor
total = total * cont
Next cont
lblX1Val.Caption = total

End If

Else

If opt1.Value = True Then
Dim A As Single
Dim B As Single
Dim C As Single
Dim Discriminante As Single
Dim X1 As Single
Dim X2 As Single
Dim n As Integer
Dim mensagem As String

A = Val(txtCoX2.Text)
B = Val(txtCoX.Text)
C = Val(txtConst.Text)

If A = 0 Then
MsgBox "O valor do coeficiente A não pode ser igual a zero.", vbCritical, mensagem
Exit Sub
End If

Discriminante = (B ^ 2 - 4 * A * C)

If Discriminante < 0 Then
lblX1Val = ""
lblX2Val = ""
MsgBox ("Esta equação não possui raizes reais !."), vbCritical, mensagem
Else
X1 = (-B + Sqr(Discriminante)) / (2 * A)
X2 = (-B - Sqr(Discriminante)) / (2 * A)
If Discriminante = 0 Then
lblX1Val = X1
lblX2Val = X1
MsgBox ("Esta equação possui duas raizes reais iguais."), vbInformation, mensagem
Else
lblX1Val = X1
lblX2Val = X2
MsgBox ("Esta equação possui duas raizes reais."), vbInformation, mensagem
End If
End If

End If
End If

End Sub


Private Sub opt1_Click()

txtCoX2.Text = ""
txtCoX.Text = ""
txtCoX.Visible = True
txtConst.Text = ""
txtConst.Visible = True
lblX1Val.Caption = ""
lblX2Val.Caption = ""
lbla.Caption = "A"
lblb.Visible = True
lblc.Visible = True
End Sub

Private Sub opt2_Click()

txtCoX2.Text = ""
txtCoX.Text = ""
txtCoX.Visible = False
txtConst.Text = ""
txtConst.Visible = False
lblX1Val.Caption = ""
lblX2Val.Caption = ""
lbla.Caption = "N"
lblb.Visible = False
lblc.Visible = False
End Sub

Private Sub txtConst_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then
txtConst.Text = Left(txtConst.Text, (Len(txtConst.Text) - 1))
SendKeys "{END}"
End If
If KeyAscii < 43 Or KeyAscii > 57 Or KeyAscii = 47 Or KeyAscii = 44 Then KeyAscii = 0
End Sub

Private Sub txtCoX_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then
txtCoX.Text = Left(txtCoX.Text, (Len(txtCoX.Text) - 1))
SendKeys "{END}"
End If
If KeyAscii < 43 Or KeyAscii > 57 Or KeyAscii = 47 Or KeyAscii = 44 Then KeyAscii = 0
End Sub

Private Sub txtCoX2_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then
txtCoX2.Text = Left(txtCoX2.Text, (Len(txtCoX2.Text) - 1))
SendKeys "{END}"
End If
If KeyAscii < 43 Or KeyAscii > 57 Or KeyAscii = 47 Or KeyAscii = 44 Then KeyAscii = 0
End Sub