功能要求:
1、 能进行加减乘除4种运算
2、 可以清除运行结果
3、 输错的数字可以退格改正
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的方法也可以只是不太适合初学者.
望采纳!还有不会的随时问我!
窗体代码如下
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追问
写一下 做这题的感受 遇到什么问题 什么的 谢谢 老师留得作业
追答感受嘛,这个题太简单,就是老师留的作业太简单,而且太少,也太不负责任。所以才会教出你这样什么也不会的学生!
本回答被提问者和网友采纳这要完全做好可不是个简单的事,你参考一下我以前做的一个例子。如果要源代码的话给我邮箱,我把这个例子发给你!
设计时使用了控件数组 cmdnum 0-9 数字
cmdCaculate 0-3 表示+-*/
还用数组记录运算数据,考虑了括号
布局是在form_load()
中进行的,如果在窗体中做好了,可以考虑不要部分代码
下面是代码
Private number() As Double, i As Integer
Private strcalculate() As String
Private Sub CALCULATE(strFuhao As String)
If strFuhao = "=" And strcalculate(0, i) = "" Then Exit Sub
Select Case strFuhao '判断当前是什么运算符
Case "+" '刚输入+ ***************************************************************************************************
strcalculate(1, i) = strFuhao
Select Case strcalculate(0, i) '判断前面是什么运算
Case "+"
number(0, i) = number(0, i) + Val(txtshow.Text)
txtshow.Text = number(0, i)
Case "-"
number(0, i) = number(0, i) - Val(txtshow.Text)
txtshow.Text = number(0, i)
Case "*"
number(0, i) = number(0, i) * Val(txtshow.Text)
Select Case strcalculate(2, i) '判断乘除法前面有没有加减法要计算,有计算
Case "+"
number(0, i) = number(1, i) + number(0, i)
number(1, i) = 0
strcalculate(2, i) = ""
Case "-"
number(0, i) = number(1, i) - number(0, i)
number(1, i) = 0
strcalculate(2, i) = ""
End Select
txtshow.Text = number(0, i)
Case "/"
number(0, i) = number(0, i) / Val(txtshow.Text)
Select Case strcalculate(2, i) '判断乘除法前面有没有加减法要计算,有计算
Case "+"
number(0, i) = number(1, i) + number(0, i)
number(1, i) = 0
strcalculate(2, i) = ""
Case "-"
number(0, i) = number(1, i) - number(0, i)
number(1, i) = 0
strcalculate(2, i) = ""
End Select
txtshow.Text = number(0, i)
Case Else
number(0, i) = txtshow.Text
End Select
Case "-" '刚输入- *******************************************************************************************
strcalculate(1, i) = strFuhao
Select Case strcalculate(0, i) '判断前面是什么运算
Case "+"
number(0, i) = number(0, i) + Val(txtshow.Text)
txtshow.Text = number(0, i)
Case "-"
number(0, i) = number(0, i) - Val(txtshow.Text)
txtshow.Text = number(0, i)
Case "*"
number(0, i) = number(0, i) * Val(txtshow.Text)
Select Case strcalculate(2, i) '判断乘除法前面有没有加减法要计算,有计算
Case "+"
number(0, i) = number(1, i) + number(0, i)
number(1, i) = 0
strcalculate(2, i) = ""
Case "-"
number(0, i) = number(1, i) - number(0, i)
number(1, i) = 0
strcalculate(2, i) = ""
End Select
txtshow.Text = number(0, i)
Case "/"
number(0, i) = number(0, i) / Val(txtshow.Text)
Select Case strcalculate(2, i) '判断乘除法前面有没有加减法要计算,有计算
Case "+"
number(0, i) = number(1, i) + number(0, i)
number(1, i) = 0
strcalculate(2, i) = ""
Case "-"
number(0, i) = number(1, i) - number(0, i)
number(1, i) = 0
strcalculate(2, i) = ""
End Select
txtshow.Text = number(0, i)
Case Else
number(0, i) = txtshow.Text
End Select
Case "*" '刚输入* *******************************************************************************************
strcalculate(1, i) = strFuhao
Select Case strcalculate(0, i)
Case "*"
number(0, i) = number(0, i) * Val(txtshow.Text)
txtshow.Text = number(0, i)
Case "/"
number(0, i) = number(0, i) / Val(txtshow.Text)
txtshow.Text = number(0, i)
Case "+", "-"
number(1, i) = number(0, i)
number(0, i) = Val(txtshow.Text)
strcalculate(2, i) = strcalculate(0, i)
Case Else
number(0, i) = Val(txtshow.Text)
End Select
Case "/" '刚输入/ *******************************************************************************************
strcalculate(1, i) = strFuhao
Select Case strcalculate(0, i)
Case "*"
number(0, i) = number(0, i) * Val(txtshow.Text)
txtshow.Text = number(0, i)
Case "/"
number(0, i) = number(0, i) / Val(txtshow.Text)
txtshow.Text = number(0, i)
Case "+", "-"
number(1, i) = number(0, i)
number(0, i) = Val(txtshow.Text)
strcalculate(2, i) = strcalculate(0, i)
Case Else
number(0, i) = txtshow.Text
End Select
Case "(" '刚输入前括号 *******************************************************************************************
i = i + 1
ReDim Preserve number(2, i) As Double
ReDim Preserve strcalculate(2, i) As String
txtshow.Text = 0
Case ")" ' 刚输入后括号 *******************************************************************************************
Select Case strcalculate(0, i)
Case "+"
number(0, i) = number(0, i) + Val(txtshow.Text)
txtshow.Text = number(0, i)
i = i - 1 '让数组最后一纬减1
ReDim Preserve number(2, i) As Double
ReDim Preserve strcalculate(2, i) As String
Case "-"
number(0, i) = number(0, i) - Val(txtshow.Text)
txtshow.Text = number(0, i)
i = i - 1 '让数组最后一纬减1
ReDim Preserve number(2, i) As Double
ReDim Preserve strcalculate(2, i) As String
Case "*"
number(0, i) = number(0, i) * Val(txtshow.Text)
Select Case strcalculate(2, i)
Case "+"
number(0, i) = number(1, i) + number(0, i)
txtshow.Text = number(0, i)
i = i - 1 '让数组最后一纬减1
ReDim Preserve number(2, i) As Double
ReDim Preserve strcalculate(2, i) As String
Case "-"
number(0, i) = number(1, i) - number(0, i)
txtshow.Text = number(0, i)
i = i - 1 '让数组最后一纬减1
ReDim Preserve number(2, i) As Double
ReDim Preserve strcalculate(2, i) As String
End Select
Case Else
MsgBox "括号里没有表达式!", vbSystemModal + vbInformation, "输入有误码"
Exit Sub
End Select
Case "="
Select Case strcalculate(0, i) '判断前面是什么运算
Case "+"
number(0, i) = number(0, i) + Val(txtshow.Text)
txtshow.Text = number(0, i)
Case "-"
number(0, i) = number(0, i) - Val(txtshow.Text)
txtshow.Text = number(0, i)
Case "*"
number(0, i) = number(0, i) * Val(txtshow.Text)
Select Case strcalculate(2, i) '判断乘除法前面有没有加减法要计算,有计算
Case "+"
number(0, i) = number(1, i) + number(0, i)
number(1, i) = 0
strcalculate(2, i) = ""
Case "-"
number(0, i) = number(1, i) - number(0, i)
number(1, i) = 0
strcalculate(2, i) = ""
End Select
txtshow.Text = number(0, i)
Case "/"
number(0, i) = number(0, i) / Val(txtshow.Text)
Select Case strcalculate(2, i) '判断乘除法前面有没有加减法要计算,有计算
Case "+"
number(0, i) = number(1, i) + number(0, i)
number(1, i) = 0
strcalculate(2, i) = ""
Case "-"
number(0, i) = number(1, i) - number(0, i)
number(1, i) = 0
strcalculate(2, i) = ""
End Select
txtshow.Text = number(0, i)
End Select
End Select
strcalculate(0, i) = strcalculate(1, i)
'Case 5
' i = i + 1
' ReDim Preserve number(2, i) As Double
' ReDim Preserve strcalculate(2, i) As Integer
' strcalculate(1, i) = strcalculate(1, i - 1)
' GoTo calc1234
'Case 6
'End Select
'txtshow.Text = number(0, i)
'strcalculate(0, i) = strcalculate(1, i)
End Sub
Private Sub cmdBackspace_Click()
If Len(txtshow.Text) > 1 Then
txtshow.Text = Left(txtshow.Text, Len(txtshow.Text) - 1)
ElseIf txtshow.Text <> "0" Then
txtshow.Text = "0"
End If
End Sub
Private Sub cmdC_Click()
ReDim number(2, 0) As Double
ReDim strcalculate(2, 0) As String
txtshow.Text = "0"
i = 0
End Sub
Private Sub cmdcalculate_Click(Index As Integer)
CALCULATE (cmdcalculate(Index).Caption)
End Sub
Private Sub cmdCE_Click()
txtshow.Text = "0"
End Sub
Private Sub cmdnegative_Click()
txtshow.Text = -Val(txtshow.Text)
number(0, i) = txtshow.Text
End Sub
Private Sub cmdnum_Click(Index As Integer)
If CStr(number(0, i)) = txtshow.Text Then
txtshow.Text = "0"
End If
If Index = 0 And txtshow.Text = "0" Then
ElseIf txtshow.Text = "0" Then
txtshow.Text = Index
Else
txtshow.Text = txtshow.Text & Index
End If
End Sub
Private Sub cmdparentheses_Click(Index As Integer)
CALCULATE (cmdparentheses(Index).Caption)
Dim intkuohao As Integer
Dim strkuohao As String
For intkuohao = 1 To i
strkuohao = strkuohao & " ("
Next intkuohao
Label1.Caption = strkuohao
Label1.Refresh
End Sub
Private Sub cmdpoint_Click()
If InStr(txtshow.Text, ".") = 0 Then txtshow.Text = txtshow.Text & "."
End Sub
Private Sub cmdResult_Click()
If i = 0 Then
For intkuohao = i To 1 Step -1
CALCULATE (")")
Next intkuohao
End If
CALCULATE (cmdResult.Caption)
ReDim strcalculate(2, 0) As String
ReDim number(2, 0) As Double
i = 0
End Sub
Private Sub Form_Load()
Dim intwidth As Integer, intheight As Integer
intwidth = cmdnum(0).Width + 50
intheight = cmdnum(0).Height + 50
For i = 1 To 9
cmdnum(i).Caption = i
cmdnum(i).Move cmdnum(0).Left + intwidth * ((i - 1) Mod 3), cmdnum(0).Top - intheight * ((i - 1) \ 3) - intheight
Next i
For i = 0 To 3
cmdcalculate(i).Move cmdnum(9).Left + intwidth, cmdnum(9).Top + intheight * i
Next i
cmdnegative.Move cmdnum(0).Left + intwidth, cmdnum(0).Top
cmdpoint.Move cmdnum(0).Left + intwidth * 2, cmdnum(0).Top
cmdCE.Move cmdnum(0).Left + intwidth * 2, cmdnum(0).Top - 4 * intheight
cmdC.Move cmdnum(0).Left + intwidth * 3, cmdnum(0).Top - 4 * intheight
cmdResult.Move cmdnum(0).Left + intwidth * 4, cmdnum(0).Top - 4 * intheight
cmdBackspace.Move cmdnum(0).Left + intwidth * 4, cmdnum(0).Top - 2 * intheight
For i = 0 To 1
cmdparentheses(i).Move cmdnum(0).Left + i * intwidth, cmdnum(0).Top - 4 * intheight
Next i
txtshow.Move cmdnum(0).Left, cmdnum(7).Top - 3 * intheight, 5 * intwidth
Label1.Move cmdnum(0).Left, cmdnum(7).Top - 3 * intheight - Label1.Height, 5 * intwidth
txtshow.Text = "0"
i = 0
ReDim strcalculate(2, i) As String
ReDim number(2, i) As Double
End Sub
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