如何用vb编写简单计算器

如何用vb编写简单计算器... 如何用vb编写简单计算器 展开
 我来答
蓝の薇光闪星
推荐于2018-02-11
知道答主
回答量:20
采纳率:0%
帮助的人:0
展开全部
不知道你想要哪种,偶是新手,只会最简单的
首先拖出三个text,再拖出4个Command按钮 caption分别为+ - * / 这样就准备就绪了
先讲一下简单用法,第一个text是第一个加数/被减数/因数/被除数,而第二个text是第二个加数/减数/因数/除数,第三个text是和/差/积/商,在text1输入一个数,text2输入一个,然后分别点击command“+ - * /”text3就会出现结果
然后随便单击一个窗口,把里面的文字删去,再写上:
Private Sub Command1_Click()
Text3.Text = Str$(Val(Text1.Text) + Val(Text2.Text))
End Sub

Private Sub Command2_Click()
Text3.Text = Str$(Val(Text1.Text) - Val(Text2.Text))
End Sub

Private Sub Command3_Click()
Text3.Text = Str$(Val(Text1.Text) * Val(Text2.Text))
End Sub

Private Sub Command4_Click()
Text3.Text = Str$(Val(Text1.Text) / Val(Text2.Text))
End Sub
然后运行,应该就可以了
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
谢远愉视角
2010-04-25 · 超过12用户采纳过TA的回答
知道答主
回答量:52
采纳率:0%
帮助的人:32.4万
展开全部
Public choice As String
Public current As Double
Public prev As Double
Public counter As Integer

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub ac_Click()
Text1.Text = ""
prev = current = 0
'dz.Visible = False
'asterisk.Enabled = False
'equal.Enabled = False
'expo.Enabled = False
'logn.Enabled = False
'logten.Enabled = False
'minus.Enabled = False
'plus.Enabled = False
'sign.Enabled = False
'slash.Enabled = False
'sroot.Enabled = False
'cosine.Enabled = False
'sine.Enabled = False
'tangent.Enabled = False
'invsine.Visible = False
'invcos.Visible = False
'invtan.Visible = False
'expo.Visible = False
End Sub

Private Sub asterisk_Click()
Text1.Text = ""
choice = "*"
prev = current
current = 0
End Sub

Private Sub Command1_Click(Index As Integer)
Text1.Text = Text1.Text & Command1(Index).Caption
current = Text1.Text
asterisk.Enabled = True
equal.Enabled = True
expo.Enabled = True
' logn.Enabled = True
' logten.Enabled = True
minus.Enabled = True
plus.Enabled = True
sign.Enabled = True
slash.Enabled = True
sroot.Enabled = True
' tangent.Enabled = True
'' cosine.Enabled = True
' sine.Enabled = True
End Sub

Private Sub Command3_Click()
Dim MyAngle, MyHSin
MyAngle = 1.3 ' 定义角度(以"弧度"为单位)
MsgBox (Exp(MyAngle) - Exp(-1 * MyAngle)) / 2 ' 计算双曲正弦函数值(sin())

End Sub

Private Sub cosine_Click()
Text1.Text = Cos(current * 3.14159265358979 / 180)
current = Cos(current * 3.14159265358979 / 180)
End Sub

Private Sub equal_Click()
On Error GoTo MyErr
If choice = "+" Then
current = prev + current
Text1.Text = current
ElseIf choice = "-" Then
current = prev - current
Text1.Text = current
ElseIf choice = "*" Then
current = prev * current
Text1.Text = current
ElseIf choice = "/" And current <> 0 Then
current = prev / current
Text1.Text = current
Else
Text1.Text = "Press AC to continue"
dz.Visible = True
dz.Caption = "Division by ZERO"
End If
If current < 0 Then
sroot.Enabled = False
Else
sroot.Enabled = True
End If

Exit Sub
MyErr:
MsgBox Err.Description, vbInformation, "信息提示"
End Sub

Private Sub expo_Click()
On Error GoTo MyErr

Exit Sub
MyErr:
MsgBox Err.Description, vbInformation, "信息提示"
End Sub

Private Sub expo10_Click()
Dim i As Integer
Dim j As Double
j = current
current = 1
For i = 1 To j Step 1
current = current * 10
Next i
Text1.Text = current

End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim value As Integer

End Sub

Private Sub invcos_Click()
Dim temp As Double
temp = current * 3.14159265358979 / 180
If (temp <= 1 And temp >= -1) Then
Text1.Text = 1 / Cos(temp)
current = 1 / Cos(temp)
End If
invsine.Visible = False
invcos.Visible = False
invtan.Visible = False
expo.Visible = False
expo10.Visible = False
End Sub

Private Sub invsine_Click()
Dim temp As Double
temp = current * 3.14159265358979 / 180
If (temp <= 1 And temp >= -1) Then
Text1.Text = 1 / Sin(temp)
current = 1 / Sin(temp)
End If

End Sub

Private Sub invtan_Click()
Text1.Text = 1 / Tan(current * 3.14159265358979 / 180)
current = 1 / Tan(current * 3.14159265358979 / 180)
invsine.Visible = False
invcos.Visible = False
invtan.Visible = False
expo.Visible = False
expo10.Visible = False
End Sub

Private Sub logn_Click()
Text1.Text = Log(current)
current = Log(current)
End Sub

Private Sub logten_Click()
Text1.Text = Log(current) / Log(10)
current = Log(current) / Log(10)
End Sub

Private Sub mode_MouseDown(Button As Integer, shift As Integer, X As Single, Y As Single)
counter = counter + 1
If counter > 2 Then
counter = 1
End If
If counter = 2 Then
displaylb.Caption = "Normal Calculator"
displaylb.ForeColor = &HFFC0C0
Form1.Width = 4830
sine.Visible = False
cosine.Visible = False
tangent.Visible = False
logn.Visible = False
logten.Visible = False
random.Enabled = False
random.Visible = False
shiftb.Visible = False
recall.Visible = False
Else
displaylb.Caption = " Scientific Calculator"
displaylb.ForeColor = &HC0&
Form1.Width = 5670
sine.Visible = True
cosine.Visible = True
tangent.Visible = True
logn.Visible = True
logten.Visible = True
random.Enabled = True
random.Visible = True
shiftb.Visible = True
recall.Visible = True
End If
End Sub

Private Sub point_Click()
If Text1.Text = "" Then
Text1.Text = "0."
Else
Text1.Text = Text1.Text & "."
End If
End Sub

Private Sub random_Click()
Text1.Text = (2.1021020134 * Rnd + 1)
End Sub

Private Sub recall_Click()
Text1.Text = current
End Sub

Private Sub shiftb_Click()
invsine.Visible = True
invcos.Visible = True
invtan.Visible = True
expo.Visible = True
expo10.Visible = True
If Text1.Text <= 1 And Text1.Text >= -1 Then
invsine.Enabled = True
invcos.Enabled = True
End If
invtan.Enabled = True
expo.Enabled = True
expo10.Enabled = True
End Sub

Private Sub sign_Click()
If current < 0 Then
Text1.Text = -current
current = -current
Else
Text1.Text = "-" & Text1.Text
current = -current
End If
If current < 0 Then
sroot.Enabled = False
Else
sroot.Enabled = True
End If
End Sub

Private Sub minus_Click()
Text1.Text = ""
choice = "-"
prev = current
current = 0
End Sub

Private Sub plus_Click()
Text1.Text = ""
choice = "+"
prev = current
current = 0
End Sub

Private Sub sine_Click()
Text1.Text = Sin(current * 3.14159265358979 / 180)
current = Sin(current * 3.14159265358979 / 180)
End Sub

Private Sub slash_Click()
Text1.Text = ""
choice = "/"
prev = current
current = 0
End Sub

Private Sub sroot_Click()
On Error GoTo MyErr
Text1.Text = Sqr(current)
current = Sqr(current)

Exit Sub
MyErr:
MsgBox Err.Description, vbInformation, "信息提示"
End Sub

Private Sub tangent_Click()
Text1.Text = Tan(current * 3.14159265358979 / 180)
current = Tan(current * 3.14159265358979 / 180)
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
一念春风十里不如你
推荐于2016-10-03 · TA获得超过1536个赞
知道小有建树答主
回答量:2560
采纳率:68%
帮助的人:299万
展开全部
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

Private Sub Command2_Click()
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 drjsb_Click()
If Readout <> "0" And Readout <> "0." Then
Form1.Text1.Text = Form1.Text1.Text & Readout
Form1.Text1.SelStart = Len(Form1.Text1.Text)
Form6.Hide
End If
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
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式