'程序
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0 To 9 '如果点击了数字键
'***************************************
'*** Str函数是转换字符串 ***
'*** RTrim函数是去字符串右侧空格 ***
'*** LTrim函数是去字符串左侧空格 ***
'***************************************
If FirstNum = True Then
If Index = 0 Then
strnum1 = "0."
FirstNum = False
Else
strnum1 = Str(Index)
FirstNum = False
End If
Else
strnum1 = RTrim(strnum1) + LTrim(Str(Index))
End If
Text1.Text = strnum1
Case 10 '如果点击了小数点
'**********************************
'*** InStr函数是比较字符串 ***
'**********************************
If InStr(Text1.Text, ".") = 0 Then
If FirstNum = True Then
strnum1 = "0."
FirstNum = False
Else
strnum1 = strnum1 + "."
End If
End If
Text1.Text = strnum1
Case 11 To 14 '如果点击了算数运算符
FirstNum = True
'如果点击了等于号
If Epual = True Then
'调用自定义的reckon计算函数
Text1.Text = Reckon
'如果没点击等于号
Else
Epual = True
strnum2 = strnum1
strnum1 = ""
End If
RunFlag = Index - 10
Case 15 '如果点击了等号
If Epual = False Then
FirstNum = True
Text1.Text = myval(strnum1)
Else
Text1.Text = myval(Reckon)
Epual = False
End If
Case 16 '如果点击了清除
num1 = 0
num2 = 0
strnum1 = ""
strnum2 = ""
FirstNum = True
Epual = False
RunFlag = 0
Text1.Text = "0"
Case 17 '如果点击了正负号
'调用正、负号转换过程
SignInterconvert
Text1.Text = strnum1
Case 18
Unload Me
Case 19 '如果点击了退格
strnum1 = Text1.Text
'调用删除过程
Delect
Text1.Text = strnum1
End Select
End Sub
Private Sub Command1_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
'Print KeyCode
Select Case KeyCode
Case 96 To 105 '如果点击了数字键
Call Command1_Click(KeyCode - 96)
Case 110 '如果点击了小数点
Call Command1_Click(10)
Case 107 '如果点击了"+"算数运算符
Call Command1_Click(11)
Case 109 '如果点击了"-"算数运算符
Call Command1_Click(12)
Case 106 '如果点击了"*"算数运算符
Call Command1_Click(13)
Case 111 '如果点击了"/"算数运算符
Call Command1_Click(14)
Case 187 '如果点击了等号
Call Command1_Click(15)
Case 8 '如果点击了清除
Call Command1_Click(19)
Case 27
Unload Me
Case 8 '如果点击了退格
Call Command1_Click(19)
End Select
End Sub
Private Sub Form_Load() '窗体激活
Picture1.Picture = LoadPicture(App.Path & "\CALC.ico")
num1 = 0
num2 = 0
strnum1 = ""
strnum2 = ""
FirstNum = True
Epual = False '等号为假
RunFlag = 0 '运算符为0
Text1.Text = "0"
End Sub
'模块
Public FirstNum As Boolean '判断是否以数字开头
Public Epual As Boolean '判断是否点击了运算符
Public RunFlag As Double '存放运算符索引
Public sum As Double
Sub Delect() '自定义退格过程
If Len(Trim(strnum1)) = 4 And InStr(strnum1, "-0.") = 1 Then
strnum1 = "0"
FirstNum = True
ElseIf Len(Trim(strnum1)) = 3 And InStr(strnum1, "0.") = 1 Then
strnum1 = "0"
FirstNum = True
ElseIf Len(Trim(strnum1)) = 2 And InStr(strnum1, "-") = 1 Then
strnum1 = "0"
FirstNum = True
End If
If Len(Trim(strnum1)) > 1 Then
'如果退格遇到右起第二位是小数点时退两格
If Mid(strnum1, Len(strnum1) - 1, 1) = "." Then
strnum1 = Left(strnum1, Len(strnum1) - 2)
Else
'Left函数返回从左边取指定个数的字符
strnum1 = Left(strnum1, Len(strnum1) - 1)
End If
Else
strnum1 = "0"
FirstNum = True
End If
End Sub
'自定义正、负号互换过程
Sub SignInterconvert()
'***********************************************
'*** Len函数返回字符串的长度 ***
'*** Sgn函数返回一个数的符号值 ***
'*** Right函数返回从右边取指定个数的字符 ***
'***********************************************
If Sgn(Val(strnum1)) = 1 Then
strnum1 = myval(0 - LTrim(strnum1))
ElseIf Sgn(Val(strnum1)) = -1 Then
strnum1 = Right(strnum1, Len(strnum1) - 1)
Else
strnum1 = "0"
End If
End Sub
'自定义计算结果的函数
Function Reckon()
Dim strnum As String
num1 = myval(strnum2)
num2 = myval(strnum1)
Select Case RunFlag
Case 1
sum = num1 + num2
Verdict
Case 2
sum = num1 - num2
Verdict
Case 3
On Error GoTo err1
sum = num1 * num2
Verdict
Case 4
On Error GoTo err1
If num1 / num2 < 1 And num1 / num2 > 0 Then
'If Left(LTrim(Str(num1 / num2)), 1) = "." Then
strnum = "0" & num1 / num2
sum = myval(Trim(strnum))
ElseIf num1 / num2 = 0 Then
sum = 0
FirstNum = True
Else
sum = num1 / num2
End If
End Select
strnum2 = Str(sum)
strnum1 = strnum2
Reckon = strnum1
err1:
If Err.Number = 11 Then
sum = 0
FirstNum = True
End If
'Resume Next
End Function
Sub Verdict()
If sum = 0 Then
FirstNum = True
End If
End Sub
'数字转换文本函数
Public Function myval(ByVal tt11 As Double) As String
Dim tt As String
tt = Trim(Str(tt11))
If InStr(1, tt, ".") > 0 Then
If Left(tt, 1) = "." Then
tt = "0" & tt
End If
End If
If InStr(1, tt, "-.") > 0 Then
tt = "-0." & Right(tt, Len(tt) - 2)
End If
myval = tt
End Function