Option Explicit
Dim potflag As Integer '标识是否用小数点
Dim numcol As Integer ' 点击运算符的个数
Dim LastInput ' 指示上一次操作的内容
Dim colflag 'numcol为1时,保存运算符
Dim temp1,temp2 '分别保存运算符两端的运算数
Private Sub CmdCel_Click() '重新开始计算按钮,个计量数和标识初始化
Res = Format(0,"0.")
temp1 = 0
temp2 = 0
numcol = 0
potflag = False
LastInput = "cel" '标识此操作为"cel"
End Sub
Private Sub CmdPot_Click() '使用小数点
If LastInput <> "num" Then
Res = Format(0,"0.")
ElseIf String(1,Res) = "-" Then
Res = Format(0,"-0.")
End If
potflag = True
LastInput = "num" '标识此操作为"num"
End Sub
Private Sub Cmdsign_Click() '使用正负号
If String(1,Res) <> "-" Then '如果当前是正数添加负号
Res = "-" & Res
Else: Res = Right(Res,Len(Res) - 1) '如果当前是证号,取消负号
End If
LastInput = "num" '标识此操作为"num"
End Sub
Private Sub ComCol_Click(Index As Integer) '点击操作符,可进行连续运算
numcol = numcol + 1 '通过numcol标识点击运算符的次数
If numcol = 1 Then '当前数为操作符左侧运算数时,记录当前操作数
temp1 = Res
Res = Format(0,"0.")
ElseIf numcol = 2 Then '当前操作数为操作符右侧数,进行相应运算
temp2 = Res
Select Case colflag
Case "+"
temp1 = CDbl(temp1) + CDbl(temp2)
Case "-"
temp1 = CDbl(temp1) - CDbl(temp2)
Case "*"
temp1 = CDbl(temp1) * CDbl(temp2)
Case "/"
If temp2 = 0 Then
MsgBox "输入错误,除数不能为0",vbOKOnly,"提示"
Else: temp1 = CDbl(temp1) / CDbl(temp2)
End If
Case "="
Res = temp2
End Select
Res = temp1
numcol = 0
End If
colflag = ComCol(Index).Caption '记录该次选择的操作符
LastInput = "col" '标识此操作为"col"
End Sub
Private Sub ComNum_Click(Index As Integer) '点击数字按钮
If LastInput <> "num" Then '显示需要先判断是否启用小数点,再选择显示的方法
Res = Format(0,".")
potflag = False
End If If potflag Then
Res = Res + ComNum(Index).Caption
Else: Res = Left(Res,InStr(Res,Format(0,".")) - 1) + ComNum(Index).Caption + Format(0,".")
End If
LastInput = "num" '标识此操作为"num"
End Sub
Private Sub Res_Change() 'Res为操作数和计算结果显示的文本框
End Sub
原文链接:https://www.f2er.com/vb/262371.html