怎么做VB语言的计算器
答案:2 悬赏:60
解决时间 2021-12-28 22:26
- 提问者网友:无悔疯狂
- 2021-12-28 03:40
怎么做VB语言的计算器
最佳答案
- 二级知识专家网友:不羁的心
- 2021-12-28 04:16
Private Sub Command1_Click()
Select Case Text2.Text
Case "+"
Text4.Text = Str(Val(Text1.Text) + Val(Text3.Text))
Case "-"
Text4.Text = Str(Val(Text1.Text) - Val(Text3.Text))
Case "*"
Text4.Text = Str(Val(Text1.Text) * Val(Text3.Text))
Case "/"
If Val(Text3.Text) = 0 Then
Text4.Text = "除数不能为0"
Else
Text4.Text = Str(Val(Text1.Text) / Val(Text3.Text))
End If
End Select
End SubPrivate Sub Command2_Click()
Unload Me
End Sub简易版
Select Case Text2.Text
Case "+"
Text4.Text = Str(Val(Text1.Text) + Val(Text3.Text))
Case "-"
Text4.Text = Str(Val(Text1.Text) - Val(Text3.Text))
Case "*"
Text4.Text = Str(Val(Text1.Text) * Val(Text3.Text))
Case "/"
If Val(Text3.Text) = 0 Then
Text4.Text = "除数不能为0"
Else
Text4.Text = Str(Val(Text1.Text) / Val(Text3.Text))
End If
End Select
End SubPrivate Sub Command2_Click()
Unload Me
End Sub简易版
全部回答
- 1楼网友:duile
- 2021-12-28 04:47
编辑一个有0~9、小数点、加减乘除的计算器 用一个窗体就可以实现,你可以试试 option explicit dim strnumber as string dim strpoint as string dim dblnum1 as double dim intoperator as integer '清除结果 private sub cmdgt_click() txtdisplay.text = "0." strnumber = "" strpoint = "." intoperator = 7 end sub '输入数字 private sub cmdnumber_click(index as integer) strnumber = strnumber & cmdnumber(index).caption txtdisplay.text = strnumber & strpoint end sub private sub cmdonoff_click() end end sub '运算过程 private sub cmdoperator_click(index as integer) dim dblnum2 as double '是第一次单击运算符时,将输入的值先赋给第一个数,否则赋值给第二个数进行运算 if intoperator = 7 then dblnum1 = cdbl(txtdisplay.text) else dblnum2 = cdbl(val(txtdisplay.text)) '根据输入的符号进行运算 '求普通运算 select case intoperator case 0 dblnum1 = dblnum1 + dblnum2 case 1 dblnum1 = dblnum1 - dblnum2 case 2 dblnum1 = dblnum1 * dblnum2 case 3 if dblnum2 <> 0 then dblnum1 = dblnum1 / dblnum2 else msgbox "除数不能为“0”!请重新输入除数。", vbokonly + vbinformation, "除零错误" index = intoperator end if case 6 dblnum1 = dblnum1 * dblnum2 / 100 end select end if '取得当前输入的运算符,以做下次运算 intoperator = index strnumber = "" txtdisplay = cstr(dblnum1) '判断是否为文本框中的数字加点 if not txtdisplay like "*.*" then txtdisplay.text = txtdisplay.text & "." end if end sub private sub cmdotheroper_click(index as integer) dim dblnum as double '求平方根,平方, dblnum = cdbl(val(txtdisplay.text)) select case index case 0 '验证数据是否有效 if dblnum >= 0 then txtdisplay.text = cstr(sqr(dblnum)) else msgbox "负数不能开平方根!", _ vbokonly + vbcritical, "开平方根错误" end if case 1 txtdisplay.text = cstr(dblnum ^ 2) end select '判断是否为文本框中的数字加点 if not txtdisplay like "*.*" then txtdisplay.text = txtdisplay.text & "." end if end sub private sub cmdpoint_click() strnumber = strnumber & strpoint strpoint = "" end sub private sub form_keydown(keycode as integer, shift as integer) '使被按下的数字键的对应按钮取得焦点 select case keycode case 48 to 57 cmdnumber(keycode - 48).setfocus case 96 to 105 cmdnumber(keycode - 96).setfocus case else '使按下的符号键对应的按钮取得焦点 if keycode = 107 or (shift = vbshiftmask and keycode = 187) then cmdoperator(0).setfocus cmdoperator_click (0) elseif keycode = 109 or keycode = 189 then cmdoperator(1).setfocus cmdoperator_click (1) elseif keycode = 106 or (shift = vbshiftmask and keycode = 56) then cmdoperator(2).setfocus cmdoperator_click (2) elseif keycode =
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯