前端之家收集整理的这篇文章主要介绍了
【VB】 写小计算器,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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 Form_Load() '给变量赋初值
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
End Sub
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0 To 9
If FirstNum Then 'FirstNum=True 处理第一次数字输入
StrNum1 = Str(Index) '实型转字符型
FirstNum = False '赋初值为假,输入一个数字后标记是否首字符为假
Else '非初赋值
StrNum1 = StrNum1 + Str(Index) '从第二次输入开始往后面写字符
End If
Text1.Text = StrNum1 '显示
Case 10
If Not PointFlag Then 'PointFlag=False 处理第一次点击"."
If FirstNum Then 'FirstNum=True,还没有输入数字的时候
StrNum1 = "0."
FirstNum = False '输入"."后,标记是否首字符为假
Else
StrNum1 = StrNum1 + "." '前面已有输入的时候往后一位添加"."
End If
' Else 'PointFlag=True 已有小数点
' Exit Sub '退出单击事件
End If
PointFlag = True
Text1.Text = StrNum1
Case 12 To 15
FirstNum = True '还原标记值,使下一次可以输入新数据
PointFlag = False '使可输入"."
If SignFlag Then 'SignFlag=True 前面已有运算符未运算
Call Run '调用过程
Else '首次输入运算符
SignFlag = True
StrNum2 = StrNum1 '把字符串1复制给字符串2
StrNum1 = "" '字符串1清空
End If
Runsign = Index - 11 '储存键入的运算符
Case 11 '等号
If Not SignFlag Then 'SignFlag=False 还没有运算符的时候直接显示StrNum1
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
原文链接:https://www.f2er.com/vb/259209.html