用VB编写一个计算器程序的代码

 我来答
飛丶飛灬
推荐于2019-10-20 · TA获得超过1.6万个赞
知道答主
回答量:87
采纳率:100%
帮助的人:3万
展开全部

1、创建控件组的方法
首先创建一个命令按钮,调整其大小(觉得合适就行),名称为Command1,Caption 属性为数字 0 ;然后进行“复制”和“粘贴”,当选择“粘贴”时,出现对话框提示已有一个同名控件,询问是否创建控件组,选择“是”后,即创建了一个名为“Command”的控件组。

这时,第一个按钮的Index属性值默认为“0”,第二个的Index属性值自动设为“1”,并且大小与第一个按钮相同,只需修改其 Caption 属性为数字“1”并将其拖至合适位置即可。此后继续使用“粘贴”的方法建立其他控件组中其余按钮,共20个按钮,每建立一个,就将它拖到合适处,并修改相应的Caption属性值。

2、各控件组其属性设置如下:


设置效果如下图所示:

二、编写代码

Dim s1 As Single, s2 As Single, ysf As String

‘定义两个单精度数变量用与存放参与运算的数,一个字符型存放运算符

Private Sub Command1_Click(Index As Integer)

Text1.Text = Text1.Text & Command1(Index).Caption ’将command1的单击事件与文本框显示的内容连接

End Sub

Private Sub Command2_Click()

Text1.Text = Text1.Text + “。”

If (InStr(Text1.Text, “。”) = 1) Then ‘第一位不能为小数

Text1.Text = “”

End If

If InStr(Text1.Text, “。”) 《 Len(Text1.Text) Then ’防止出现两个小数点

Text1.Text = Left

(Text1.Text, Len(Text1.Text) - 1)

End If

End Sub

Private Sub

Command3_Click()

s2 = Val(Text1.Text) ‘开始加减乘除运算

Select Case ysf Case “+”

Text1.Text = s1 + s2

Case “-”

Text1.Text = s1 - s2

Case “*”

Text1.Text = s1 * s2

Case “/”

If s2 = 0 Then

MsgBox “分母不能为零!”

Text1.Text = “”

Else

Text1.Text = s1 / s2 End If End Select

Text1 = IIf(Left(Text1.Text, 1) = “。”, 0 & Text1.Text, Text1.Text) ‘

这个很关键,如果没有这个的话,得出小于1的小数前面没有0

End Sub

Private Sub Command4_Click()

If Text1.Text = “” Then ’文本为空就结束

Exit Sub

End If

Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) ‘文本退一格

End Sub

Private Sub Command5_Click()

Text1.Text = “” ’清除当前框内文本

End Sub

Private Sub Command6_Click(Index As Integer)

s1 = Val(Text1.Text) ‘将s1隐藏起来 ysf = Command6(Index).Caption

Text1.Text = “”

End Sub

Private Sub Command7_Click()

If Left(Text1.Text, 1) 《》 “-” Then ’判断作为负数

Text1.Text = “-” & Text1.Text

Else

Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1)

End If

End Sub

Private Sub Command8_Click()

Text1.Text = Text1.Text * Text1.Text ‘平方

End Sub

拓展资料

Visual Basic(VB)是由微软公司开发的包含环境的事件驱动编程语言。它源自于BASIC编程语言。VB拥有图形用户界面(GUI)和快速应用程序开发(RAD)系统,可以轻易的使用DAO、RDO、ADO连接数据库,或者轻松的创建ActiveX控件。程序员可以轻松地使用VB提供的组件快速创建一个应用程序。

参考链Visual Basic——百度百科

让猫飞一会儿
2018-07-03 · TA获得超过1119个赞
知道小有建树答主
回答量:540
采纳率:80%
帮助的人:273万
展开全部

控件:text,com1,com2三种

属性设置:无

命令如下:

通用命令

Option Explicit

Dim v As Boolean

Dim s As Integer

Dim x As Double

Dim y As Double

按钮一的命令

Private Sub Command1_Click(Index As Integer)

If Form1.Tag = "s" Then

If Index = 10 Then

Text1.Text = "0"

Else

Text1.Text = Command1(Index).Caption

End If

Form1.Tag = ""

Else

Text1.Text = Text1.Text & Command1(Index).Caption

End If

End Sub

按钮二的命令

Private Sub Command2_Click(Index As Integer)

Form1.Tag = "s"

If v Then

x = Val(Text1.Text)

v = Not v

Else

y = Val(Text1.Text)

Select Case s

Case 0

Text1.Text = x + y

Case 1

Text1.Text = x - y

Case 2

Text1.Text = x * y

Case 3

If y <> 0 Then

Text1.Text = x / y

Else

MsgBox ("不能以0为除数")

Text1.Text = x

v = False

End If

Case 4

y = 0

v = False

End Select

x = Val(Text1.Text)

End If

s = Index

End Sub

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
推荐于2018-07-04
展开全部
编辑一个有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 = 111 Or KeyCode = 191 Then
cmdOperator(3).SetFocus
cmdOperator_Click (3)
ElseIf KeyCode = 13 Then
cmdOperator(7).SetFocus
cmdOperator_Click (7)
ElseIf KeyCode = 8 Then
cmdGT.SetFocus
Call cmdGT_Click
End If
End Select
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
'将合法的数据输入到文本框
Select Case KeyAscii
Case 48 To 58
'调用数字键点击处理程序
cmdNumber_Click KeyAscii - 48
KeyAscii = 0
Case 46
'调用小数点输入
cmdPoint_Click
KeyAscii = 0
Case 13
'当敲击回车时,不能触发Form的 KeyUp 事件,因此在这里设置文本框的焦点
txtDisplay.SetFocus
Case Else
KeyAscii = 0
End Select
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
txtDisplay.SetFocus
End Sub

Private Sub Form_Load()
strNumber = ""
strPoint = "."
intOperator = 7
End Sub
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
阿火精分了
2019-12-25
知道答主
回答量:10
采纳率:0%
帮助的人:1.7万
展开全部

先放图


代码如下

Private Sub Command1_Click()

Text3.Text = Val(Text1.Text) + Val(Text2.Text)

End Sub


Private Sub Command2_Click()

Text3.Text = Val(Text1.Text) - Val(Text2.Text)

End Sub


Private Sub Command3_Click()

Text3.Text = Val(Text1.Text) * Val(Text2.Text)

End Sub


Private Sub Command4_Click()

Text3.Text = Val(Text1.Text) / Val(Text2.Text)

End Sub


Private Sub Command5_Click()

Text1 = ""

Text2 = ""

Text3 = ""

End Sub


Private Sub Command6_Click()

End

End Sub

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
点点要吃牛肉bi
2013-12-10 · TA获得超过1603个赞
知道小有建树答主
回答量:282
采纳率:0%
帮助的人:0
展开全部
完全版的前后台代码...

'请把下面的保存为 form1.frm

VERSION 5.00
Begin VB.Form Calculator
BorderStyle = 1 'Fixed Single
Caption = "计算器"
ClientHeight = 2970
ClientLeft = 2580
ClientTop = 1485
ClientWidth = 3270
ClipControls = 0 'False
BeginProperty Font
Name = "System"
Size = 9.75
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Icon = "CALC.frx":0000
LinkMode = 1 'Source
LinkTopic = "Form1"
MaxButton = 0 'False
PaletteMode = 1 'UseZOrder
ScaleHeight = 2970
ScaleWidth = 3270
WhatsThisHelp = -1 'True
Begin VB.CommandButton Number
Caption = "7"
Height = 480
Index = 7
Left = 120
TabIndex = 7
Top = 600
Width = 480
End
Begin VB.CommandButton Number
Caption = "8"
Height = 480
Index = 8
Left = 720
TabIndex = 8
Top = 600
Width = 480
End
Begin VB.CommandButton Number
Caption = "9"
Height = 480
Index = 9
Left = 1320
TabIndex = 9
Top = 600
Width = 480
End
Begin VB.CommandButton Cancel
Caption = "C"
Height = 480
Left = 2040
TabIndex = 10
Top = 600
Width = 480
End
Begin VB.CommandButton CancelEntry
Caption = "CE"
Height = 480
Left = 2640
TabIndex = 11
Top = 600
Width = 480
End
Begin VB.CommandButton Number
Caption = "4"
Height = 480
Index = 4
Left = 120
TabIndex = 4
Top = 1200
Width = 480
End
Begin VB.CommandButton Number
Caption = "5"
Height = 480
Index = 5
Left = 720
TabIndex = 5
Top = 1200
Width = 480
End
Begin VB.CommandButton Number
Caption = "6"
Height = 480
Index = 6
Left = 1320
TabIndex = 6
Top = 1200
Width = 480
End
Begin VB.CommandButton Operator
Caption = "+"
Height = 480
Index = 1
Left = 2040
TabIndex = 12
Top = 1200
Width = 480
End
Begin VB.CommandButton Operator
Caption = "-"
Height = 480
Index = 3
Left = 2640
TabIndex = 13
Top = 1200
Width = 480
End
Begin VB.CommandButton Number
Caption = "1"
Height = 480
Index = 1
Left = 120
TabIndex = 1
Top = 1800
Width = 480
End
Begin VB.CommandButton Number
Caption = "2"
Height = 480
Index = 2
Left = 720
TabIndex = 2
Top = 1800
Width = 480
End
Begin VB.CommandButton Number
Caption = "3"
Height = 480
Index = 3
Left = 1320
TabIndex = 3
Top = 1800
Width = 480
End
Begin VB.CommandButton Operator
Caption = "X"
Height = 480
Index = 2
Left = 2040
TabIndex = 14
Top = 1800
Width = 480
End
Begin VB.CommandButton Operator
Caption = "/"
Height = 480
Index = 0
Left = 2640
TabIndex = 15
Top = 1800
Width = 480
End
Begin VB.CommandButton Number
Caption = "0"
Height = 480
Index = 0
Left = 120
TabIndex = 0
Top = 2400
Width = 1080
End
Begin VB.CommandButton Decimal
Caption = "."
Height = 480
Left = 1320
TabIndex = 18
Top = 2400
Width = 480
End
Begin VB.CommandButton Operator
Caption = "="
Height = 480
Index = 4
Left = 2040
TabIndex = 16
Top = 2400
Width = 480
End
Begin VB.CommandButton Percent
Caption = "%"
Height = 480
Left = 2640
TabIndex = 17
Top = 2400
Width = 480
End
Begin VB.Label Readout
Alignment = 1 'Right Justify
BackColor = &H0000FFFF&
BorderStyle = 1 'Fixed Single
Caption = "0."
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00000000&
Height = 375
Left = 120
TabIndex = 19
Top = 105
Width = 3000
End
End
Attribute VB_Name = "Calculator"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' --------------------------------------------------------------------------
' 版权所有 (C) 1994 Microsoft Corporation
'
' 您可以免费以任何方式使用、修改、复制并分发您认为有用的
' 示例应用程序文件 (或任何修改过的版本)。Microsoft 对任何
' 示例应用程序文件不做任何保证,不负任何责任和义务。
' --------------------------------------------------------------------------
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
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(6)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式