用vb怎么做一个计算器?

代码怎么写啊?

Option Explicit
Dim str1 As String, str2 As String
Dim strtemp As String
Dim sum As Double
Dim d1 As Double, d2 As Double
Dim b As Boolean
Dim i As Integer

Private Sub cmdback_Click()
Command4.SetFocus
If b = False Then strtemp = str1 Else strtemp = str2
If Len(strtemp) > 0 Then
strtemp = Left(strtemp, Len(strtemp) - 1)
If strtemp = "" Then Text1.Text = "0" Else Text1.Text = strtemp
ElseIf strtemp = "" Then
Text1.Text = "0"
End If
If b = False Then str1 = strtemp Else str2 = strtemp
End Sub

Private Sub cmdpn_Click()
Command4.SetFocus
If b = False Then strtemp = str1 Else strtemp = str2
If Len(strtemp) > 0 And Left(strtemp, 1) = "-" Then
strtemp = Right(strtemp, Len(strtemp) - 1)
Else
strtemp = "-" & strtemp
End If
Text1.Text = strtemp
If b = False Then str1 = strtemp Else str2 = strtemp
End Sub

Private Sub Command1_Click(Index As Integer)
Command4.SetFocus
If b = False Then strtemp = str1 Else strtemp = str2
If Index < 10 Then
strtemp = strtemp + CStr(Index)
Else
If InStr(strtemp, ".") = 0 Then strtemp = strtemp + "."
End If
If Len(strtemp) > 1 And Left(strtemp, 1) = "0" And Mid(strtemp, 2, 1) <> "." Then
strtemp = Right(strtemp, Len(strtemp) - 1)
End If
Text1.Text = strtemp
If b = False Then str1 = strtemp Else str2 = strtemp
End Sub

Private Sub Command2_Click(Index As Integer)
Command4.SetFocus
If Len(Trim(str1)) <> 0 And Len(Trim(str2)) <> 0 Then
d1 = Val(str1)
d2 = Val(str2)
Select Case i
Case 0
d1 = d1 + d2
Case 1
d1 = d1 - d2
Case 2
d1 = d1 * d2
Case 3
If d2 = 0 Then
MsgBox "除数不能为0!", 16, "错误"
Exit Sub
End If
d1 = d1 / d2
End Select
str1 = CStr(d1)
str2 = ""
Text1.Text = str1
b = True
i = Index
Exit Sub
End If
str2 = ""
If str1 = "" Then str1 = Str(d1)
Text1.Text = str1
b = True
i = Index
End Sub

Private Sub Command3_Click()
Command4.SetFocus
str1 = ""
str2 = ""
Text1.Text = "0"
b = False
d1 = 0
End Sub

Private Sub Command4_Click()
If Len(Trim(str1)) = 0 Or Len(Trim(str2)) = 0 Then
b = False
Exit Sub
End If

d1 = Val(str1)
d2 = Val(str2)
Select Case i
Case 0
d1 = d1 + d2
Case 1
d1 = d1 - d2
Case 2
d1 = d1 * d2
Case 3
If d2 = 0 Then
MsgBox "除数不能为0!", 16, "错误"
Exit Sub
End If
d1 = d1 / d2
End Select
str1 = CStr(d1)
Text1.Text = str1
b = False
str2 = ""
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
Command4.SetFocus
Dim s As Integer, f As Integer
If (KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 46 Then

Select Case KeyAscii
Case 48
s = 0
Case 49
s = 1
Case 50
s = 2
Case 51
s = 3
Case 52
s = 4
Case 53
s = 5
Case 54
s = 6
Case 55
s = 7
Case 56
s = 8
Case 57
s = 9
Case 46
s = 10
End Select
Call Command1_Click(s)
ElseIf KeyAscii = 42 Or KeyAscii = 43 Or KeyAscii = 45 Or KeyAscii = 47 Then
Select Case KeyAscii
Case 42
f = 2
Case 43
f = 0
Case 45
f = 1
Case 47
f = 3
End Select
Call Command2_Click(f)
ElseIf KeyAscii = 13 Or KeyAscii = 61 Then
Call Command4_Click
ElseIf KeyAscii = 32 Or KeyAscii = 82 Or KeyAscii = 114 Then
Call Command3_Click
ElseIf KeyAscii = 67 Or KeyAscii = 99 Then
Call cmdback_Click
Else
KeyAscii = 0
End If
End Sub

Private Sub Label1_Click()
frmAbout.Show
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-05-25
用vb做这种东西是最简单的了用鼠标点简单的可以用
command控件
用键盘输入是写个KEY事件按哪个数字就直接调用哪个command控件的点击事件
连加减乘除最简单了
别把你保存数字的函数或者控件清空就可以了
sin,cos,tan,平方和立方的运算
这些VB里有内部函数直接用
后面的写下公式就可以了
第2个回答  2006-03-07
这个简单了
画几个控件
写几个语句就行
第3个回答  2006-03-07
用一个累加器
第4个回答  2006-03-07
这个东西不是一句两句话说的清楚的

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网