如何用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
温馨提示:内容为网友见解,仅供参考
第1个回答  2014-06-17
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 ...

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

用Visual Basic 语言怎样编写简单计算器(记事本 )
Private Sub cmdcalc_Click()If opernum < 2 Then Exit Sub Select Case opersymbol Case 0 num1 = num1 + num2 Case 1 num1 = num1 - num2 Case 2 num1 = num1 * num2 case3 If num2 <> 0 Then num1 = num1 \/ num2 Else MsgBox "除数不能为0"End If End Select opernu...

用vb设计一个计算器,里面的加减乘除按键的代码怎么写
dim 操作 as string private sub 加减乘除(byval sender as object, byval e as eventargs) handles 加.click, 减.click, 乘.click, 除.click 上一个数 = cdbl(输入框.text) ' 存储当前输入的数值 输入框.text = "" ' 等待下一个数值 select case sender.text ' 根据按下的按钮...

VB设计“加减乘除计算器”谢谢大家,计算器最好是带负数和小数的...
我们知道小数点只能输入一次,一个数据中不能有多个小数点存在,所以需要一个布尔变量做判断。在Form_Load里进行初赋值:PointFlag = False 在Command的单击事件里接着上面输入新的代码:Case 10 If Not PointFlag Then’如果没有小数点 If FirstNum Then’如果是第一个字符 StrNum1 = "0."FirstNum...

急急急,求大神用vb教做一个简易计算器,加减乘除就可以。能用加分
添加1个Command控件数组(4个符号)用作加减乘除按钮,name=cmdArithmetic,index=0~3 好了,然后复制以下代码运行即可成功啦:'Private strCalculateType As String '存放运算类型 Private blnCalFlag As Boolean '标记是否已经输入加数、或减数、或乘数、或除数等 Private dblNum1 As Double '存放被加数、...

用VB做一个简单的加减乘除的计算器,代码是什么?
我们现在首先要做的就是拉出Label,准备三个Label,把label1,label2,label3,这些改成第一个数、第二个数、结果。我们现在要text也要拉出三个,把了text里面text1、text2、text3全部删掉。我们现在要把command拉出四个,把里面command1-4都给成+-*\/ 现在我们现在要双击+,我们开始进行输入代码 ...

如何用VB编写计算器,要求实现加,减,乘,除,求乘方,正弦,余弦,正切,余...
Private Const opAdd = 1 Private Const opSubtract = 2 Private Const opMultiply = 3 Private Const opDivide = 4 Private Operator As Integer Private NewEntry As Boolean ' Remove the last character.Private Sub DeleteCharacter()Dim txt As String Dim min_len As Integer txt = txt...

如何用vb实现在EXCEL中自动计算加减乘除?
打开Excel,并按下Alt+F11键,打开VBA编辑器。在左侧的项目资源管理器中,双击要运行代码的工作簿,以展开其目录。右键单击"Microsoft Excel Objects"文件夹,选择"插入"->"模块",在项目资源管理器中会出现一个新的模块。在右侧的代码窗口中,将上述代码复制粘贴到新模块中。修改代码中的运算符和列号...

VB简易计算器代码怎么写
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 = False Text1.Text = "2"...

相似回答