功能要求:
1、 能进行加减乘除4种运算
2、 可以清除运行结果
3、 输错的数字可以退格改正
vb制作简易计算器
答案:5 悬赏:0 手机版
解决时间 2021-02-16 08:32
- 提问者网友:眉目添风霜
- 2021-02-16 04:12
最佳答案
- 五星知识达人网友:纵马山川剑自提
- 2020-01-11 00:47
初学者的VB计算器
窗体代码如下
Option Explicit
Dim blnStratrOperationFlag As Boolean '是否开始运算
Dim lngOperationFlag As Long '运算标志
Dim dblFirstOperationValue As Double '先前的操作数
Private Sub cmdValuedDecimal_Click(Index As Integer) '数字及小数点
Select Case Index
Case 0
AddTextOperationValue "0"
Case 1
AddTextOperationValue "1"
Case 2
AddTextOperationValue "2"
Case 3
AddTextOperationValue "3"
Case 4
AddTextOperationValue "4"
Case 5
AddTextOperationValue "5"
Case 6
AddTextOperationValue "6"
Case 7
AddTextOperationValue "7"
Case 8
AddTextOperationValue "8"
Case 9
AddTextOperationValue "9"
Case 10
AddTextOperationValue "."
End Select
End Sub
Private Sub AddTextOperationValue(strOperationValue As String)
If Len(txtOperationValue) > 8 And blnStratrOperationFlag = False Then Exit Sub '判断是否输入数字超过9个
If txtOperationValue = "0" And strOperationValue = "0" Then Exit Sub '是否什么也没输入或为0时候输入0则退出
If lngOperationFlag <> 0 And blnStratrOperationFlag = True Then '如果有操作符且开始运算为真则
txtOperationValue = ""
blnStratrOperationFlag = False
End If
If txtOperationValue = "0" And strOperationValue <> "." Then txtOperationValue = "" '头次输入数字
If Right$(txtOperationValue, 1) = "." And strOperationValue = "." Then Exit Sub '避免多次输入小数点
txtOperationValue = txtOperationValue & strOperationValue '累加字符
End Sub
Private Sub cmdOperation_Click(Index As Integer) '+ - * / =
Select Case Index
Case 0
lngOperationFlag = 1
Evaluate
Case 1
lngOperationFlag = 2
Evaluate
Case 2
lngOperationFlag = 3
Evaluate
Case 3
lngOperationFlag = 4
Evaluate
Case 4
DisposeResult
End Select
End Sub
Private Sub Evaluate() '四则运算赋值
dblFirstOperationValue = Val(txtOperationValue)
blnStratrOperationFlag = True
txtOperationValue = ""
End Sub
Private Sub DisposeResult() '处理计算结果
On Error GoTo ToExit '打开错误陷阱
Select Case lngOperationFlag '操作标志
Case 1
txtOperationValue = dblFirstOperationValue + Val(txtOperationValue)
Case 2
txtOperationValue = dblFirstOperationValue - Val(txtOperationValue)
Case 3
txtOperationValue = dblFirstOperationValue * Val(txtOperationValue)
Case 4
txtOperationValue = dblFirstOperationValue / Val(txtOperationValue)
End Select
lngOperationFlag = 0 '操作标志清0
Exit Sub
ToExit:
MsgBox "除数不能为0!", vbOKOnly, "错误"
Resume Next
End Sub
Private Sub cmdClearAll_Click() 'C按钮清除所有的运算结果
lngOperationFlag = 0
txtOperationValue = "0"
End Sub
Private Sub cmdClearLastInput_Click() 'CE按钮清除最后一次输入的数字
txtOperationValue = "0"
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set frmCalculator = Nothing
End Sub
窗体代码如下
Option Explicit
Dim blnStratrOperationFlag As Boolean '是否开始运算
Dim lngOperationFlag As Long '运算标志
Dim dblFirstOperationValue As Double '先前的操作数
Private Sub cmdValuedDecimal_Click(Index As Integer) '数字及小数点
Select Case Index
Case 0
AddTextOperationValue "0"
Case 1
AddTextOperationValue "1"
Case 2
AddTextOperationValue "2"
Case 3
AddTextOperationValue "3"
Case 4
AddTextOperationValue "4"
Case 5
AddTextOperationValue "5"
Case 6
AddTextOperationValue "6"
Case 7
AddTextOperationValue "7"
Case 8
AddTextOperationValue "8"
Case 9
AddTextOperationValue "9"
Case 10
AddTextOperationValue "."
End Select
End Sub
Private Sub AddTextOperationValue(strOperationValue As String)
If Len(txtOperationValue) > 8 And blnStratrOperationFlag = False Then Exit Sub '判断是否输入数字超过9个
If txtOperationValue = "0" And strOperationValue = "0" Then Exit Sub '是否什么也没输入或为0时候输入0则退出
If lngOperationFlag <> 0 And blnStratrOperationFlag = True Then '如果有操作符且开始运算为真则
txtOperationValue = ""
blnStratrOperationFlag = False
End If
If txtOperationValue = "0" And strOperationValue <> "." Then txtOperationValue = "" '头次输入数字
If Right$(txtOperationValue, 1) = "." And strOperationValue = "." Then Exit Sub '避免多次输入小数点
txtOperationValue = txtOperationValue & strOperationValue '累加字符
End Sub
Private Sub cmdOperation_Click(Index As Integer) '+ - * / =
Select Case Index
Case 0
lngOperationFlag = 1
Evaluate
Case 1
lngOperationFlag = 2
Evaluate
Case 2
lngOperationFlag = 3
Evaluate
Case 3
lngOperationFlag = 4
Evaluate
Case 4
DisposeResult
End Select
End Sub
Private Sub Evaluate() '四则运算赋值
dblFirstOperationValue = Val(txtOperationValue)
blnStratrOperationFlag = True
txtOperationValue = ""
End Sub
Private Sub DisposeResult() '处理计算结果
On Error GoTo ToExit '打开错误陷阱
Select Case lngOperationFlag '操作标志
Case 1
txtOperationValue = dblFirstOperationValue + Val(txtOperationValue)
Case 2
txtOperationValue = dblFirstOperationValue - Val(txtOperationValue)
Case 3
txtOperationValue = dblFirstOperationValue * Val(txtOperationValue)
Case 4
txtOperationValue = dblFirstOperationValue / Val(txtOperationValue)
End Select
lngOperationFlag = 0 '操作标志清0
Exit Sub
ToExit:
MsgBox "除数不能为0!", vbOKOnly, "错误"
Resume Next
End Sub
Private Sub cmdClearAll_Click() 'C按钮清除所有的运算结果
lngOperationFlag = 0
txtOperationValue = "0"
End Sub
Private Sub cmdClearLastInput_Click() 'CE按钮清除最后一次输入的数字
txtOperationValue = "0"
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set frmCalculator = Nothing
End Sub
全部回答
- 1楼网友:底特律间谍
- 2019-08-01 05:51
Dim OperatorState As Integer
Dim data1 As Integer
Dim data2 As Integer
Dim data3 As Integer
Dim result As Integer
Private Sub Command1_Click()
Text1.Text = "7"
End Sub
Private Sub Command10_Click()
Text1.Text = "6"
End Sub
Private Sub Command11_Click()
Text1.Text = "3"
End Sub
Private Sub Command12_Click()
Text1.Text = ""
End Sub
Private Sub Command13_Click()
data1 = Val(Text1.Text)
OperatorState = 1
Text1.Text = ""
End Sub
Private Sub Command14_Click()
data1 = Val(Text1.Text)
OperatorState = 2
Text1.Text = Text1.Text + ""
End Sub
Private Sub Command15_Click()
data1 = Val(Text1.Text)
OperatorState = 3
Text1.Text = Text1.Text + ""
End Sub
Private Sub Command16_Click()
data1 = Val(Text1.Text)
OperatorState = 4
Text1.Text = Text1.Text + ""
End Sub
Private Sub Command2_Click()
Text1.Text = "4"
End Sub
Private Sub Command3_Click()
Text1.Text = "1"
End Sub
Private Sub Command4_Click()
Text1.Text = "0"
End Sub
Private Sub Command5_Click()
Text1.Text = "8"
End Sub
Private Sub Command9_Click()
Text1.Text = "9"
End Sub
Private Sub Command6_Click()
Text1.Text = "5"
End Sub
Private Sub Command7_Click()
Text1.Text = "2"
End Sub
Private Sub Command8_Click()
data2 = Val(Text1.Text)
If (OperatorState = 1) Then
result = data1 + data2
labvarious = labvarious & Text1.Text
End If
If (OperatorState = 2) Then
result = data1 - data2
End If
If (OperatorState = 3) Then
result = data1 * data2
End If
If (OperatorState = 4) Then
result = data1 / data2
End If
Text1.Text = Str(result)
End Sub
- 2楼网友:低血压的长颈鹿
- 2020-01-13 19:41
Private Sub Command1_Click()
If Not Text1.Text = "" Then Text1.Text = Text1.Text & 7
If Text1.Text = "" Then Text1.Text = 7
End Sub
Private Sub Command10_Click()
If Not Text1.Text = "" Then Text1.Text = Text1.Text & 6
If Text1.Text = "" Then Text1.Text = 6
End Sub
Private Sub Command11_Click()
If Not Text1.Text = "" Then Text1.Text = Text1.Text & 3
If Text1.Text = "" Then Text1.Text = 3
End Sub
Private Sub Command12_Click()
If Text1.Text = "" Then MsgBox "你必须要先输个数啊!嗯~~~"
If Text2.Text = "" Then Text2.Text = Text1.Text
If Not Text1.Text = "" Then Text2.Text = Val(Text2.Text) / Val(Text1.Text)
If Not Text1.Text = "" Then Text2.Text = Text1.Text
End Sub
Private Sub Command13_Click()
Text1.Text = Int(Val(Text1.Text) / 10)
End Sub
Private Sub Command14_Click()
If Text1.Text = "" Then MsgBox "你必须要先输个数啊!喵~~~ 如果你是按照公告的那么请放心正常运算,当输出值为1说明正常启动了!"
If Text1.Text = "" Then Text2.Text = 1
If Not Text1.Text = "" Then Text2.Text = Val(Text1.Text) * Val(Text2.Text)
If Not Text1.Text = "" Then Text2.Text = Text1.Text
If Not Text2.Text = "" Then Label2 = "乘多少?"
End Sub
Private Sub Command15_Click()
If Text1.Text = "" Then MsgBox "你必须要先输个数啊!哈~~~"
If Not Text1.Text = "" Then Text2.Text = Val(Text1.Text) + Val(Text2.Text)
If Not Text1.Text = "" Then Text2.Text = Text1.Text
If Not Text2.Text = "" Then Label2 = "加多少?"
End Sub
Private Sub Command16_Click()
MsgBox " 正常的加减乘除都是正常的,当你使用混合加减乘除时。切记,必须要先写上你要乘,除,加,减或二次方的数字再点击加减乘除符号,否则会造成数据复位,前面的会白算的,这与计算器是一个道理。举例来说本来是(2+2)/2的按扭顺序,在这里是2 + 2 2 / 举例2:要计算((2+3-4)*5)/6 应该 2 + 3 4 - 5 * 6 / 这个顺序进行。很抱歉有点小啰嗦哈!"
End Sub
Private Sub Command17_Click()
If Text1.Text = "" Then MsgBox "你必须要先输个数啊!哒~~~"
If Not Text1.Text = "" Then Text2.Text = Val(Text1.Text) - Val(Text2.Text)
If Not Text1.Text = "" Then Text2.Text = Text1.Text
End Sub
Private Sub Command18_Click()
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub Command19_Click()
If Text1.Text = "" Then MsgBox "缺少二次方的对象!"
If Not Text1.Text = "" Then Text2.Text = Val(Text1.Text) * Val(Text1.Text)
End Sub
Private Sub Command2_Click()
If Not Text1.Text = "" Then Text1.Text = Text1.Text & 4
If Text1.Text = "" Then Text1.Text = 4
End Sub
Private Sub Command20_Click()
If Text1.Text = "" Then MsgBox "缺少三次方的对象!"
If Not Text1.Text = "" Then Text2.Text = Val(Text1.Text) * Val(Text1.Text) * Val(Text1.Text)
End Sub
Private Sub Command21_Click()
If Text2.Text = "" Then MsgBox "缺少二次方的对象!"
If Not Text2.Text = "" Then Text2.Text = Val(Text2.Text) * Val(Text2.Text)
End Sub
Private Sub Command22_Click()
If Text2.Text = "" Then MsgBox "缺少三次方的对象!"
If Not Text2.Text = "" Then Text2.Text = Val(Text2.Text) * Val(Text2.Text) * Val(Text2.Text)
End Sub
Private Sub Command3_Click()
If Not Text1.Text = "" Then Text1.Text = Text1.Text & 1
If Text1.Text = "" Then Text1.Text = 1
End Sub
Private Sub Command4_Click()
If Not Text1.Text = "" Then Text1.Text = Text1.Text & 0
If Text1.Text = "" Then Text1.Text = 0
End Sub
Private Sub Command5_Click()
If Not Text1.Text = "" Then Text1.Text = Text1.Text & 8
If Text1.Text = "" Then Text1.Text = 8
End Sub
Private Sub Command6_Click()
If Not Text1.Text = "" Then Text1.Text = Text1.Text & 5
If Text1.Text = "" Then Text1.Text = 5
End Sub
Private Sub Command7_Click()
If Not Text1.Text = "" Then Text1.Text = Text1.Text & 2
If Text1.Text = "" Then Text1.Text = 2
End Sub
Private Sub Command8_Click()
If Not Text1.Text = "" Then Text1.Text = Text1.Text & "."
If Text1.Text = "" Then MsgBox "小数点不能乱加的啦(* ̄︶ ̄)~~~"
End Sub
Private Sub Command9_Click()
If Not Text1.Text = "" Then Text1.Text = Text1.Text & 9
If Text1.Text = "" Then Text1.Text = 9
End Sub
Private Sub Text2_Change()
If Not Text2.Text = "" Then Text1.Text = ""
If Val(Text2.Text) > 9.99973000350998E+85 Then MsgBox "EFFORT错误!结果过大请点击AC重新启动!!!"
If Val(Text2.Text) > 9.99973000350998E+85 Then Text2.Text = "EFFORT错误!结果过大请点击AC重新启动!!!"
End Sub
Private Sub Timer1_Timer()
Label1.Caption = " 当前时间为" & Now & "---独家计算器"
End Sub
根据图片command的顺序和label的顺序弄好后
再将以上代码复制粘贴进去,这个代码适用于初学者但效果一致。效果图如下.
记住!复制内容是开头Private到结尾的End Sub
开心的同时还是好好学吧.
那个plfushicn的方法也可以只是不太适合初学者.
望采纳!还有不会的随时问我!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯