用VB做一个简单的计算器,该怎么做?

如果要用控制数组该怎么做呢?... 如果要用控制数组该怎么做呢? 展开
 我来答
jinjunhao12345
推荐于2017-10-07 · TA获得超过125个赞
知道答主
回答量:41
采纳率:0%
帮助的人:0
展开全部
我是抢劫的,把分给我吧
首先建立一个command1 然后剪切复制 系统提示说已经有一个COMMAND1 是否要建立控件数组?
你点击是 这样重复9次 分别把caption该成0到9 然后建立command2 方法同上,caption为 +-*/ 然后是 command3 caption是=
然后输入代码,首先在通用里生命dim shu1,shu2
dim suanfu as string
private sub command1(index)_click
text1.text=text1.text & commmand1(index).capton
end sub
command2里 输入
shu1=text1.text
suanfu=text1.text & command(index).capiton
text1.text=""
end sub
command3里输入
shu2=text1.text
selece case suanfu
case "+"
text1.text=shu1+shu2
case "-"
text1.text=shu1-shu2
case "*"
text1.text=shu*shu2
case "/"
if shu2=0 then
msgbox "分母不能为0"
else text1.text=shu1/shu2
end if
end select
end sub
这是最简单的计算器的 如果还需要什么功能 我也可以告诉你的 我学VB3个月了 略有小成 所以想来照顾新手的 我以前也是新手啊 新手见新手两眼泪旺旺 顺便把分给我吧
广州中仪测绘
2023-08-25 广告
作为广州中仪测绘科技有限公司的工作人员,我回答如下:使用工程计算器需要遵循一定的步骤和技巧。首先,打开计算器后,根据需要设置计算器的模式,例如科学模式或程序员模式。然后,根据所进行的工程计算需求,输入相应的数值和运算符号。例如,在计算土方量... 点击进入详情页
本回答由广州中仪测绘提供
du瓶邪
推荐于2017-10-03 · TA获得超过2.4万个赞
知道大有可为答主
回答量:1.7万
采纳率:100%
帮助的人:2805万
展开全部
Option Explicit
Dim Op1, Op2 ' 前面输入的操作数
Dim DecimalFlag As Integer ' 小数点仍然存在吗?
Dim NumOps As Integer ' 操作数个数
Dim LastInput ' 指示上一次按键事件的类型
Dim OpFlag ' 指示未完成的操作
Dim TempReadout

' C (取消) 按钮的 Click 事件过程
' 重新设置显示并初始化变量
Private Sub Cancel_Click()
Readout = Format(0, "0.")
Op1 = 0
Op2 = 0
Form_Load
End Sub

' CE (取消输入) 按钮的 Click 事件过程
Private Sub CancelEntry_Click()
Readout = Format(0, "0.")
DecimalFlag = False
LastInput = "CE"
End Sub

' 小数点 (.) 按钮的 Click 事件过程
' 如果上一次按键为运算符,初始化 readout 为 "0.";
' 否则显示时追加一个小数点
Private Sub Decimal_Click()
If LastInput = "NEG" Then
Readout = Format(0, "-0.")
ElseIf LastInput <> "NUMS" Then
Readout = Format(0, "0.")
End If
DecimalFlag = True
LastInput = "NUMS"
End Sub

' 窗体的初始化过程
' 设置所有变量为其初始值
Private Sub Form_Load()
DecimalFlag = False
NumOps = 0
LastInput = "NONE"
OpFlag = " "
Readout = Format(0, "0.")
'Decimal.Caption = Format(0, ".")
End Sub

' 数字键 (0-9) 的 Click 事件过程
' 向显示中的数追加新数
Private Sub Number_Click(Index As Integer)
If LastInput <> "NUMS" Then
Readout = Format(0, ".")
DecimalFlag = False
End If
If DecimalFlag Then
Readout = Readout + Number(Index).Caption
Else
Readout = Left(Readout, InStr(Readout, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".")
End If
If LastInput = "NEG" Then Readout = "-" & Readout
LastInput = "NUMS"
End Sub

' 运算符 (+, -, x, /, =) 的 Click 事件过程
' 如果接下来的按键是数字键,增加 NumOps。
' 如果有一个操作数,则设置 Op1。
' 如果有两个操作数,则将 Op1 设置为 Op1 与
' 当前输入字符串的运算结果,并显示结果
Private Sub Operator_Click(Index As Integer)
TempReadout = Readout
If LastInput = "NUMS" Then
NumOps = NumOps + 1
End If
Select Case NumOps
Case 0
If Operator(Index).Caption = "-" And LastInput <> "NEG" Then
Readout = "-" & Readout
LastInput = "NEG"
End If
Case 1
Op1 = Readout
If Operator(Index).Caption = "-" And LastInput <> "NUMS" And OpFlag <> "=" Then
Readout = "-"
LastInput = "NEG"
End If
Case 2
Op2 = TempReadout
Select Case OpFlag
Case "+"
Op1 = CDbl(Op1) + CDbl(Op2)
Case "-"
Op1 = CDbl(Op1) - CDbl(Op2)
Case "X"
Op1 = CDbl(Op1) * CDbl(Op2)
Case "/"
If Op2 = 0 Then
MsgBox "除数不能为 0", 48, "计算器"
Else
Op1 = CDbl(Op1) / CDbl(Op2)
End If
Case "="
Op1 = CDbl(Op2)
Case "%"
Op1 = CDbl(Op1) * CDbl(Op2)
End Select
Readout = Op1
NumOps = 1
End Select
If LastInput <> "NEG" Then
LastInput = "OPS"
OpFlag = Operator(Index).Caption
End If
End Sub

' 百分比键 (%) 的 Click 事件过程
' 计算并显示第一个操作数的百分数
Private Sub Percent_Click()
Readout = Readout / 100
LastInput = "Ops"
OpFlag = "%"
NumOps = NumOps + 1
DecimalFlag = True
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
星月O相随
2009-10-12 · TA获得超过120个赞
知道小有建树答主
回答量:506
采纳率:0%
帮助的人:134万
展开全部
学vb连个计算器都不会,你可真有前途。。。。

Option Explicit

Private StoredValue As Double

Private Const opNone = 0
Private Const opAdd = 1
Private Const opSubtract = 2
Private Const opMultiply = 3
Private Const opDivide = 4
Private Operator As Integer

Private NewEntry As Boolean

'删除最后的字符
Private Sub DeleteCharacter()
Dim txt As String
Dim min_len As Integer

txt = txtDisplay.Text
If Left$(txt, 1) = "-" Then
min_len = 2
Else
min_len = 1
End If

If Len(txt) > min_len Then
txtDisplay.Text = Left$(txt, Len(txt) - 1)
Else
txtDisplay.Text = "0"
End If
End Sub

'清除显示内容,保存运算符
Private Sub cmdClear_Click()
cmdClearEntry_Click
StoredValue = 0
Operator = opNone
End Sub

'清除显示内容
Private Sub cmdClearEntry_Click()
txtDisplay.Text = ""
End Sub

' 显示小数点
Private Sub cmdDecimal_Click()
If InStr(txtDisplay.Text, ".") Then
Beep
Else
If NewEntry Then
txtDisplay.Text = "."
NewEntry = False
Else
txtDisplay.Text = txtDisplay.Text & "."
End If
End If
End Sub

'计算上一操作符的运算结果
Private Sub cmdEquals_Click()
Dim new_value As Double

If txtDisplay.Text = "" Then
new_value = 0
Else
new_value = CDbl(txtDisplay.Text)
End If
Select Case Operator
Case opNone
StoredValue = new_value
Case opAdd
StoredValue = StoredValue + new_value
Case opSubtract
StoredValue = StoredValue - new_value
Case opMultiply
StoredValue = StoredValue * new_value
Case opDivide
StoredValue = StoredValue / new_value
End Select
Operator = opNone
NewEntry = True
txtDisplay.Text = Format$(StoredValue)
End Sub

' 显示数字
Private Sub cmdNumber_Click(Index As Integer)
If NewEntry Then
txtDisplay.Text = Format$(Index)
NewEntry = False
Else
txtDisplay.Text = txtDisplay.Text & Format$(Index)
End If
End Sub

Private Sub cmdOperator_Click(Index As Integer)
cmdEquals_Click
Operator = Index
NewEntry = True
End Sub
'改变符号
Private Sub cmdPlusMinus_Click()
If NewEntry Then
txtDisplay.Text = "-"
ElseIf Left$(txtDisplay.Text, 1) = "-" Then
txtDisplay.Text = Right$(txtDisplay.Text, 2)
Else
txtDisplay.Text = "-" & txtDisplay.Text
End If
End Sub

'处理键盘按键
Private Sub Form_KeyPress(KeyAscii As Integer)
txtDisplay_KeyPress KeyAscii
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
txtDisplay_KeyUp KeyCode, Shift
End Sub

Private Sub txtDisplay_Change()
txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub

Private Sub txtDisplay_GotFocus()
txtDisplay_Change
End Sub

Private Sub txtDisplay_KeyPress(KeyAscii As Integer)
Dim ch As String

ch = Chr$(KeyAscii)
Select Case ch
Case "0"
cmdNumber_Click 0
Case "1"
cmdNumber_Click 1
Case "2"
cmdNumber_Click 2
Case "3"
cmdNumber_Click 3
Case "4"
cmdNumber_Click 4
Case "5"
cmdNumber_Click 5
Case "6"
cmdNumber_Click 6
Case "7"
cmdNumber_Click 7
Case "8"
cmdNumber_Click 8
Case "9"
cmdNumber_Click 9
Case "*", "x", "X"
cmdOperator_Click opMultiply
Case "+"
cmdOperator_Click opAdd
Case vbCrLf, vbCr, "="
cmdEquals_Click
Case "-"
cmdOperator_Click opSubtract
Case "."
cmdDecimal_Click
Case "/"
cmdOperator_Click opDivide
Case "C", "c"
cmdClearEntry_Click
End Select

KeyAscii = 0
End Sub

Private Sub txtDisplay_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyNumpad0
cmdNumber_Click 0
Case vbKeyNumpad1
cmdNumber_Click 1
Case vbKeyNumpad2
cmdNumber_Click 2
Case vbKeyNumpad3
cmdNumber_Click 3
Case vbKeyNumpad4
cmdNumber_Click 4
Case vbKeyNumpad5
cmdNumber_Click 5
Case vbKeyNumpad6
cmdNumber_Click 6
Case vbKeyNumpad7
cmdNumber_Click 7
Case vbKeyNumpad8
cmdNumber_Click 8
Case vbKeyNumpad9
cmdNumber_Click 9
Case vbKeyMultiply
cmdOperator_Click opMultiply
Case vbKeyAdd
cmdOperator_Click opAdd
Case vbKeySeparator
cmdEquals_Click
Case vbKeySubtract
cmdOperator_Click opSubtract
Case vbKeyDecimal
cmdDecimal_Click
Case vbKeyDivide
cmdOperator_Click opDivide
Case vbKeyBack, vbKeyDelete
DeleteCharacter
End Select
KeyCode = 0
End Sub
按钮什么的在哪就自己想了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友057a1a6370
2019-07-31
知道答主
回答量:8
采纳率:0%
帮助的人:5638
展开全部
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
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
石春乐正端
2020-03-24 · TA获得超过3806个赞
知道大有可为答主
回答量:3089
采纳率:34%
帮助的人:175万
展开全部
我这里有个你用VB打开里面自然有
代码
的.....................
附件:计算器.rar
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(5)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式