Calculadora

Ze21038

Power Member
boas,

eu estou a fazer uma calculadora ja esta praticamente finalizada, o problema é que quando carrego no enter(butao do igual), ao clicar depois no +-*/ qualqer um deles ela da valores esqueitos, algem me pode ajudar??
Código:
Option Explicit
Dim operacao As String
Dim resultado As Double
Dim entrou As Boolean
Dim v1 As Double
Dim v2 As Double
Dim clicou As Boolean
Dim vir As Boolean
Dim entmul As Integer
Dim entdiv As Integer
Dim entsom As Integer
Dim entsub As Integer
Dim KeyAscii As Integer
Dim tec As Boolean
Dim loc As Integer


Private Sub cmd0_Click()
    If txtVisor.Text = "0" Then
        v1 = 0
        txtVisor.Text = "0"
    
    Else
        If clicou = True Then
            If vir = True Then
                v2 = v2 & "," & 0
                txtVisor.Text = v2
                vir = False
            Else
                v2 = v2 & 0
                txtVisor.Text = v2
            End If
         Else
            If vir = True Then
                v1 = v1 & "," & 0
                txtVisor.Text = v1
                vir = False
              Else
                v1 = v1 & 0
                txtVisor.Text = v1
            End If
        End If
    End If
End Sub

Private Sub cmd1_Click()


    If txtVisor.Text = "0" And vir = False Then
        v1 = 1
        txtVisor.Text = "1"
    
    Else
        If clicou = True Then
            If vir = True Then
                v2 = v2 & "," & 1
                txtVisor.Text = v2
                vir = False
            Else
                v2 = v2 & 1
                txtVisor.Text = v2
            End If
         Else
            If vir = True Then
                v1 = v1 & "," & 1
                txtVisor.Text = v1
                vir = False
              Else
                v1 = v1 & 1
                txtVisor.Text = v1
            End If
            End If
    End If


End Sub

Private Sub cmd2_Click()


    If txtVisor.Text = "0" And vir = False Then
        v1 = 2
        txtVisor.Text = "2"
    
    Else
       If clicou = True Then
            If vir = True Then
                v2 = v2 & "," & 2
                txtVisor.Text = v2
                vir = False
            Else
                v2 = v2 & 2
                txtVisor.Text = v2
            End If
         Else
            If vir = True Then
                v1 = v1 & "," & 2
                txtVisor.Text = v1
                vir = False
              Else
                v1 = v1 & 2
                txtVisor.Text = v1
            End If
    End If
    End If
End Sub

Private Sub cmd3_Click()

If txtVisor.Text = "0" And vir = False Then
    v1 = 3
    txtVisor.Text = "3"

Else
    If clicou = True Then
        If vir = True Then
            v2 = v2 & "," & 3
            txtVisor.Text = v2
            vir = False
        Else
            v2 = v2 & 3
            txtVisor.Text = v2
        End If
     Else
        If vir = True Then
            v1 = v1 & "," & 3
            txtVisor.Text = v1
            vir = False
          Else
            v1 = v1 & 3
            txtVisor.Text = v1
        End If
End If
End If
End Sub

Private Sub cmd4_Click()

    If txtVisor.Text = "0" And vir = False Then
        v1 = 4
        txtVisor.Text = "4"
    
    Else
        If clicou = True Then
            If vir = True Then
                v2 = v2 & "," & 4
                txtVisor.Text = v2
                vir = False
            Else
                v2 = v2 & 4
                txtVisor.Text = v2
            End If
         Else
            If vir = True Then
                v1 = v1 & "," & 4
                txtVisor.Text = v1
                vir = False
              Else
                v1 = v1 & 4
                txtVisor.Text = v1
            End If
    End If
    End If
End Sub

Private Sub cmd5_Click()

    If txtVisor.Text = "0" And vir = False Then
        v1 = 5
        txtVisor.Text = "5"
    
    Else
        If clicou = True Then
            If vir = True Then
                v2 = v2 & "," & 5
                txtVisor.Text = v2
                vir = False
            Else
                v2 = v2 & 5
                txtVisor.Text = v2
            End If
         Else
            If vir = True Then
                v1 = v1 & "," & 5
                txtVisor.Text = v1
                vir = False
              Else
                v1 = v1 & 5
                txtVisor.Text = v1
            End If
    End If
    End If
End Sub

Private Sub cmd6_Click()

    If txtVisor.Text = "0" And vir = False Then
        v1 = 6
        txtVisor.Text = "6"
    
    Else
        If clicou = True Then
            If vir = True Then
                v2 = v2 & "," & 6
                txtVisor.Text = v2
                vir = False
            Else
                v2 = v2 & 6
                txtVisor.Text = v2
            End If
         Else
            If vir = True Then
                v1 = v1 & "," & 6
                txtVisor.Text = v1
                vir = False
              Else
                v1 = v1 & 6
                txtVisor.Text = v1
            End If
    End If
    End If
End Sub

Private Sub cmd7_Click()

    If txtVisor.Text = "0" And vir = False Then
        v1 = 7
        txtVisor.Text = "7"
    
    Else
        If clicou = True Then
            If vir = True Then
                v2 = v2 & "," & 7
                txtVisor.Text = v2
                vir = False
            Else
                v2 = v2 & 7
                txtVisor.Text = v2
            End If
         Else
            If vir = True Then
                v1 = v1 & "," & 7
                txtVisor.Text = v1
                vir = False
              Else
                v1 = v1 & 7
                txtVisor.Text = v1
            End If
    End If
    End If
End Sub

Private Sub cmd8_Click()

    If txtVisor.Text = "0" And vir = False Then
        v1 = 8
        txtVisor.Text = "8"
    
    Else
        If clicou = True Then
            If vir = True Then
                v2 = v2 & "," & 8
                txtVisor.Text = v2
                vir = False
            Else
                v2 = v2 & 8
                txtVisor.Text = v2
            End If
         Else
            If vir = True Then
                v1 = v1 & "," & 8
                txtVisor.Text = v1
                vir = False
              Else
                v1 = v1 & 8
                txtVisor.Text = v1
            End If
        End If
    End If
End Sub

Private Sub cmd9_Click()
    
    If txtVisor.Text = "0" And vir = False And vir = False Then
        v1 = 9
        txtVisor.Text = "9"
    
    Else
        If clicou = True Then
            If vir = True Then
                v2 = v2 & "," & 9
                txtVisor.Text = v2
                vir = False
            Else
                v2 = v2 & 9
                txtVisor.Text = v2
            End If
         Else
            If vir = True Then
                v1 = v1 & "," & 9
                txtVisor.Text = v1
                vir = False
              Else
                v1 = v1 & 9
                txtVisor.Text = v1
            End If
    End If
    End If
End Sub

Private Sub cmdc_Click()
    v1 = 0
    v2 = 0
    clicou = False
    entrou = False
    txtVisor.Text = "0"

End Sub

Private Sub cmddividir_Click()
    operacao = "/"
   
    If clicou = True Then
        v1 = resultado / v1 'antes v1=v1/v2
        v2 = 0
        txtVisor.Text = v1
    Else
        clicou = True
        resultado = v1 'txtvisor.Text
        If entrou = True Then
            v1 = resultado / v2
        End If
    End If
    loc = 3
End Sub

Private Sub cmdigual_Click()
    clicou = False
    entrou = True
    'Soma
    If operacao = "+" And entsom = 1 Then
        resultado = resultado + v2
    End If
    
    If operacao = "+" And entsom = 0 Then
        resultado = v1 + v2
        entsom = 1
    End If
    
    'Divisao
    If operacao = "/" And entdiv = 1 Then
        If v2 = 0 Then
         MsgBox "É impossivel dividir por zero", vbOKOnly
         cmdc_Click
         v2 = 0
        Else
         resultado = resultado / v2
        End If
        
    End If
    
    If operacao = "/" And entdiv = 0 Then
        If v2 = 0 Then
           MsgBox "É impossivel dividir por zero", vbOKOnly
           cmdc_Click
           v1 = 0
           resultado = 0
         Else
           resultado = v1 / v2
           entdiv = 1
        End If
    
    End If
    
    'Multiplicacao
    If operacao = "*" And entmul = 1 Then
        resultado = resultado * v2
    End If
    If operacao = "*" And entmul = 0 Then
        resultado = v1 * v2
        entmul = 1
    End If
    'Subtracao
    If operacao = "-" And entsub = 1 Then
        resultado = resultado - v2
        loc = 2
    End If
    If operacao = "-" And entsub = 0 Then
        resultado = v1 - v2
        entsub = 1
        loc = 2
    End If
  
    txtVisor.Text = resultado


End Sub

Private Sub cmdmultiplicar_Click()
    operacao = "*"
   

    If clicou = True Then
        v1 = v1 * v2
        v2 = 0
        txtVisor.Text = v1
        
    Else
        clicou = True
        resultado = v1 'txtvisor.Text
        
        If entrou = True Then
             
             
            v1 = resultado * v2
        End If
    End If
    loc = 4
End Sub

Private Sub cmdsomar_Click()
    operacao = "+"
    
    If clicou = True Then
        v1 = v1 + v2
        v2 = 0
        txtVisor.Text = v1
    Else
        clicou = True
        resultado = v1 'txtvisor.Text
        If entrou = True Then
            v1 = resultado + v2
        End If
    End If
    loc = 1

End Sub

Private Sub cmdsubtrair_Click()
    operacao = "-"
   'If loc = 2 Then
        
        If clicou = True Then 'And loc = 2 Then
            v1 = resultado - v2 'antes v1=resultado - v2 'v1=v1-v2
            v2 = 0
            txtVisor.Text = v1
        Else
            clicou = True
            resultado = v1 'txtvisor.Text
            If entrou = True Then
                v1 = resultado - v2
                txtVisor.Text = v1
            End If
        End If
    'Else
     '    txtVisor.Text = resultado
    
   ' End If
     
End Sub

Private Sub cmdVirgula_Click()
    vir = True
End Sub

Private Sub Form_Load()
    
    
    
    loc = 0
    entrou = False
    clicou = False
    vir = False
    entmul = 0
    entdiv = 0
    entsom = 0
    entsub = 0
    tec = False
    
End Sub



Private Sub txtVisor_KeyPress(KeyAscii As Integer)
    If KeyAscii <> 0 Then
        tec = True
       
        Select Case KeyAscii
            Case Asc("1")
                    KeyAscii = 0
                    cmd1_Click
                
            Case Asc("2")
                KeyAscii = 0
                   cmd2_Click
            Case Asc("3")
                   KeyAscii = 0
                   cmd3_Click
               
            Case Asc("4")
                   KeyAscii = 0
                   cmd4_Click
                
            Case Asc("5")
                   KeyAscii = 0
                   cmd5_Click
                
            Case Asc("6")
                   KeyAscii = 0
                   cmd6_Click
               
            Case Asc("7")
                   KeyAscii = 0
                   cmd7_Click
               
            Case Asc("8")
                   KeyAscii = 0
                   cmd8_Click
                
            Case Asc("9")
                   KeyAscii = 0
                   cmd9_Click
                
            Case Asc("0")
                   KeyAscii = 0
                   cmd0_Click
                
            Case Asc("+")
                   KeyAscii = 0
                   cmdsomar_Click
            Case Asc("*")
                   KeyAscii = 0
                   cmdmultiplicar_Click
            Case Asc("/")
                   KeyAscii = 0
                   cmddividir_Click
            Case Asc("-")
                   KeyAscii = 0
                   cmdsubtrair_Click
            Case Asc(".")
                   KeyAscii = 0
                   cmdVirgula_Click
            Case Is = 27
                   KeyAscii = 0
                   cmdc_Click
            Case Is = 13
                   KeyAscii = 0
                   cmdigual_Click
                       
        
        End Select
    End If
   
  
 End Sub
cumprimentos,
ze21038
 
Última edição pelo moderador:
Vir, v1, v2, clicou...
Zero code mantainability :007:

Houve um post de uma calculadora hà uns tempos.
Não me lembro se disponibiliza a source, mas dá uma espreitadela, e pede a source ao criador se vier compilado.
Talvez ele a distribua.
 
Back
Topo