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

如题所述

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——百度百科

温馨提示:内容为网友见解,仅供参考
第1个回答  2018-07-03

控件: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

第2个回答  推荐于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本回答被网友采纳
第3个回答  2019-12-25

先放图


代码如下

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

第4个回答  2013-12-10
完全版的前后台代码...

'请把下面的保存为 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

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

用VB编写计算器的代码如何写??
Sub btnDiv_Click()语句,若byteOperation非0,则调用Calc()函数,设置byteOperation为4(除法)和byteTmp为4。Sub btnEnter_Click()语句,调用Calc()函数,结束输入操作。通过以上代码实现了一个基本的计算器功能,包括加、减、乘、除和清除功能。通过按钮点击触发相应操作,最终结果显示在lblResult控件...

vb:简易计算器(加减乘除)代码
Public Sub com()x = Val(l1.Caption)Select Case t1 Case Is = 1: y = y + x Case Is = 2: y = y - x Case Is = 3: y = y * x Case Is = 4: y = y \/ x End Select t1 = 0 t = 0 l1.Caption = "0"End Sub Private Sub Form_Load()x = 0 y = 0 t =...

vb一个计算器代码,谁能帮我看出错误,我懵了,等号那里好像不行
Private Sub Cmdpoint_Click()...

VB简易计算器代码怎么写
Dim n& Dim Plus As Boolean Dim yunsuan(100) As Integer '---这里增加 Dim a100 As Double Private Sub cmd1_Click()If Plus = True Then Plus = False Text1.Text = "1"Else Text1.Text = Text1.Text & "1"End If End Sub Private Sub cmd2_Click()If Plus = True Then Plus...

如何在VB中实现基本的计算器功能?
要使用VB编写一个计算器程序,首先需要打开Visual Basic环境。步骤如下:1. 打开Visual Basic:在开始编写的前提下,打开你的Visual Basic软件。2. 创建UserForm:在软件中,你需要创建一个UserForm,这是计算器界面的基础。通过点击“工具”或“视图”菜单,找到“UserForm”,并创建一个新的空白用户表单...

怎么样用VB编程制作一 个计算器
在开始编写代码之前,确保你已经安装了Visual Basic Express或其他VB编辑器。接着,打开编辑器,新建一个VB项目。在项目中,添加一个窗体,这是我们将进行计算器操作的界面。在窗体上添加按钮,用于代表不同的数字和运算符号。对于数字按钮,可以设置其Caption属性为对应的数字。对于运算符号按钮,可以设置为...

用vb设计一个计算器,里面的加减乘除按键的代码怎么写
输入框.text = "" ' 等待下一个数值 select case sender.text ' 根据按下的按钮的文本属性确定你要做什么,这里可以用加减乘除号代替 case "加"操作 = "加"case "减"操作 = "减"case "乘"操作 = "乘"case "除"操作 = "除"end select end sub private sub 计算(byval sender a...

Vb中计算器清空需要什么代码?
Vb中要实现计算器清空功能,只需要对显示结果的文本框赋值为零。代码如下:在命令按钮的点击事件中,可以使用如下代码:Private Sub CmdCls_Click()Text1.Text = 0 End Sub 在这里,Text1是用于显示计算结果的文本框的名称。将Text属性设置为0即实现了清空功能。这段代码简单明了,有效地满足了计算器...

可以用vbs制作四则运算计算器吗
VBS计算器,代码如下:dim x,y,dDo Until d = vbnox=inputbox("输入算式","算式计算器",y)y=eval(x)d=msgbox (x & "=" & y & chr(10) & "继续吗?" ,vbYesNo, "算式计算器")loop满意请采纳,谢谢

相似回答