'fuhao存储运算符,x存储第一个数,y存储第二个数,z存储结果,flag用来标识是否连续作运算
'flag2 用来标识是否执行过等于操作
Dim fuhao As String, x As Single, y As Single, z As String, flag As Integer, flag2 As Integer
'执行计算器退格操作
Private Sub backs_Click()
  If Text1.Text <> "" Then
    Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
  Else
    Text1.Text = ""
    Exit Sub
  End If
End Sub
Private Sub c_Click() '将计算器所有值全部置 0
Text1.Text = ""
x = 0
y = 0
End Sub
Private Sub ce_Click() '将计算器输入的当前值置 0
Text1.Text = ""
y = 0
End Sub
'点击0-9数字
Private Sub Command14_Click(Index As Integer)
  If flag2 = 1 Then 'flag为1时,表示执行过等于操作,需要清空之前的计算结果
  Text1.Text = ""
  flag2 = flag2 + 1
  End If
  If Left(Text1.Text, 1) = "0" And InStr(Text1.Text, ".") Then '只能输入一个'.',输入的数开头不能为0
    Text1.Text = Text1.Text & Index
  ElseIf Left(Text1.Text, 1) = "0" And Text1.Text <> "" Then
    If Index = 0 Then
      Text1.Text = "0"
    ElseIf Index <> 0 Then
      'Print "ddddd"
      Text1.Text = Right((Text1.Text & Index), 1)
    End If
  Else
    Text1.Text = Text1.Text & Index
  End If
End Sub
'取倒数运算
Private Sub daoshu_Click()
    flag2 = 1
  If Val(Text1.Text) <> 0 Then '分母不能为0
        z = 1 / Text1.Text  '取倒数
    Else
        MsgBox "分母不能为0!", vbCritical, "错误提示"
    End If
   
    If Left(Val(z), 1) = "." Then '将.###转成0.###
      z = "0" & Val(z)
    ElseIf Left(Val(z), 2) = "-." Then '将-.###转成-0.###
      z = "-0." & Right(Val(z), Len(z) - 2)
    End If
    Text1.Text = z
End Sub
'开根号运算
Private Sub kaigen_Click()
    flag2 = 1
    If Text1.Text <> "" Then
        If Val(Text1.Text) >= 0 Then
            z = Text1.Text ^ (1 / 2) '开根号运算
        Else
            MsgBox "负数不能开根号!", vbCritical, "错误提示"
        End If
    End If
    If Left(Val(z), 1) = "." Then '将.###转成0.###
      z = "0" & Val(z)
    ElseIf Left(Val(z), 2) = "-." Then '将-.###转成-0.###
      z = "-0." & Right(Val(z), Len(z) - 2)
    End If
    Text1.Text = z
End Sub
'百分号运算,公式A ? B % =  A × (1 + B/100) or A + (A × B/100)
Private Sub quyu_Click()
    flag2 = 1
    z = x * (Val(Text1.Text) / 100)
    If Left(Val(z), 1) = "." Then '将.###转成0.###
      z = "0" & Val(z)
vb计算器代码大全    ElseIf Left(Val(z), 2) = "-." Then '将-.###转成-0.###
      z = "-0." & Right(Val(z), Len(z) - 2)
    End If
    Text1.Text = z
   
End Sub
'取负运算
Private Sub zf_Click()
  flag2 = 1
  If Val(Text1.Text) <> "" Then
    Text1.Text = -(Text1.Text)
  ElseIf Left(Text1.Text, 1) = "-" Then
    Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
  ElseIf Text1.Text = 0 Then
    Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
  Else
    Text1.Text = "-" & Text1.Text
  End If
End Sub
'加法运算
Private Sub plus_Click()
    If flag = 0 Then
        flag = flag + 1
        x = Val(Text1.Text)
        fuhao = "+"
        Text1.Text = ""
    Else
        If fuhao = "-" Then '连续运算操作
            x = x - Val(Text1.Text)
        ElseIf fuhao = "*" Then
            x = x * Val(Text1.Text)
        ElseIf fuhao = "/" Then
            If Val(Text1.Text) <> 0 Then
              x = x / Val(Text1.Text)
            Else
              MsgBox "除数不能为0", vbCritical, "错误提示"
            End If
        Else
            x = x + Val(Text1.Text)
            'Print x & "ccc"
        End If
        fuhao = "+"
        Text1.Text = ""
    End If
End Sub
'减法运算
Private Sub jian_Click()
    If flag = 0 Then
        flag = flag + 1
        x = Val(Text1.Text)
        fuhao = "-"
        Text1.Text = ""
    Else
        If fuhao = "+" Then
            x = x + Val(Text1.Text)
        ElseIf fuhao = "*" Then
            x = x * Val(Text1.Text)
        ElseIf fuhao = "/" Then
            If Val(Text1.Text) <> 0 Then
              x = x / Val(Text1.Text)
            Else
              MsgBox "除数不能为0", vbCritical, "错误提示"
            End If
        Else
            x = x - Val(Text1.Text)
        End If
        fuhao = "-"
        Text1.Text = ""
    End If
End Sub
'乘法运算
Private Sub chen_Click()
If flag = 0 Then
        flag = flag + 1
        x = Val(Text1.Text)
        fuhao = "*"
        Text1.Text = ""
Else
        If fuhao = "+" Then
            x = x + Val(Text1.Text)
            'Print x & "bbbb"
        ElseIf fuhao = "-" Then
            x = x - Val(Text1.Text)
        ElseIf fuhao = "/" Then
            If Val(Text1.Text) <> 0 Then

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。