怎样使用VB编写有加减乘除功能的计算器?

提供代码,能使用的,在加200分... 提供代码,能使用的,在加200分 展开
 我来答
888rain
2008-09-19
知道答主
回答量:11
采纳率:0%
帮助的人:0
展开全部
Dim x!, n1!, n2!
'N1,N2分别代表输入的第一个数和第二个数
Dim strn1$, strn2$
'StrN1,StrN2分别代表输入第一个
Dim firstN As Boolean
'判断是否是数字开头
Dim pointflag As Boolean
'判断是否已有小数点
Dim runsign As Integer
'变量Runsign用来储存运算符号
Dim signflag As Boolean
'变量SignFlag判断是否已有运算符号

Private Sub form_load()
n1 = 0: n2 = 0
strn1 = "": strn2 = ""
firstN = True
pointflag = False
'将 PointFlag 设为否,即没有小数点
runsign = 0
signflag = False
'没有运算符号输入
End Sub
Private Sub cmdN_Click(Index As Integer)
Select Case Index
'用Index 作为判断分支的条件
Case 0 To 9
'如果Index是0到9,即数字
If firstNum Then
'输入为第一个数字的话
strn1 = Str(Index)
'实型转字符型
firstN = False
'初赋值标志为假
Else
strn1 = strn1 + Str(Index)
'输入不是第一个数字的话,将输入的字符添加到原有字符串上
End If '结束 if 块
Text1.Text = strn1 '将字符串结果显示到标签上
Case 10 '如果输入为小数点
If Not pointflag Then '如果没有小数点
If firstN Then '如果是第一个字符
strn1 = "0." '显示为" 0." 不变
firstN = False
Else
strn1 = strn1 + "."
'初赋值标志为假,否则字符串上加上小数点符号
End If '结束if块
Else
Exit Sub
'已有小数点,则退出单击事件,也就是对单击事件不反应
End If
pointflag = True
Text1.Text = strn1
'将小数点标志为真,即在输入串中有了小数点,在显示屏上显示数字串
Case 17
If Sgn(Val(strn1)) = 1 Then
strn1 = "-" + strn1
Else
If Sgn(Val(strn1)) = -1 Then
strn1 = Right(strn1, Len(strn1) - 1)
End If
strn1 = "0."
End If
Text1.Text = strn1
Case 12 To 15
firstN = True
'当输入为运算符号时,使下一次可以输入新数据
pointflag = False '还原小数点标记值
If signflag Then '前面已有运算符未运算
Call Run '调用计算子程序,该子程序在下面有说明
Else
signflag = True
'前面没有未运算的,将有无运算符标志设为"有"
strn2 = strn1 '把字符串1复制给字符串2
strn1 = "" '字符串1清空
End If
runsign = Index - 11
Case 11 '如果输入等号
If Not signflag Then '如果没有输入运算符
Text1.Text = strn1 '以下均为重置各个变量
ysjg = Val(strn1)
firstN = True
pointflag = False
Else
Call Run
signflag = False
'如果已输入运算符在内,计算清除是否有运算符标志
End If
Case 16
n1 = 0
n2 = 0
'如果输入清除按钮,以下均为重置各个变量,相当于Form_Load中的代码
strn1 = ""
strn2 = ""
firstN = True
pointflag = False
runsign = 0
signflag = False
Text1.Text = "0."
End Select
End Sub
Public Sub Run()
Dim ysjg As Single 'Ysjg为局部变量,用来存储运算结果
n1 = Val(strn2) '将字符串转换成数值
n2 = Val(strn1)
Select Case runsign
Case 1
ysjg = n1 + n2
Case 2
ysjg = n1 - n2
Case 3
ysjg = n1 * n2
Case 4
ysjg = n1 / n2
End Select
strn2 = Str(ysjg)
strn1 = strn2
Text1.Text = strn2
End Sub
dl_会飞的青蛙
2008-09-19 · TA获得超过2456个赞
知道大有可为答主
回答量:3294
采纳率:0%
帮助的人:3385万
展开全部
Dim flag, s, fh

Private Sub cd_Click(Index As Integer)
Select Case Index
Case 0 To 9
Text1.Text = Text1.Text & Index
Case 10
If flag = 0 Then
Text1.Text = Text1.Text & "."
flag = 1
End If
Case 11
If fh = 12 Then
Text1.Text = s + Val(Text1.Text)
End If
If fh = 13 Then
Text1.Text = s - Val(Text1.Text)
End If
If fh = 14 Then
Text1.Text = s * Val(Text1.Text)
End If
If fh = 15 Then
Text1.Text = s / Val(Text1.Text)
End If
Case 12 To 15

If Text1.Text = "" And Index = 13 Then
Text1.Text = "-"
Else
s = Text1.Text
Text1.Text = ""
flag = 0
fh = Index
End If
Case 16
Text1.Text = ""
flag = 0
Case 17
End
Case 18
Text1.Text = Val(Text1.Text) / 2
End Select

End Sub

Private Sub Command1_Click()
Text1.SelStart = Len(Text1.Text) - 1
Text1.SelLength = 1
Text1.SelText = ""
flag = 0
End Sub

Private Sub Command2_Click()
Text1.Text = ""
flag = 0
End Sub

Private Sub Form_Load()
flag = 0
For i = 0 To 9
cd(i).Caption = i
Next
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
joka2002
2008-09-18 · TA获得超过245个赞
知道小有建树答主
回答量:421
采纳率:0%
帮助的人:489万
展开全部
vb6.0 支持点击 不支持键盘输入,
从command1~15 分别是12~0 + - * / =
Dim StrNo1 As Double
Dim StrNo2 As Double
Dim Str As String
Dim StrON As Boolean

Private Sub Command1_Click()
If StrON = False Then
Text1.Text = ""
End If

If Text1.Text = "" Then
Text1.Text = 1
Else
Text1.Text = CDbl(Text1.Text) * 10 + 1
End If
End Sub

Private Sub Command10_Click()
If StrON = False Then
Text1.Text = ""
End If

If Text1.Text = "" Then
Text1.Text = ""
Else
Text1.Text = CDbl(Text1.Text) * 10
End If
End Sub

Private Sub Command11_Click()
If Text1.Text = "" Then
StrNo1 = 0
Str = "+"
Else
StrNo1 = CDbl(Text1.Text)
Str = "+"
Text1.Text = ""
End If

End Sub

Private Sub Command12_Click()
If Text1.Text = "" Then
StrNo1 = 0
Str = "-"
Else
StrNo1 = CDbl(Text1.Text)
Str = "-"
Text1.Text = ""
End If
End Sub

Private Sub Command13_Click()
If Text1.Text = "" Then
StrNo1 = 0
Str = "*"
Else
StrNo1 = CDbl(Text1.Text)
Str = "*"
Text1.Text = ""
End If
End Sub

Private Sub Command14_Click()
If Text1.Text = "" Then
StrNo1 = 0
Str = "/"
Else
StrNo1 = CDbl(Text1.Text)
Str = "/"
Text1.Text = ""
End If
End Sub

Private Sub Command15_Click()
If Text1.Text = "" Then
StrNo2 = 0
Else
StrNo2 = CDbl(Text1.Text)
Text1.Text = ""
End If
If Str = "+" Then
Text1.Text = StrNo1 + StrNo2
ElseIf Str = "-" Then
Text1.Text = StrNo1 - StrNo2
ElseIf Str = "*" Then
Text1.Text = StrNo1 * StrNo2
ElseIf Str = "/" Then
Text1.Text = StrNo1 / StrNo2
End If
StrON = False
End Sub

Private Sub Command2_Click()
If StrON = False Then
Text1.Text = ""
End If

If Text1.Text = "" Then
Text1.Text = 2
Else
Text1.Text = CDbl(Text1.Text) * 10 + 2
End If
End Sub

Private Sub Command3_Click()
If StrON = False Then
Text1.Text = ""
End If

If Text1.Text = "" Then
If StrON = False Then
Text1.Text = ""
End If

Text1.Text = 3
Else
Text1.Text = CDbl(Text1.Text) * 10 + 3
End If
End Sub

Private Sub Command4_Click()
If Text1.Text = "" Then
Text1.Text = 4
Else
Text1.Text = CDbl(Text1.Text) * 10 + 4
End If
End Sub

Private Sub Command5_Click()
If StrON = False Then
Text1.Text = ""
End If

If Text1.Text = "" Then
Text1.Text = 5
Else
Text1.Text = CDbl(Text1.Text) * 10 + 5
End If
End Sub

Private Sub Command6_Click()
If StrON = False Then
Text1.Text = ""
End If

If Text1.Text = "" Then
Text1.Text = 6
Else
Text1.Text = CDbl(Text1.Text) * 10 + 6
End If
End Sub

Private Sub Command7_Click()
If StrON = False Then
Text1.Text = ""
End If

If Text1.Text = "" Then
Text1.Text = 7
Else
Text1.Text = CDbl(Text1.Text) * 10 + 7
End If
End Sub

Private Sub Command8_Click()
If StrON = False Then
Text1.Text = ""
End If

If Text1.Text = "" Then
Text1.Text = 8
Else
Text1.Text = CDbl(Text1.Text) * 10 + 8
End If
End Sub

Private Sub Command9_Click()
If StrON = False Then
Text1.Text = ""
End If

If Text1.Text = "" Then
Text1.Text = 9
Else
Text1.Text = CDbl(Text1.Text) * 10 + 9
End If
End Sub

Private Sub Form_Load()
Text1.Text = ""
StrON = True
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
图杭科技
2008-09-19 · TA获得超过531个赞
知道小有建树答主
回答量:1014
采纳率:50%
帮助的人:590万
展开全部
我有v6.0的科学计算器代码.
与他们的都不一样.
button 有几十个,textbox一个.
可用粟标,也可用键盘.
如button1,
Private Sub commend1_Click()
text1.tex=text1.tex+val(commend1.lable)'我的发明专利.
end sub
......
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友7c6a320
2008-09-18 · TA获得超过812个赞
知道小有建树答主
回答量:2462
采纳率:100%
帮助的人:1013万
展开全部
老兄你使用的VB是完全版本的吗? 我使用的是(Microsoft Visual Studio 2005)。。 代码如下

添加3个textbox,4个button
textbox1,textbox2 用来输入,textbox3用来显示结果
button1为加, button2为减, button3 为乘, button4 为除

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ss, ee As Double
ss = Val(TextBox1.Text)
ee = Val(TextBox2.Text)
TextBox3.Text = Trim(Str(ss + ee))

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ss, ee As Double
ss = Val(TextBox1.Text)
ee = Val(TextBox2.Text)
TextBox3.Text = Trim(Str(ss - ee))

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim ss, ee As Double
ss = Val(TextBox1.Text)
ee = Val(TextBox2.Text)
TextBox3.Text = Trim(Str(ss * ee))

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim ss, ee As Double
ss = Val(TextBox1.Text)
ee = Val(TextBox2.Text)
TextBox3.Text = Trim(Str(ss / ee))

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox3.ReadOnly = True
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
huanghe6403
2008-09-18
知道答主
回答量:22
采纳率:0%
帮助的人:16.4万
展开全部
这个问题应该自己解决、其实它并不难。
代码都是那几行,关键得弄清VB的事件触发过程。
添加几个按钮,几个文本框,稍微改改属性值,在相应的事件下添加几行代码就好了,其实并不需要人家的代码,因为如果这个不懂的话,给你代码可能你还是不懂哦 @_@
共同进步!!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(5)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式