求VB 加减乘除 计算机代码

作者&投稿:纳虏 (若有异议请与网页底部的电邮联系)
求用VB写计算机的代码~

编辑一个有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

Dim dflag As Integer
Dim I As Integer
Dim opnre As Integer
Dim prev As Double
Dim oflag As Integer
Dim ind As Integer
Dim soundbz As Boolean
Dim ProgramPath As String
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal Cx As Long, ByVal Cy As Long, ByVal wFlags As Long) As Long
Public Sub SetOnTop(ByVal IsOnTop As Integer)
Dim rtn As Long
If IsOnTop = 1 Then
'将窗口置于最上
rtn = SetWindowPos(Me.hwnd, -1, 0, 0, 0, 0, 3)
Else
rtn = SetWindowPos(Me.hwnd, -2, 0, 0, 0, 0, 3)
End If
End Sub
Private Sub Check1_Click()
If Check1.Value = 1 Then
SetOnTop 1
Else
SetOnTop 0
End If
End Sub
Private Sub Command1_Click(Index As Integer)
text1 = Trim(text1)
If Len(text1) > 20 Then
Beep
Exit Sub
End If
soundbz = False
MMControl1.Command = "Close"

MMControl1.FileName = ProgramPath & "/声音文件/" & Index & ".wav"
MMControl1.Command = "Open"
MMControl1.Command = "Play"

If ind = 4 Then
prev = 0
text1.Caption = " "
ind = 0
End If
opnre = 0
If oflag = 0 Then
text1.Caption = " "
End If
oflag = 1
If Command1(Index).Caption "." Then
If text1.Caption " 0" Then
text1.Caption = text1.Caption & Command1(Index).Caption
Text = Mid(text1, 1, 1)
If Text = "." Then
text1 = "0" & text1
End If
Else
text1.Caption = " " & Command1(Index).Caption
End If
Else
If dflag = 0 Then
text1.Caption = text1.Caption & "."

dflag = 1
Else
Command6.SetFocus
Exit Sub
End If
End If
Command6.SetFocus
End Sub
Private Sub Command1_KeyPress(Index As Integer, KeyAscii As Integer)
If KeyAscii = 13 Then KeyAscii = 0

End Sub
Private Sub Command2_Click(Index As Integer)
soundbz = False
MMControl1.Command = "Close"
If Index = 0 Then MMControl1.FileName = ProgramPath & "/声音文件/加.wav"
If Index = 1 Then MMControl1.FileName = ProgramPath & "/声音文件/减.wav"
If Index = 3 Then MMControl1.FileName = ProgramPath & "/声音文件/乘.wav"
If Index = 2 Then MMControl1.FileName = ProgramPath & "/声音文件/除.wav"
If Index = 4 Then
MMControl1.FileName = ProgramPath & "/声音文件/等于.wav"
soundbz = True
End If
MMControl1.Command = "Open"
MMControl1.Command = "Play"
If opnre = 0 Or Index = 4 Then
If ind = 0 Then
prev = prev + Val(text1.Caption)
ElseIf ind = 1 Then
prev = prev - Val(text1.Caption)
ElseIf ind = 2 Then
If Val(text1.Caption) = 0 Then
text1 = "错误"
Beep
Command6.SetFocus
Exit Sub
Else
prev = prev / Val(text1.Caption)
End If
ElseIf ind = 3 Then
prev = prev * Val(text1.Caption)
End If
text1.Caption = Str(prev)
oflag = 0
End If
opnre = 1
ind = Index
dflag = 0

Command6.SetFocus
If Index = 4 Then
If Option1.Value = True Then text1 = Trim(Round(text1, 2))
If Option2.Value = True Then text1 = Trim(Round(text1, 3))
If Option3.Value = True Then text1 = Trim(Round(text1, 4))
If Option4.Value = True Then text1 = Trim(text1)
If text1 0 Then
Text = Mid(text1, 1, 1)
If Text = "." Then
text1 = "0" & text1
End If
End If
ls = Len(text1)
If ls 0 Then
For I = 1 To ls
t = Mid(text1, I, 1)
DoEvents
Do While (MMControl1.Mode 525) And soundbz

DoEvents
Loop
MMControl1.Command = "Close"
If t "." Then
If t = "-" Then
MMControl1.FileName = ProgramPath & "/声音文件/负.wav"
Else
MMControl1.FileName = ProgramPath & "/声音文件/" & t & ".wav"
End If
Else
MMControl1.FileName = ProgramPath & "/声音文件/10.wav"

End If
MMControl1.Command = "Open"
MMControl1.Command = "Play"
Next I
End If
End If
End Sub
Private Sub Command2_KeyPress(Index As Integer, KeyAscii As Integer)
If KeyAscii = 13 Then KeyAscii = 0
End Sub
Private Sub Command3_Click()
soundbz = False
MMControl1.Command = "Close"
MMControl1.FileName = ProgramPath & "/声音文件/归零.wav"
MMControl1.Command = "Open"
MMControl1.Command = "Play"
text1.Caption = " 0"
Command6.SetFocus
End Sub
Private Sub Command3_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then KeyAscii = 0
End Sub
Private Sub Command4_Click()
soundbz = False
MMControl1.Command = "Close"
MMControl1.FileName = ProgramPath & "/声音文件/清除.wav"
MMControl1.Command = "Open"
MMControl1.Command = "Play"

dflag = 0
prev = 0
oflag = 0
ind = 0
opnre = 0
text1.Caption = " 0"
Command6.SetFocus
End Sub
Private Sub Command4_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then KeyAscii = 0
End Sub
Private Sub Command5_Click()
soundbz = False
SaveSetting App.EXEName, "保留", "n1", Option1.Value
SaveSetting App.EXEName, "保留", "n2", Option2.Value
SaveSetting App.EXEName, "保留", "n3", Option3.Value
SaveSetting App.EXEName, "保留", "n4", Option4.Value
SaveSetting App.EXEName, "置顶", "yorn", Check1.Value
' MMControl1.Command = "Close"
Unload Me
End Sub
Private Sub Command5_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then KeyAscii = 0
End Sub
Private Sub Command6_Click()
Command2_Click (4)
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
If text1 = "错误!" Then
Command4_Click
End If
If KeyAscii = Asc(".") Then
I = 10
Command1_Click (I)
ElseIf KeyAscii = Asc("0") Then
I = 0
Command1_Click (I)
ElseIf KeyAscii = Asc("1") Then
I = 1
Command1_Click (I)
ElseIf KeyAscii = Asc("2") Then
I = 2
Command1_Click (I)
ElseIf KeyAscii = Asc("3") Then
I = 3
Command1_Click (I)
ElseIf KeyAscii = Asc("4") Then
I = 4
Command1_Click (I)
ElseIf KeyAscii = Asc("5") Then
I = 5
Command1_Click (I)
ElseIf KeyAscii = Asc("6") Then
I = 6
Command1_Click (I)
ElseIf KeyAscii = Asc("7") Then
I = 7
Command1_Click (I)
ElseIf KeyAscii = Asc("8") Then
I = 8
Command1_Click (I)
ElseIf KeyAscii = Asc("9") Then
I = 9
Command1_Click (I)
ElseIf KeyAscii = Asc("0") Then
I = 0
Command1_Click (I)
ElseIf KeyAscii = Asc("+") Then
I = 0
Command2_Click (I)
ElseIf KeyAscii = Asc("+") Then
I = 0
Command2_Click (I)
ElseIf KeyAscii = Asc("-") Then
I = 1
Command2_Click (I)
ElseIf KeyAscii = Asc("/") Then
I = 2
Command2_Click (I)
ElseIf KeyAscii = Asc("*") Then
I = 3
Command2_Click (I)
ElseIf KeyAscii = Asc("=") Or KeyAscii = 13 Then
I = 4
Command2_Click (I)
ElseIf KeyAscii = Asc("c") Or KeyAscii = Asc("C") Or KeyAscii = 27 Then
Command4_Click
ElseIf KeyAscii = Asc("d") Or KeyAscii = Asc("D") Then
Command3_Click
End If
End Sub
Private Sub Form_Load()
If App.PrevInstance = True Then
Unload Me
MsgBox "程序正在运行", 64, "提示"
End
End If
soundbz = True
dflag = 0
prev = 0
oflag = 0
ind = 0
opnre = 0
Clipboard.Clear
MMControl1.DeviceType = "WaveAudio"
ProgramPath = App.Path
Option1.Value = GetSetting(App.EXEName, "保留", "n1", True)
Option2.Value = GetSetting(App.EXEName, "保留", "n2", False)
Option3.Value = GetSetting(App.EXEName, "保留", "n3", False)
Option4.Value = GetSetting(App.EXEName, "保留", "n4", False)
Check1.Value = GetSetting(App.EXEName, "置顶", "yorn", 0)
End Sub
Private Sub Form_Unload(Cancel As Integer)
MMControl1.Command = "Close"
End
End Sub

最简单的

Private Sub Command1_Click()
Select Case Text3.Text
Case "+": Label1.Caption = Format(Val(Text1.Text) + Val(Text2.Text), "0.########")
Case "-": Label1.Caption = Format(Val(Text1.Text) - Val(Text2.Text), "0.########")
Case "*": Label1.Caption = Format(Val(Text1.Text) * Val(Text2.Text), "0.########")
Case "/": If Val(Text2.Text) = 0 Then MsgBox "除数不能为0": Exit Sub
Label1.Caption = Format(Val(Text1.Text) / Val(Text2.Text), "0.########")
Case Else: MsgBox "输入有误", 48
End Select
End Sub

'VB6.0 测试通过

Private Sub Command1_Click()
If IsNumeric(Text1.Text) <> True Or IsNumeric(Text3.Text) <> True Then
Label1.Caption = "请输入数字"
Exit Sub
End If
Select Case Text2.Text
Case "+", "+", "加", "加上"
Label1.Caption = (CStr(addition(Text1.Text, Text3.Text)))
Case "-", "-", "减", "减去"
Label1.Caption = (CStr(subtraction(Text1.Text, Text3.Text)))
Case "X", "*", "x", "×", "乘以", "乘"
Label1.Caption = (CStr(multiplication(Text1.Text, Text3.Text)))
Case "÷", "/", "除以", "除"
Label1.Caption = (CStr(division(Text1.Text, Text3.Text)))
Case Else
Label1.Caption = ("无法识别运算符")
End Select
End Sub

Function addition(a, b)
number1 = CDbl(a)
number2 = CDbl(b)
addition = number1 + number2
addition = NM(addition)
End Function
Function subtraction(a, b)
number1 = CDbl(a)
number2 = CDbl(b)
subtraction = a - b
subtraction = NM(subtraction)
End Function

Function multiplication(a, b)
number1 = CDbl(a)
number2 = CDbl(b)
multiplication = number1 * number2
multiplication = NM(multiplication)
End Function

Function division(a, b)
number1 = CDbl(a)
number2 = CDbl(b)
If b = 0 Then
division = ("除数不能为零")
Exit Function
End If
division = number1 / number2
division = NM(division)
End Function

Function NM(a)
number1 = CDbl(a)
If a > 0 And a < 1 Then
NM = "0" + CStr(a)
Exit Function
End If
If a < 0 And a > -1 Then
NM = "-0" + CStr(Abs(a))
Exit Function
End If
NM = CStr(a)
End Function

用text1和text2分别输入被除数和除数。text3输入“+ - * / ” ,然后单击窗体时在窗体上打印出结果:

Private Sub Form1_Click()
Cls
Dim a as Single , b as Single ,c as Double
a=Text1.Text:b=Text2.Text
If Text3.Text="+" Then
c=a+b
Ptint Format(d,#0.0##)
End If

If Text3.Text="-" Then
c=a-b
Ptint Format(d,#0.0##)
End If
If Text3.Text="*" Then
c=a*b
Ptint Format(d,#0.0##)
End If

If Text3.Text="+"
ElseIf Text2.Text=0 Then
MsgBox "出错"
Exit Sub
Else
c=a/b
Print Format(d,#0.0##)
End If
End Sub

没在计算机调试过,不知道是不是完全正确,你自己调试看看。


琼中黎族苗族自治县19454009281: VB做加减乘除的代码是什么? -
貊左君凯: 代码如下 '已增加操作数正确性验证及 除数不能为0验证Private Sub Command1_Click() If IsNumeric(Text1.Text) And Text1.Text <> "" And IsNumeric(Text2.Text) And Text2.Text <> "" Then Select Case Combo1.ListIndex Case 0 Text3.Text ...

琼中黎族苗族自治县19454009281: 用vb编写一个计算器程序,实现加减乘除, -
貊左君凯: Dim v As Boolean Dim s As Integer Dim X As Double Dim Y As DoublePrivate Sub Command1_Click(Index As Integer)If Form1.Tag = "T" ThenIf Index = 10 ThenText1.Text = "0"ElseText1.Text = Command1(Index).CaptionEnd IfForm...

琼中黎族苗族自治县19454009281: 有没有用VB设计计算器加减乘除的代码? -
貊左君凯: 最简单的,使用ADOSQL = 3-2 +5A = cn.excuse SQL

琼中黎族苗族自治县19454009281: 求VB加、减、乘、除的代码~!
貊左君凯: 向窗体上添加3个文本框4个按钮控件 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 ...

琼中黎族苗族自治县19454009281: VB程序代码:“加减乘除” -
貊左君凯: Private a As IntegerPrivate b As IntegerPrivate c As IntegerPrivate d As IntegerPrivate Const ds As String = "+-*/" Private Sub Command1_Click()a = Int(Rnd * 10 + 1)b = Int(Rnd * 10 + 1)d = Int(Rnd * 4 + 1)Text1.Text = a & Mid(ds, d, 1) ...

琼中黎族苗族自治县19454009281: VB加减乘除代码 -
貊左君凯: 在第一个命令按钮输入text3=val(text1)+val(text2)!第二text3=val(text1)-val(text2)!第三个text3=val(text1)*val(text2)第四个text3=val(text1)/val(text2)第五个text1.text="" text2.text=""text3.text=""

琼中黎族苗族自治县19454009281: 怎样用VB编写一个加减乘除计算器啊?? -
貊左君凯: 1、建立14个按钮,按钮的名字都要Command1,设置Index属性,0~9分别对应数字0~9,10为小数点,11为等号,12~14分别对应+、-、*和/. 2、输入如下代码: Dim Num1, Num2 As Single Dim StrNum1, StrNum2 As String Dim FirstNum As ...

琼中黎族苗族自治县19454009281: 求VB制作计算器的程序代码
貊左君凯: 需要3个文本框 第一文本框保存第一个数字 第二个文本框保存第二个数字第三个文本框保存结果然后四个命令按钮 作用是 加减乘除所以代码如下private sub command1_click()text3.text=val(text1.text)+val(text2.text)end sub一次类推private sub ...

琼中黎族苗族自治县19454009281: 求一个VB计算器代码 -
貊左君凯: 我是刚刚学VB的,只会做1个简单的,但是拥有+ - * /: Private Sub Command1_Click() Label1.Caption ="+" text3.text=val(text1.text) + val (text2.text) END subPrivate Sub Command2_Click() Label1.Caption ="-" text3.text=val(text...

琼中黎族苗族自治县19454009281: 如何用VB进行加减乘除运算? -
貊左君凯: 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....

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网