在VB中计算器的代码(要求0到9的加减乘除运算)只要0到9的加减乘除还有等于号其他都不需要
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/15 07:42:31
在VB中计算器的代码(要求0到9的加减乘除运算)只要0到9的加减乘除还有等于号其他都不需要
在VB中计算器的代码(要求0到9的加减乘除运算)
只要0到9的加减乘除还有等于号其他都不需要
在VB中计算器的代码(要求0到9的加减乘除运算)只要0到9的加减乘除还有等于号其他都不需要
Dim Num1 As Double
Dim Num2 As Double
Dim Equal As Double
Dim M As Double
Dim Equal2 As Integer
Dim StrNum1 As String
Dim StrNum2 As String
Dim FirstPoint As Boolean
Dim FirstNum As Boolean
Private Sub Command1_Click(Index As Integer) '数字输入
Select Case Index
Case 0 To 9
StrNum1 = LTrim(StrNum1) + LTrim(Str(Index))
Do While Left(LTrim(StrNum1), 1) = "0" '去掉高位数字0
StrNum1 = Mid(StrNum1, 2)
Loop
If Left(LTrim(StrNum1), 1) = "." Then StrNum1 = "0" + LTrim(StrNum1)
Text1.Text = StrNum1 '数字显示
Case 10
If FirstPoint Then '小数点的输入
StrNum1 = LTrim(StrNum1) + LTrim(".")
Do While Left(LTrim(StrNum1), 1) = "0" '去掉高位数字0
StrNum1 = Mid(StrNum1, 2)
Loop
If Left(LTrim(StrNum1), 1) = "." Then StrNum1 = "0" + LTrim(StrNum1)
Text1.Text = StrNum1 '数字显示
FirstPoint = False
Text1.Text = StrNum1
End If
Case 11
StrNum1 = Text1.Text '退格
If Len(StrNum1) > 0 Then
StrNum1 = Left(StrNum1, Len(StrNum1) - 1) '每一次退格,从字符串后面去掉一个字符
Text1.Text = StrNum1
Else
Text1.Text = "0.0"
End If
If InStr(StrNum1, ".") = 0 Then FirstPoint = True '若字符串中没有小数点,把小数点开关打开
Case 12 '清零
StrNum1 = ""
StrNum2 = ""
Text1.Text = "0.0"
aa = ""
Equal = 0
Num1 = 0
Num2 = 0
FirstPoint = True '小数点开关
FirstNum = True '第一个数开关
Case 13 '正负符号
If Val(StrNum1) > 0 Then
StrNum1 = "-" + LTrim(StrNum1)
Else
StrNum1 = Abs(Val(StrNum1))
End If
Text1.Text = StrNum1
Case 14 '电源开关
End
End Select
End Sub
Private Sub Command2_Click(Index As Integer) '+-*/运算
Equal2 = Index '=号开关
If FirstNum Then '存第一个数,其中FirstNum为开关
StrNum2 = StrNum1
StrNum1 = ""
FirstNum = False
FirstPoint = True '小数点开,以便输入第二个数
Else '若不是第一个数进入下面的+-*/运算
Select Case Index
Case 0 '0为加法
Num1 = Val(StrNum2) 'Num1存第一个数
Num2 = Val(StrNum1) 'Num2存第二个数
Equal = Num1 + Num2 'Equal存结果
StrNum2 = Str(Equal) 'StrNum2返回结果,以便下次让Num1存结果
StrNum1 = "" 'StrNum1清空,以便下次数字输入
aa = Str(Equal)
If Left(LTrim(aa), 1) = "-" And Left(LTrim(aa), 2) = "-." Then aa = "-0" + LTrim(Abs(Equal))
If Left(LTrim(aa), 1) = "." Then aa = "0" + LTrim(aa)
Text1.Text = aa '显示结果
FirstPoint = True '小数点开
Case 1 '1为减法
Num1 = Val(StrNum2)
Num2 = Val(StrNum1)
Equal = Num1 - Num2
StrNum2 = Str(Equal)
StrNum1 = ""
aa = Str(Equal)
If Left(LTrim(aa), 1) = "-" And Left(LTrim(aa), 2) = "-." Then aa = "-0" + LTrim(Abs(Equal))
If Left(LTrim(aa), 1) = "." Then aa = "0" + LTrim(aa)
Text1.Text = aa
FirstPoint = True
Case 2 '为乖法
Num1 = Val(StrNum2)
If StrNum1 = "" Then '便于+-*/之间切换
Num2 = 1
Else
Num2 = Val(StrNum1)
End If
Equal = Num1 * Num2
StrNum2 = Str(Equal)
StrNum1 = ""
aa = Str(Equal)
If Left(LTrim(aa), 1) = "-" And Left(LTrim(aa), 2) = "-." Then aa = "-0" + LTrim(Abs(Equal))
If Left(LTrim(aa), 1) = "." Then aa = "0" + LTrim(aa)
Text1.Text = aa
FirstPoint = True
Case 3 '3为除法
Num1 = Val(StrNum2) 'Num1存第一个数
If StrNum1 = "" Then
Num2 = 1
Else
Num2 = Val(StrNum1)
End If
Equal = Num1 / Num2
StrNum2 = Str(Equal) 'Num2存第二个数
StrNum1 = ""
aa = Str(Equal)
If Left(LTrim(aa), 1) = "-" And Left(LTrim(aa), 2) = "-." Then aa = "-0" + LTrim(Abs(Equal))
If Left(LTrim(aa), 1) = "." Then aa = "0" + LTrim(aa)
Text1.Text = aa
FirstPoint = True
End Select
End If
End Sub
Private Sub Command3_Click() '=号运算
FirstNum = True '第一个数关,关掉连继+-*/
Select Case Equal2 'Equal2为=号按键判断+-*/哪种运算
Case 0 '+运算
If StrNum2 = "" Then
Num1 = Equal
Else
Num1 = Val(StrNum2)
End If
Num2 = Val(Text1.Text)
Equal = Num1 + Num2
StrNum1 = "" '返回结果
aa = Str(Equal)
If Left(LTrim(aa), 1) = "-" And Left(LTrim(aa), 2) = "-." Then aa = "-0" + LTrim(Abs(Equal))
If Left(LTrim(aa), 1) = "." Then aa = "0" + LTrim(aa)
Text1.Text = aa '显示结果
FirstPoint = True '小数点开
Case 1 '-运算
If StrNum2 = "" Then
Num1 = Equal
Else
Num1 = Val(StrNum2)
End If
Num2 = Val(Text1.Text)
Equal = Num1 - Num2
StrNum1 = ""
aa = Str(Equal)
If Left(LTrim(aa), 1) = "-" And Left(LTrim(aa), 2) = "-." Then aa = "-0" + LTrim(Abs(Equal))
If Left(LTrim(aa), 1) = "." Then aa = "0" + LTrim(aa)
Text1.Text = aa
FirstPoint = True
Case 2 '*运算
If StrNum2 = "" Then
Num1 = Equal
Else
Num1 = Val(StrNum2)
End If
If StrNum1 = "" Then
Num2 = 1
Else
Num2 = Val(Text1.Text)
End If
Equal = Num1 * Num2
StrNum1 = ""
aa = Str(Equal)
If Left(LTrim(aa), 1) = "-" And Left(LTrim(aa), 2) = "-." Then aa = "-0" + LTrim(Abs(Equal))
If Left(LTrim(aa), 1) = "." Then aa = "0" + LTrim(aa)
Text1.Text = aa
FirstPoint = True
Case 3 '/运算
If StrNum2 = "" Then
Num1 = Equal
Else
Num1 = Val(StrNum2)
End If
If StrNum1 = "" Then
Num2 = 1
Else
Num2 = Val(Text1.Text)
End If
Equal = Num1 / Num2
StrNum1 = ""
aa = Str(Equal)
If Left(LTrim(aa), 1) = "-" And Left(LTrim(aa), 2) = "-." Then aa = "-0" + LTrim(Abs(Equal))
If Left(LTrim(aa), 1) = "." Then aa = "0" + LTrim(aa)
Text1.Text = aa
FirstPoint = True
End Select
End Sub
Private Sub Command4_Click() '开方运算
aa = Val(Text1.Text)
StrNum1 = ""
If aa >= 0 Then
aa = Str(Sqr(aa))
If Left(LTrim(aa), 1) = "." Then aa = "0" + LTrim(aa)
Text1.Text = aa
Else
MsgBox "被开方数少于零"
End If
End Sub
Private Sub Command7_Click() '倒数运算
aa = Val(Text1.Text)
StrNum1 = ""
aa = Str(1 / aa)
If Left(LTrim(aa), 1) = "-" And Left(LTrim(aa), 2) = "-." Then aa = "-0" + LTrim(Abs(Equal))
If Left(LTrim(aa), 1) = "." Then aa = "0" + LTrim(aa)
Text1.Text = aa
End Sub
Private Sub Command8_Click(Index As Integer) '存贮器操作
Select Case Index
Case 0 'MC,存贮器清零
Text2.Text = ""
M = 0
Case 1 'MR,显示存贮器内容