vb编写计算器加减乘除怎么弄?

2025-03-01 20:59:00
推荐回答(1个)
回答1:

我好不容易才弄出来的,你看着给分吧。用复制粘贴的方法把Cmddigit创建到(20)(也就是21个按钮)。
代码如下。
===========================
Option Explicit
Dim x As Integer
Dim num As Double
Private Sub cmddigit_click(index As Integer)
Select Case index
Case Is < 11
If InStr(1, Text1.Text, ".") = 0 Then
Text1.Text = Text1.Text + cmddigit(index).Caption
Else
If index <> 10 Then Text1.Text = Text1.Text + cmddigit(index).Caption
End If
Case 11
Text1.Text = Left(Len(Text1.Text) - 1, Text1.Text)
Case 12
Text1.Text = ""
Text2.Text = ""
Case 13
If InStr(1, Text1.Text, "-") = 1 Then
Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1)
Else
Text1.Text = "-" & Text1.Text
End If
Case 18
Text2.Text = Str(1 / Text1.Text)
Case 19
Text2.Text = Str(Sqr(Text1.Text))
Case 20
Select Case x
Case 14
Text2.Text = num + CDbl(Text1.Text)
Case 15
Text2.Text = num - CDbl(Text1.Text)
Case 16
Text2.Text = num * CDbl(Text1.Text)
Case 17
Text2.Text = num / CDbl(Text1.Text)
End Select
Text1.Text = ""
Case Else
num = CDbl(Text1.Text)
x = index
Text1.Text = ""
End Select
End Sub

Private Sub Form_Load()
cmddigit(10).Caption = "."
cmddigit(11).Caption = "Backspace"
cmddigit(12).Caption = "CE"
cmddigit(13).Caption = "+/-"
cmddigit(14).Caption = "+"
cmddigit(15).Caption = "-"
cmddigit(16).Caption = "*"
cmddigit(17).Caption = "/"
cmddigit(18).Caption = "1/x"
cmddigit(19).Caption = "Sqrt"
cmddigit(20).Caption = "="
Text1.Text = ""
Text2.Text = ""
End Sub