用VB编写计算器的代码如何写??
1个回答
展开全部
在窗体上放置以下控件:
1、lblResult
Label控件
设置lblResult.Caption
=
0
2、btnNum(0)~btnNum(9)
按钮控件数组,下标对应齐数字(如:btnNum(0).Caption=0,依次类推)
3、添加btnAdd(加法)、btnSub(减法)、btnMul(乘法)、btnDiv(除法)、btnEnter(等于号)、btnReset(清除键)六个按钮。
在窗体代码中输入以下内容:
Dim
byteOperation
As
Byte
'操作符
Dim
byteTmp
As
Byte
'临时操作符
Dim
intNum
As
Double
'输入的数字
Sub
Calc()
Select
Case
byteOperation
Case
1
'Add
lblResult.Caption
=
CDbl(lblResult.Caption)
+
intNum
Case
2
'Sub
lblResult.Caption
=
intNum
-
CDbl(lblResult.Caption)
Case
3
'Mul
lblResult.Caption
=
CDbl(lblResult.Caption)
*
intNum
Case
4
'Div
lblResult.Caption
=
intNum
/
CDbl(lblResult.Caption)
Case
Else
End
Select
End
Sub
Private
Sub
btnNum_Click(Index
As
Integer)
If
byteTmp
<>
0
Then
intNum
=
lblResult.Caption
lblResult.Caption
=
0
byteTmp
=
0
End
If
If
lblResult.Caption
=
0
Then
lblResult.Caption
=
Index
Else
If
Len(lblResult.Caption)
<
9
Then
lblResult.Caption
=
lblResult.Caption
&
Index
End
If
End
If
End
Sub
Private
Sub
btnAdd_Click()
If
byteOperation
<>
0
Then
Call
Calc
End
If
byteOperation
=
1
byteTmp
=
1
End
Sub
Private
Sub
btnReset_Click()
byteTmp
=
0
byteOperation
=
0
intNum
=
0
lblResult.Caption
=
0
End
Sub
Private
Sub
btnSub_Click()
If
byteOperation
<>
0
Then
Call
Calc
End
If
byteOperation
=
2
byteTmp
=
2
End
Sub
Private
Sub
btnMul_Click()
If
byteOperation
<>
0
Then
Call
Calc
End
If
byteOperation
=
3
byteTmp
=
3
End
Sub
Private
Sub
btnDiv_Click()
If
byteOperation
<>
0
Then
Call
Calc
End
If
byteOperation
=
4
byteTmp
=
4
End
Sub
Private
Sub
btnEnter_Click()
Call
Calc
byteTmp
=
0
End
Sub
1、lblResult
Label控件
设置lblResult.Caption
=
0
2、btnNum(0)~btnNum(9)
按钮控件数组,下标对应齐数字(如:btnNum(0).Caption=0,依次类推)
3、添加btnAdd(加法)、btnSub(减法)、btnMul(乘法)、btnDiv(除法)、btnEnter(等于号)、btnReset(清除键)六个按钮。
在窗体代码中输入以下内容:
Dim
byteOperation
As
Byte
'操作符
Dim
byteTmp
As
Byte
'临时操作符
Dim
intNum
As
Double
'输入的数字
Sub
Calc()
Select
Case
byteOperation
Case
1
'Add
lblResult.Caption
=
CDbl(lblResult.Caption)
+
intNum
Case
2
'Sub
lblResult.Caption
=
intNum
-
CDbl(lblResult.Caption)
Case
3
'Mul
lblResult.Caption
=
CDbl(lblResult.Caption)
*
intNum
Case
4
'Div
lblResult.Caption
=
intNum
/
CDbl(lblResult.Caption)
Case
Else
End
Select
End
Sub
Private
Sub
btnNum_Click(Index
As
Integer)
If
byteTmp
<>
0
Then
intNum
=
lblResult.Caption
lblResult.Caption
=
0
byteTmp
=
0
End
If
If
lblResult.Caption
=
0
Then
lblResult.Caption
=
Index
Else
If
Len(lblResult.Caption)
<
9
Then
lblResult.Caption
=
lblResult.Caption
&
Index
End
If
End
If
End
Sub
Private
Sub
btnAdd_Click()
If
byteOperation
<>
0
Then
Call
Calc
End
If
byteOperation
=
1
byteTmp
=
1
End
Sub
Private
Sub
btnReset_Click()
byteTmp
=
0
byteOperation
=
0
intNum
=
0
lblResult.Caption
=
0
End
Sub
Private
Sub
btnSub_Click()
If
byteOperation
<>
0
Then
Call
Calc
End
If
byteOperation
=
2
byteTmp
=
2
End
Sub
Private
Sub
btnMul_Click()
If
byteOperation
<>
0
Then
Call
Calc
End
If
byteOperation
=
3
byteTmp
=
3
End
Sub
Private
Sub
btnDiv_Click()
If
byteOperation
<>
0
Then
Call
Calc
End
If
byteOperation
=
4
byteTmp
=
4
End
Sub
Private
Sub
btnEnter_Click()
Call
Calc
byteTmp
=
0
End
Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询