龙空技术网

简易计算器代码2

编程堆代码 156

前言:

现时姐妹们对“简单计算器程序代码怎么写”大致比较珍视,各位老铁们都需要学习一些“简单计算器程序代码怎么写”的相关资讯。那么小编也在网络上搜集了一些有关“简单计算器程序代码怎么写””的相关资讯,希望我们能喜欢,看官们快快来了解一下吧!

'窗体代码

Dim arr(16) As New Class1

Dim dic As Object

Private Sub form_load()

Set dic = CreateObject("Scripting.Dictionary")

Call setDic

Text1.Text = ""

Call Init

End Sub

Private Sub Init()

Dim i%, obj As Class1

For i = 1 To 16

Set obj = arr(i)

obj.formload Me.Text1, dic

Set obj.cBtn = Me.Controls("command" & i)

Next

End Sub

Private Sub setDic()

' dic.Add "mark", ""

' dic.Add "flag", 0

' dic.Add "num1", ""

' dic.Add "num2", ""

dic("mark") = ""

dic("flag") = 0

dic("num1") = ""

dic("num2") = ""

End Sub

'类模块代码

Public WithEvents cBtn As CommandButton

Private tb1 As TextBox, dic As Object

Private Sub cBtn_click()

Dim cpt$, i%

i = Val(Mid(cBtn.Name, 8))

cpt = cBtn.Caption

If i < 11 Then

isNumber cpt

ElseIf i < 15 Then

isMark cpt

ElseIf i = 15 Then

isClac cpt

Else

Call isClear

End If

End Sub

Public Sub formload(tb As TextBox, mDic As Object)

Set tb1 = tb

Set dic = mDic

End Sub

Private Sub isClac(mark$)

Dim res$, num1$, num2$

num1 = dic("num1")

num2 = dic("num2")

If dic("mark") = "+" Then

res = Val(num1) + Val(num2)

ElseIf dic("mark") = "-" Then

res = num2 - num1

ElseIf dic("mark") = "×" Then

res = num1 * num2

Else

If num2 <> 0 Then

res = num2 / num1

Else

MsgBox "除数不能为0"

Exit Sub

End If

End If

tb1.Text = res

Call Init

dic("num1") = res

End Sub

Private Sub isClear()

Call Init

tb1 = ""

End Sub

Private Function isNumber(n$)

If dic("flag") = 0 Then: dic("num1") = ""

dic("num1") = dic("num1") & n

If dic("flag") = 0 Then: tb1.Text = ""

tb1.Text = tb1.Text & n

dic("flag") = 1

End Function

Private Function isMark(m$)

dic("flag") = 1

dic("mark") = m

tb1.Text = tb1.Text & m

dic("num2") = dic("num1")

dic("num1") = ""

End Function

Private Function Init()

dic("flag") = 0

dic("num1") = ""

dic("num2") = ""

dic("mark") = ""

End Function

标签: #简单计算器程序代码怎么写 #简单计算器程序代码怎么写出来