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 '判断是否已有运算符号
Sub Run() ' 建立过程
Num1 = Val(StrNum2)
Num2 = Val(StrNum1)
Select Case Runsign
Case 1 '加
equal = Num1 + Num2
Case 2 '减
equal = Num1 - Num2
Case 3 '乘
equal = Num1 * Num2
Case 4 '除
equal = Num1 / Num2
End Select
StrNum2 = Str(equal)
StrNum1 = StrNum2 '(如再按同一个运算符时,在原有的数上和结果再运算一次)
Text1.Text = StrNum2
End Sub
Sub ClearData()
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
Text1.Text = "0."
End Sub
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0 To 9
If FirstNum Then
StrNum1 = Str(Index) '实型转字符型
FirstNum = False '初赋值为假
Else '非初赋值
StrNum1 = StrNum1 + Str(Index) '("+"是连接符)
End If
Text1.Text = StrNum1 '显示
Case 10
If Not PointFlag Then '如果没有小数点(not是非)
If FirstNum Then '如果是第一个字符
StrNum1 = "0."
FirstNum = False
Else
StrNum1 = StrNum1 + "."
End If
Else '已有小数点
Exit Sub '则退出单击事件
End If
PointFlag = True
Text1.Text = StrNum1
Case 12 To 15
FirstNum = True '使下一次可以输入新数据
PointFlag = False '还原标记值
If SignFlag Then '前面已有运算符未运算(signflag的值是ture)
Call Run '(调用前面建立的过程)
Else
SignFlag = True
StrNum2 = StrNum1 '把字符串1复制给字符串2
StrNum1 = "" '字符串1清空
End If
Runsign = Index - 11 '储存键入的运算符
Case 11 '(等于号的处理)
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()
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
End Sub
Private Sub Command1_Click()
Dim a As integer
Dim b As integer
Dim c As integer
a =val( Text1.Text)
b = val(Text2.Text)
c = a + b
MsgBox "结果是" & c
End Sub
这样就对了,记住"+"在VB里,对字符型变量而言,其是连接符.