vb 小程序 计算器 【急!】
设计一个简单的计算机,有0-9数字键,+-*/=,十五个建,一个text显示,实现最基本的功能,最好不要网上抄,能自己写,比较急,请各位高手帮帮忙啊...
设计一个简单的计算机,有0-9数字键,+ - * / = ,十五个建,一个text显示,实现最基本的功能,最好不要网上抄,能自己写,比较急,请各位高手帮帮忙啊
展开
2个回答
展开全部
你很幸运,找到了以前的一个小程序
----------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click, Button10.Click, Button11.Click, Button12.Click, Button13.Click, Button14.Click, Button15.Click, Button16.Click, Button17.Click, Button18.Click
Static num, sum, flag, sflag As Integer
'num当前输入的数
'sum当前的结果
'flag取值中 0代表空,1代表+,2代表-,3代表*,4代表/,5代表=
'sflag 0代表继续输入,1代表结束输入
Select Case DirectCast(sender, Button).Name
Case Button1.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(1)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(1)
num = 1
End If
sflag = 0
Case Button2.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(2)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(2)
num = 2
End If
sflag = 0
Case Button3.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(3)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(3)
num = 3
End If
sflag = 0
Case Button4.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(4)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(4)
num = 4
End If
sflag = 0
Case Button5.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(5)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(5)
num = 5
End If
sflag = 0
Case Button6.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(6)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(6)
num = 6
End If
sflag = 0
Case Button7.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(7)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(7)
num = 7
End If
sflag = 0
Case Button8.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(8)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(8)
num = 8
End If
sflag = 0
Case Button9.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(9)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(9)
num = 9
End If
sflag = 0
Case Button10.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(0)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(0)
num = 0
End If
sflag = 0
'上面是数字的取得
Case Button11.Name '+
TextBox1.Text = CStr(sum)
flag = 1
sflag = 1
Case Button12.Name '-
TextBox1.Text = CStr(sum)
flag = 2
sflag = 1
Case Button13.Name '*
TextBox1.Text = CStr(sum)
flag = 3
sflag = 1
Case Button14.Name '/
TextBox1.Text = CStr(sum)
flag = 4
sflag = 1
Case Button15.Name '进行+ - / * 各种运算
If flag = 1 Then
sum = sum + num
TextBox1.Text = CStr(sum)
ElseIf flag = 2 Then
sum = sum - num
TextBox1.Text = CStr(sum)
ElseIf flag = 3 Then
sum = sum * num
TextBox1.Text = CStr(sum)
ElseIf flag = 4 And num <> 0 Then
sum = sum / num
TextBox1.Text = CStr(sum)
ElseIf flag = 5 Then
sum = sum
TextBox1.Text = CStr(sum)
End If
flag = 0
sflag = 1
Case Button16.Name 'CE 清除
num = 0
sum = 0
flag = 0
sflag = 0
TextBox1.Text = ""
Case Button17.Name '删除
num = 0
Case Button18.Name '退出
End
End Select
End Sub
不过有点小bug ,就是第一次运算会不成功.
----------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click, Button10.Click, Button11.Click, Button12.Click, Button13.Click, Button14.Click, Button15.Click, Button16.Click, Button17.Click, Button18.Click
Static num, sum, flag, sflag As Integer
'num当前输入的数
'sum当前的结果
'flag取值中 0代表空,1代表+,2代表-,3代表*,4代表/,5代表=
'sflag 0代表继续输入,1代表结束输入
Select Case DirectCast(sender, Button).Name
Case Button1.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(1)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(1)
num = 1
End If
sflag = 0
Case Button2.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(2)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(2)
num = 2
End If
sflag = 0
Case Button3.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(3)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(3)
num = 3
End If
sflag = 0
Case Button4.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(4)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(4)
num = 4
End If
sflag = 0
Case Button5.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(5)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(5)
num = 5
End If
sflag = 0
Case Button6.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(6)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(6)
num = 6
End If
sflag = 0
Case Button7.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(7)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(7)
num = 7
End If
sflag = 0
Case Button8.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(8)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(8)
num = 8
End If
sflag = 0
Case Button9.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(9)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(9)
num = 9
End If
sflag = 0
Case Button10.Name
If sflag = 0 Then
TextBox1.Text = TextBox1.Text & CStr(0)
num = CInt(TextBox1.Text)
Else
TextBox1.Text = CStr(0)
num = 0
End If
sflag = 0
'上面是数字的取得
Case Button11.Name '+
TextBox1.Text = CStr(sum)
flag = 1
sflag = 1
Case Button12.Name '-
TextBox1.Text = CStr(sum)
flag = 2
sflag = 1
Case Button13.Name '*
TextBox1.Text = CStr(sum)
flag = 3
sflag = 1
Case Button14.Name '/
TextBox1.Text = CStr(sum)
flag = 4
sflag = 1
Case Button15.Name '进行+ - / * 各种运算
If flag = 1 Then
sum = sum + num
TextBox1.Text = CStr(sum)
ElseIf flag = 2 Then
sum = sum - num
TextBox1.Text = CStr(sum)
ElseIf flag = 3 Then
sum = sum * num
TextBox1.Text = CStr(sum)
ElseIf flag = 4 And num <> 0 Then
sum = sum / num
TextBox1.Text = CStr(sum)
ElseIf flag = 5 Then
sum = sum
TextBox1.Text = CStr(sum)
End If
flag = 0
sflag = 1
Case Button16.Name 'CE 清除
num = 0
sum = 0
flag = 0
sflag = 0
TextBox1.Text = ""
Case Button17.Name '删除
num = 0
Case Button18.Name '退出
End
End Select
End Sub
不过有点小bug ,就是第一次运算会不成功.
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询