没有科学计算功能,不过有自动处理先乘除后加减的功能。也是为了实现这个功能,可能代码有点多,希望大家多给意见,谢了
Option Explicit
Dim a As Double, b As Double, c As Double
'a是最终结果,b是优先级结果,c是录入变量
Dim idx As Integer
Dim PreMode As String, JustMode As String, CurMode As String
Private Sub ComAC_Click() '清空按钮
a = 0
b = 0
c = 0
Text1.Text = "0"
idx = 1
PreMode = ""
JustMode = ""
CurMode = ""
End Sub
Private Sub ComAdd_Click() '加法
On Error GoTo er
c = Val(Text1.Text)
CurMode = "+"
If PreMode = "" Then
If JustMode = "" Then
a = c
ElseIf JustMode = "+" Then
a = a + c
ElseIf JustMode = "-" Then
a = a - c
ElseIf JustMode = "*" Then
a = a * c
ElseIf JustMode = "/" Then
a = a / c
End If
ElseIf PreMode = "+" Then
If JustMode = "*" Then
a = a + b * c
ElseIf JustMode = "/" Then
a = a + b / c
End If
ElseIf PreMode = "-" Then
If JustMode = "*" Then
a = a - b * c
ElseIf JustMode = "/" Then
a = a - b / c
End If
End If
Text1.Text = CStr(a)
PreMode = ""
JustMode = CurMode
idx = 2
Exit Sub
er:
MsgBox Err.Description, vbCritical, "Error", vbCritical, "Error"
Call ComAC_Click
Exit Sub
End Sub
Private Sub ComBak_Click() '退格
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
If Len(Text1.Text) = 0 Then
Text1.Text = "0"
End If
End Sub
Private Sub ComDiv_Click() '除法
On Error GoTo er
c = Val(Text1.Text)
CurMode = "/"
If PreMode = "" Then
If JustMode = "" Then
a = c
Text1.Text = CStr(a)
ElseIf JustMode = "+" Or JustMode = "-" Then
b = c
PreMode = JustMode
ElseIf JustMode = "*" Then
a = a * c
Text1.Text = CStr(a)
ElseIf JustMode = "/" Then
a = a / c
Text1.Text = CStr(a)
End If
ElseIf PreMode = "+" Then
If JustMode = "*" Then
b = b * c
ElseIf JustMode = "/" Then
b = b / c
End If
ElseIf PreMode = "-" Then
If JustMode = "*" Then
b = b * c
ElseIf JustMode = "/" Then
b = b / c
End If
End If
JustMode = CurMode
idx = 2
Exit Sub
er:
MsgBox Err.Description, vbCritical, "Error"
Call ComAC_Click
Exit Sub
End Sub
Private Sub ComDot_Click() '小数点
If idx = 1 Then
If InStr(1, Text1.Text, ".") = 0 Then
Text1.Text = Text1.Text + "."
End If
Else
Text1.Text = "0."
End If
idx = 1
End Sub
Private Sub ComEqu_Click() '等号
On Error GoTo er
c = Val(Text1.Text)
If PreMode = "" Then
If JustMode = "+" Then
a = a + c
ElseIf JustMode = "-" Then
a = a - c
ElseIf JustMode = "*" Then
a = a * c
ElseIf JustMode = "/" Then
a = a / c
End If
ElseIf PreMode = "+" Then
If JustMode = "*" Then
a = a + b * c
ElseIf JustMode = "/" Then
a = a + b / c
End If
ElseIf PreMode = "-" Then
If JustMode = "*" Then
a = a - b * c
ElseIf JustMode = "/" Then
a = a - b / c
End If
End If
Text1.Text = CStr(a)
a = 0
b = 0
c = 0
PreMode = ""
JustMode = ""
CurMode = ""
idx = 2
Exit Sub
er:
MsgBox Err.Description, vbCritical, "Error"
Call ComAC_Click
Exit Sub
End Sub
Private Sub ComMul_Click() '乘号
On Error GoTo er
c = Val(Text1.Text)
CurMode = "*"
If PreMode = "" Then
If JustMode = "" Then
a = c
Text1.Text = CStr(a)
ElseIf JustMode = "+" Or JustMode = "-" Then
b = c
PreMode = JustMode
ElseIf JustMode = "*" Then
a = a * c
Text1.Text = CStr(a)
ElseIf JustMode = "/" Then
a = a / c
Text1.Text = CStr(a)
End If
ElseIf PreMode = "+" Then
If JustMode = "*" Then
b = b * c
ElseIf JustMode = "/" Then
b = b / c
End If
ElseIf PreMode = "-" Then
If JustMode = "*" Then
b = b * c
ElseIf JustMode = "/" Then
b = b / c
End If
End If
JustMode = CurMode
idx = 2
Exit Sub
er:
MsgBox Err.Description, vbCritical, "Error"
Call ComAC_Click
Exit Sub
End Sub
Private Sub ComNum_Click(Index As Integer) '数字键入
If idx = 1 Then
If Text1.Text = "0" Then
Text1.Text = CStr(Index)
Else
Text1.Text = Text1.Text + CStr(Index)
End If
Else
Text1.Text = CStr(Index)
End If
idx = 1
End Sub
Private Sub ComOff_Click()
Unload Me
End Sub
Private Sub ComPN_Click() '正负号
If Text1.Text = "0" Then
Text1.Text = "-"
Else
If InStr(1, Text1.Text, "-") = 0 Then
Text1.Text = "-" + Text1.Text
Else
Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1)
End If
End If
idx = 1
End Sub
Private Sub ComSub_Click() '减法
On Error GoTo er
c = Val(Text1.Text)
CurMode = "-"
If PreMode = "" Then
If JustMode = "" Then
a = c
ElseIf JustMode = "+" Then
a = a + c
ElseIf JustMode = "-" Then
a = a - c
ElseIf JustMode = "*" Then
a = a * c
ElseIf JustMode = "/" Then
a = a / c
End If
ElseIf PreMode = "+" Then
If JustMode = "*" Then
a = a + b * c
ElseIf JustMode = "/" Then
a = a + b / c
End If
ElseIf PreMode = "-" Then
If JustMode = "*" Then
a = a - b * c
ElseIf JustMode = "/" Then
a = a - b / c
End If
End If
Text1.Text = CStr(a)
PreMode = ""
JustMode = CurMode
idx = 2
Exit Sub
er:
MsgBox Err.Description, vbCritical, "Error"
Call ComAC_Click
Exit Sub
End Sub
Private Sub Form_Load()
'赋上初值
PreMode = ""
JustMode = ""
CurMode = ""
a = 0
b = 0
c = 0
idx = 1
End Sub
Private Sub Text1_Change()
If Val(Text1.Text) < 1 And Val(Text1.Text) > 0 And Left(Text1.Text, 1) <> "0" Then
Text1.Text = "0" & Text1.Text
End If
End Sub
[此贴子已经被作者于2006-1-13 19:57:52编辑过]