admin 管理员组文章数量: 887021
2024年1月23日发(作者:克鲁斯卡尔算法java)
VB6.0的小程序 计算器
对于刚入门学习VB6的朋友来说肯定会做些小程序吧,这里就是给大家演示个简单的计算器程序,仅供参考啦。
界面上加减乘除四个按钮分别是cmdAdd、cmdPlus、cmdMultiple、cmdDevide,小数点按钮是cmdDot,负号按钮是cmdMinuse,数字0~9为了偷懒,用了控件数组cmdNumber(0)~ cmdNumber(9),上面txtShow是显示数字和结果用的,txtOperate是显示中间步骤的。
思路大致是这样,点加减乘除这类操作符的时候,把当前txtShow的值保存在模块变量mstrParam1里,同时把操作符保存到mstrOperate里,按等于号时把先把当前txtShow的值保存在模块变量mstrParam2里,然后对mstrParam1和mstrParam2进行运算,当然要记得设法把String转换成数值进行运算。
转换的过程要注意,这里是用的Variant数据类型,vParam1和vParam2都是Variant类型,保存的是mstrParam1和mstrParam2的数值。之所以不用integer、long、double这些标准类型,是因为这些类型都有大小限制,做出来用着不方便,VB最大的整型long才到
2147483647,这意味着计算器的计算结果只能限制在9位到10位。而Variant类型可以支持非常大的数,具体多大不清楚,但起码几十位是能够支持的。另外,最后算完的结果也要做格式化,因为如果数值非常大的话,VB会自动转成科学计数法,所以要用Format函数进行调整。
如果需要源代码的话在我百度空间里留言。/zhaozhigang517
Option Explicit
Private Const mstrFORMATDEFAULT As String = "#.##"
Private mstrParam1 As String, mstrParam2 As String
Private mstrOperate As String
Private Sub cmdAdd_Click()
mstrParam1 = Trim()
mstrOperate = "+"
= ""
= & mstrParam1 & vbCrLf
= & mstrOperate & vbCrLf
End Sub
Private Sub cmdPlus_Click()
mstrParam1 = Trim()
mstrOperate = "-"
= ""
= & mstrParam1 & vbCrLf
= & mstrOperate & vbCrLf
End Sub
Private Sub cmdMultiple_Click()
mstrParam1 = Trim()
mstrOperate = "*"
= ""
= & mstrParam1 & vbCrLf
= & mstrOperate & vbCrLf
End Sub
Private Sub cmdDevide_Click()
mstrParam1 = Trim()
mstrOperate = "/"
= ""
= & mstrParam1 & vbCrLf
= & mstrOperate & vbCrLf
End Sub
Private Sub cmdDot_Click()
Dim mstrShow As String
Dim i As Integer
mstrShow = "."
i = InStr(1, , mstrShow)
If i <= 0 Then
= & mstrShow
End If
End Sub
Private Sub cmdMinuse_Click()
Dim mstrShow As String
Dim i As Integer
mstrShow = "-"
i = InStr(1, , mstrShow)
If i <= 0 Then
= mstrShow &
Else
= Right(, Len() - 1)
End If
End Sub
Private Sub cmdNumber_Click(Index As Integer)
Dim mstrShow As String
mstrShow = Index
= & mstrShow
End Sub
Private Sub cmdClear_Click()
= ""
End Sub
Private Sub cmdResult_Click()
Dim vParam1 As Variant, vParam2 As Variant
Dim vResult As Variant
Dim strResult As String
Dim i As Long
Dim blnBeforeDot As Boolean
Dim lngDotPos As Long
Dim blnMinuse As Boolean
mstrParam2 = Trim()
= & mstrParam2 & vbCrLf
vParam1 = 0
blnBeforeDot = True
lngDotPos = -1
blnMinuse = False
For i = 1 To Len(mstrParam1)
If IsNumeric(Mid(mstrParam1, i, 1)) = True Then
If blnBeforeDot = True Then
vParam1 = vParam1 * 10 + CByte(Mid(mstrParam1, i, 1))
Else
vParam1 = vParam1 + CByte(Mid(mstrParam1, i, 1)) * (0.1 ^ (i - lngDotPos))
End If
ElseIf Mid(mstrParam1, i, 1) = "." Then
blnBeforeDot = False
lngDotPos = i
ElseIf Mid(mstrParam1, i, 1) = "-" Then
blnMinuse = True
End If
Next i
If blnMinuse = True Then
vParam1 = -vParam1
End If
vParam2 = 0
blnBeforeDot = True
lngDotPos = -1
blnMinuse = False
For i = 1 To Len(mstrParam2)
If IsNumeric(Mid(mstrParam2, i, 1)) = True Then
If blnBeforeDot = True Then
vParam2 = vParam2 * 10 + CByte(Mid(mstrParam2, i, 1))
Else
vParam2 = vParam2 + CByte(Mid(mstrParam2, i, 1)) * (0.1 ^ (i - lngDotPos))
End If
ElseIf Mid(mstrParam2, i, 1) = "." Then
blnBeforeDot = False
lngDotPos = i
ElseIf Mid(mstrParam2, i, 1) = "-" Then
blnMinuse = True
End If
Next i
If blnMinuse = True Then
vParam2 = -vParam2
End If
Select Case mstrOperate
Case "+"
vResult = vParam1 + vParam2
Case "-"
vResult = vParam1 - vParam2
Case "*"
vResult = vParam1 * vParam2
Case "/"
vResult = vParam1 / vParam2
End Select
= & "=" & vbCrLf
strResult = Format(vResult, mstrFORMATDEFAULT)
= strResult
= & strResult & vbCrLf
End Sub
Private Sub Form_Load()
= ""
= ""
End Sub
版权声明:本文标题:VB6.0计算器 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1705987634h496825.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论