请帮我添加\修改一下代码,使计算器功能更为完整.
1\使计算器能在按数字键后显示在文本框中的是带有"."的数字,例如"9."
2\使计算器能在按等号键后的运算结果显示在文本框中的是带有"."的结果.
3\添加按小数点键功能的代码本人编写的代码:
Dim q As Integer
Dim a As String
Dim y As String
Dim x As String
Dim z As IntegerPrivate Sub Form_KeyPress(KeyAscii As Integer)
  If q = 2 Then '按符号后再按数字,会发生清空
     Text1.Text = ""
  End If
   
  Select Case KeyAscii
    Case 48
      If Text1.Text = "0." Then '按0时,会始终保持为0.
         Text1.Text = "0."
      Else
         q = 1
         Text1.Text = Text1.Text & Chr$(KeyAscii)
      End If
    Case 49
      If Text1.Text = "0." Then '按数字1-9时,会发生清空
         Text1.Text = ""
      End If
      q = 1
      Text1.Text = Text1.Text & Chr$(KeyAscii)
    Case 50
      If Text1.Text = "0." Then
         Text1.Text = ""
      End If
      q = 1
      Text1.Text = Text1.Text & Chr$(KeyAscii)
    Case 51
      If Text1.Text = "0." Then
         Text1.Text = ""
      End If
      q = 1
      Text1.Text = Text1.Text & Chr$(KeyAscii)
    Case 52
      If Text1.Text = "0." Then
         Text1.Text = ""
      End If
      q = 1
      Text1.Text = Text1.Text & Chr$(KeyAscii)
    Case 53
      If Text1.Text = "0." Then
         Text1.Text = ""
      End If
      q = 1
      Text1.Text = Text1.Text & Chr$(KeyAscii)
    Case 54
      If Text1.Text = "0." Then
         Text1.Text = ""
      End If
      q = 1
      Text1.Text = Text1.Text & Chr$(KeyAscii)
    Case 55
      If Text1.Text = "0." Then
         Text1.Text = ""
      End If
      q = 1
      Text1.Text = Text1.Text & Chr$(KeyAscii)
    Case 56
      If Text1.Text = "0." Then
         Text1.Text = ""
      End If
      q = 1
      Text1.Text = Text1.Text & Chr$(KeyAscii)
    Case 57
      If Text1.Text = "0." Then
         Text1.Text = ""
      End If
      q = 1
      Text1.Text = Text1.Text & Chr$(KeyAscii)
    Case 42
      a = "*"
      x = Text1.Text
      q = 2
    Case 43
      a = "+"
      x = Text1.Text
      q = 2
    Case 45
      a = "-"
      x = Text1.Text
      q = 2
    Case 47
      a = "/"
      x = Text1.Text
      q = 2
    Case 61 '等号
      If a = "+" Then
         If z = 3 Then
            x = Text1.Text '第二次按等号
         Else
            y = Text1.Text '第一次按等号
         End If
         z = 3
         Text1.Text = Val(x) + Val(y)
      ElseIf a = "-" Then
         If z = 3 Then
            x = Text1.Text
         Else
            y = Text1.Text
         End If
         z = 3
         Text1.Text = Val(x) - Val(y)
      ElseIf a = "*" Then
         If z = 3 Then
            x = Text1.Text
         Else
            y = Text1.Text
         End If
         z = 3
         Text1.Text = Val(x) * Val(y)
      ElseIf a = "/" Then
         If z = 3 Then
            x = Text1.Text
         Else
            y = Text1.Text
         End If
         z = 3
         Text1.Text = Val(x) / Val(y)
      End If
  End Select
End SubPrivate Sub Form_Load()
  Text1.Text = "0."
  z = 0
End Sub

解决方案 »

  1.   

    上面写的能执行吗?看了两眼,我看你有个text,按键盘数字的时候首先会执行将输入的数字填充入text的事件,不会执行Form_KeyPress的,而且太难受了吧,不如老实点,就照着系统给的计算器做,那样不用你这么辛苦的查按键的asc码了,看了你的问题,好象不知道"."的asc码是多少,用asc涵数去查,"."如果我没记错的话是46,而且你的代码虽然简单,但是太长太麻烦,分又太少.