vb编写计算器程序代码(vb做一个计算器编程)
如何用VB编写一个加减乘除计算器?只要代码!
Option Explicit
Dim Op1, Op2 Dim DecimalFlag As Integer Dim NumOps As Integer Dim LastInput
Dim OpFlag Dim TempReadout
Private Sub Cancel_Click()
Readout = Format(0, "0.")
Op1 = 0
Op2 = 0
Form_Load
End Sub
Private Sub CancelEntry_Click()
Readout = Format(0, "0.")
DecimalFlag = False
LastInput = "CE"
End Sub
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
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
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
Private Sub Percent_Click()
Readout = Readout / 100
LastInput = "Ops"
OpFlag = "%"
NumOps = NumOps + 1
DecimalFlag = True
End Sub 窗体代码 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
vb:简易计算器(加减乘除)代码
代码如下:
Dim t, t1 As Integer
Dim x, y As Double
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 = 0
t1 = 1
End Sub
Private Sub c0_Click(Index As Integer)
If l1.Caption = "0" Then
l1.Caption = "0"
Else
l1.Caption = l1.Caption + "0"
End If
End Sub
Private Sub C1_Click(Index As Integer)
If l1.Caption = "0" Then
l1.Caption = "1"
Else
l1.Caption = l1.Caption + "1"
End If
End Sub
Private Sub c10_Click(Index As Integer)
If t = 0 Then
l1.Caption = l1.Caption + "."
t = 1
End If
End Sub
Private Sub C2_Click(Index As Integer)
If l1.Caption = "0" Then
l1.Caption = "2"
Else
l1.Caption = l1.Caption + "2"
End If
End Sub
Private Sub C3_Click(Index As Integer)
If l1.Caption = "0" Then
l1.Caption = "3"
Else
l1.Caption = l1.Caption + "3"
End If
End Sub
Private Sub C4_Click(Index As Integer)
If l1.Caption = "0" Then
l1.Caption = "4"
Else
l1.Caption = l1.Caption + "4"
End If
End Sub
Private Sub C5_Click(Index As Integer)
If l1.Caption = "0" Then
l1.Caption = "5"
Else
l1.Caption = l1.Caption + "5"
End If
End Sub
Private Sub C6_Click(Index As Integer)
If l1.Caption = "0" Then
l1.Caption = "6"
Else
l1.Caption = l1.Caption + "6"
End If
End Sub
Private Sub C7_Click(Index As Integer)
If l1.Caption = "0" Then
l1.Caption = "7"
Else
l1.Caption = l1.Caption + "7"
End If
End Sub
Private Sub C8_Click(Index As Integer)
If l1.Caption = "0" Then
l1.Caption = "8"
Else
l1.Caption = l1.Caption + "8"
End If
End Sub
Private Sub C9_Click(Index As Integer)
If l1.Caption = "0" Then
l1.Caption = "9"
Else
l1.Caption = l1.Caption + "9"
End If
End Sub
Private Sub z1_Click(Index As Integer)
com
t1 = 1
End Sub
Private Sub z2_Click(Index As Integer)
com
t1 = 2
End Sub
Private Sub z3_Click(Index As Integer)
com
t1 = 3
End Sub
Private Sub z4_Click(Index As Integer)
com
t1 = 4
End Sub
Private Sub z5_Click(Index As Integer)
tmp = Mid(l1.Caption, Len(l1.Caption), 1)
If tmp = "." Then
t = 0
End If
If Len(l1.Caption) = 1 Then
l1.Caption = "0"
Else
l1.Caption = Left(l1.Caption, Len(l1.Caption) - 1)
End If
End Sub
Private Sub z6_Click(Index As Integer)
l1.Caption = "0"
Form_Load
End Sub
Private Sub z7_Click(Index As Integer)
If (l1.Caption "0") Then
l1.Caption = "-" + l1.Caption
End If
End Sub
Private Sub z8_Click(Index As Integer)
com
l1.Caption = Str(y)
End Sub
扩展资料
语言缺点
Visual Basic 语言具有不支持继承、无原生支持多线程、异常处理不完善等三项明显缺点,使其有所局限性(此些缺点皆已在 vb .net 获得改进)。
不支持继承
VB 5.0 和 VB 6.0 都是基于对象的编程语言,但是不包含继承特性。VB 中提供了特殊的类的功能,但是还是不能满足程序员的需求。
无原生支持多线程
Visual Basic 对于多线程无原生支持,只能通过Windows API的调用实现,且极其的不稳定。因为在API创建的线程中,并没有自动初始化运行时库,导致部分的函数无法使用。一般的,在VB6等早期的VB开发环境下,使用API创建线程的目的是完成容易使程序假死的大量数据或者逻辑的计算。
异常处理不完善
Visual Basic 中内置异常处理,即使未写异常处理代码,一旦用户出错也会弹出一个明确写出出错原因对话框,接着程序终止。
Visual Basic 中可以使用 Err.Raise抛出异常。对系统及用户抛出的异常的处理常用两种模式:一是使用 On Error Resume Next 处理错误;另一种是使用 On Error Goto 将运行引入错误处理代码。但相对 C++ 等语言而言,这样的异常处理破坏了代码的结构。
参考资料:百度百科-VB
求VB中做计算器全代码?
在窗体上放置以下控件:
1、lblResult
Label控件
设置lblResult.Caption
=
2、btnNum(0)~btnNum(9)
按钮控件数组,下标对应齐数字(如:btnNum(0).Caption=0,依次类推)
3、添加btnAdd(加法)、btnSub(减法)、btnMul(乘法)、btnDiv(除法)、btnEnter(等于号)、btnReset(清除键)六个按钮。
在窗体代码中输入以下内容:
Dim
byteOperation
As
Byte
'操作符
Dim
byteTmp
As
Byte
'临时操作符
Dim
intNum
As
Double
'输入的数字
Sub
Calc()
Select
Case
byteOperation
Case
1
'Add
lblResult.Caption
=
CDbl(lblResult.Caption)
+
intNum
Case
2
'Sub
lblResult.Caption
=
intNum
-
CDbl(lblResult.Caption)
Case
3
'Mul
lblResult.Caption
=
CDbl(lblResult.Caption)
*
intNum
Case
4
'Div
lblResult.Caption
=
intNum
/
CDbl(lblResult.Caption)
Case
Else
End
Select
End
Sub
Private
Sub
btnNum_Click(Index
As
Integer)
If
byteTmp
Then
intNum
=
lblResult.Caption
lblResult.Caption
=
byteTmp
=
End
If
If
lblResult.Caption
=
Then
lblResult.Caption
=
Index
Else
If
Len(lblResult.Caption)
9
Then
lblResult.Caption
=
lblResult.Caption
Index
End
If
End
If
End
Sub
Private
Sub
btnAdd_Click()
If
byteOperation
Then
Call
Calc
End
If
byteOperation
=
1
byteTmp
=
1
End
Sub
Private
Sub
btnReset_Click()
byteTmp
=
byteOperation
=
intNum
=
lblResult.Caption
=
End
Sub
Private
Sub
btnSub_Click()
If
byteOperation
Then
Call
Calc
End
If
byteOperation
=
2
byteTmp
=
2
End
Sub
Private
Sub
btnMul_Click()
If
byteOperation
Then
Call
Calc
End
If
byteOperation
=
3
byteTmp
=
3
End
Sub
Private
Sub
btnDiv_Click()
If
byteOperation
Then
Call
Calc
End
If
byteOperation
=
4
byteTmp
=
4
End
Sub
Private
Sub
btnEnter_Click()
Call
Calc
byteTmp
=
End
Sub
VB编写简单的计算器程序
工具/材料
电脑
VB软件
01
打开VB,新建一个EXE文件。
02
画上四个方框,表示运算法则(即加减乘除四项),3个文本控件表示数,一个标签控件为“=”。并对控件进行命名。
03
然后进行代码编写。双击对应的按钮控件,进入代码编写界面。Text3.Text = Val(Text1.Text) + Val(Text2.Text),就这一句只要更换把‘+’分别换成‘-’‘*’‘/’即可。
04
最后,按“F5”运行即可咯。
特别提示
VB的编写相对其他程序语言会简单一些,感兴趣者可以深入研究。
用VB做一个简单的加减乘除的计算器,代码是什么?
我们现在首先要做的就是拉出Label,准备三个Label,把label1,label2,label3,这些改成第一个数、第二个数、结果。
我们现在要text也要拉出三个,把了text里面text1、text2、text3全部删掉。
我们现在要把command拉出四个,把里面command1-4都给成+-*/
现在我们现在要双击+,我们开始进行输入代码
在里面我们现在要:text3.text=val(text1.text)+val(text2.text)
我们要把这个(text3.text=val(text1.text)+val(text2.text))复制,把里面+号分别改成-*/
如图,就像这样就可以了,可以行径了,如果你在哪个代码里弄错了,你看一下还要哪些没有拉出来,或者你哪个代码输错了。
感谢观看。
Vb中计算器清空需要什么代码?
Vb中计算器清空代码如下所示:
Private?Sub?Cmdcls_Click()? ??
Text1.Text?=?0
End?Sub
VB使用了可以简单建立应用程序的GUI 系统,但是又可以开发相当复杂的程序。VB 的程序是一种基于窗体的可视化组件安排的联合,并且增加代码来指定组件的属性和方法。因为默认的属性和方法已经有一部分定义在了组件内,所以程序员不用写多少代码就可以完成一个简单的程序。
VB 的程序可以包含一个或多个窗体,或者是一个主窗体和多个子窗体,类似于操作系统的样子。有很少功能的对话框窗口(比如没有最大化和最小化按钮的窗体)可以用来提供弹出功能。
扩展资料
VB变量类型:
1、Integer(类型符%)
Integer指的是-32768到+32767之间的整数,通常这个范围已经涵盖了大多数可能会用到的数字。如果认为要用到的数字可能会超出这个范围,可以考虑将其定义为Long。
2、Long(类型符)
这种数据类型垢数字有时又称为Long Integer。这种数据类型可以处理-2147483648到+2147483647之间的所有数字。与使用2个字节的Integer不同,它使用4字节内存存储数据。
3、Single(类型符!)
Single适用于需要小数点的数字。Single可以处理-3.402823E38到-1.401298E-45之间的负值和1.401298E_45到3.402823E38之间的正值。
4、Double(类型符#)
这种数据类型需要使用8字节内存来存储数据。如果必须处理超大范围的值,这种数据类型会很有用。