请教一个简单计算器的编写方法
我是新手,想知道一个计算器的大概编程思路,还有计算器设计中小数点和退格的处理方法,谢谢大家了
给你个代码例子,自己研究吧!
把控件先建好!
0-9 +-*/ . =
text1
代码
Dim Num1, Num2 As Single
Dim StrNum1, StrNum2 As String
Dim FirstNum As Boolean
Dim PointFlag As Boolean
Dim Runsign As Integer
Dim SignFlag As Boolean
Private Sub command1_Click(Index As Integer)
Select Case Index
Case 0 To 9
If FirstNum Then
StrNum1 = str(Index)
FirstNum = False
Else
StrNum1 = LTrim(StrNum1) + LTrim(str(Index))
End If
Text1.Text = Val(StrNum1)
Case 10
If Not PointFlag Then
If FirstNum Then
StrNum1 = "0."
FirstNum = False
Else
StrNum1 = LTrim(StrNum1) + LTrim(".")
End If
Else
Exit Sub
End If
PointFlag = True
Text1.Text = Val(StrNum1)
Case 12 To 15
FirstNum = True
PointFlag = False
If SignFlag Then
Call Run
Else
SignFlag = True
StrNum2 = StrNum1
StrNum1 = ""
End If
Runsign = Index - 11
Case 11
On Error Resume Next
If Not SignFlag Then
Text1.Text = StrNum1
equal = Val(StrNum1)
FirstNum = True
PointFlag = False
Else
Call Run
SignFlag = False
End If
Case Else
Call ClearData
End Select
End Sub
Private Sub Form_Load()
Me.Top = 100
Me.Left = 8000
If App.PrevInstance Then MsgBox "请不要多次运行此程序,谢谢!", vbAbortRetryIgnore: Unload Me
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
End Sub
Sub Run()
On Error Resume Next
Dim equal As Single
Dim equal2 As Double
Num1 = Val(StrNum2)
Num2 = Val(StrNum1)
Select Case Runsign
Case 1
equal = Num1 + Num2
If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
equal = 0
equal2 = Num1 + Num2
End If
Case 2
equal = Num1 - Num2
If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
equal = 0
equal2 = Num1 - Num2
End If
Case 3
equal = Num1 * Num2
If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
equal = 0
equal2 = Num1 * Num2
End If
Case 4
equal = Num1 / Num2
If Num1 >= 65536 Or Num2 >= 65536 Or equal >= 65536 Then
equal = 0
equal2 = Num1 / Num2
End If
End Select
If equal = 0 Then a = equal2 Else a = equal
StrNum2 = str(a)
StrNum1 = StrNum2
Text1.Text = Val(StrNum2)
End Sub
Sub ClearData()
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
Text1.Text = 0
End Sub