用记事本把这段代码复制过去,然后另存为frm就可以了
VERSION 500
Begin VBForm frmMain
Appearance = 0 'Flat
BackColor = &H80000005&
BorderStyle = 3 'Fixed Dialog
Caption = "简单计算器"
ClientHeight = 2430
ClientLeft = 45
ClientTop = 435
ClientWidth = 3150
KeyPreview = -1 'True
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 2430
ScaleWidth = 3150
ShowInTaskbar = 0 'False
StartUpPosition = 3 '窗口缺省
Begin VBCommandButton Command10
Caption = "^"
Height = 375
Left = 1920
TabIndex = 22
Top = 1920
Width = 495
End
Begin VBPictureBox picDisplay
Appearance = 0 'Flat
BackColor = &H80000005&
ForeColor = &H80000008&
Height = 255
Left = 120
ScaleHeight = 225
ScaleWidth = 2865
TabIndex = 19
Top = 120
Width = 2895
Begin VBTextBox Text1
Alignment = 1 'Right Justify
BorderStyle = 0 'None
Height = 255
Left = 240
Locked = -1 'True
TabIndex = 20
Top = 0
Width = 2535
End
Begin VBLabel lblOperation
Appearance = 0 'Flat
BackColor = &H80000005&
ForeColor = &H80000008&
Height = 255
Left = 60
TabIndex = 21
Top = 0
Width = 255
End
End
Begin VBCommandButton Command9
Caption = "退格"
Height = 375
Left = 1920
TabIndex = 18
Top = 480
Width = 495
End
Begin VBCommandButton Command8
Caption = "清零"
Height = 375
Left = 2520
TabIndex = 17
Top = 480
Width = 495
End
Begin VBCommandButton cmdDot
Caption = ""
Height = 375
Left = 120
TabIndex = 16
Top = 1920
Width = 495
End
Begin VBCommandButton Command7
Caption = "Sqrt"
Height = 375
Left = 2520
TabIndex = 15
Top = 1920
Width = 495
End
Begin VBCommandButton Command6
Caption = "="
Height = 375
Left = 1320
TabIndex = 14
Top = 1920
Width = 495
End
Begin VBCommandButton Command5
Caption = "/"
Height = 375
Left = 2520
TabIndex = 13
Top = 1440
Width = 495
End
Begin VBCommandButton Command4
Caption = ""
Height = 375
Left = 1920
TabIndex = 12
Top = 1440
Width = 495
End
Begin VBCommandButton Command3
Caption = "-"
Height = 375
Left = 2520
TabIndex = 11
Top = 960
Width = 495
End
Begin VBCommandButton Command2
Caption = "+"
Height = 375
Left = 1920
TabIndex = 10
Top = 960
Width = 495
End
Begin VBCommandButton Command1
Caption = "9"
Height = 375
Index = 9
Left = 1320
TabIndex = 9
Top = 480
Width = 495
End
Begin VBCommandButton Command1
Caption = "8"
Height = 375
Index = 8
Left = 720
TabIndex = 8
Top = 480
Width = 495
End
Begin VBCommandButton Command1
Caption = "7"
Height = 375
Index = 7
Left = 120
TabIndex = 7
Top = 480
Width = 495
End
Begin VBCommandButton Command1
Caption = "6"
Height = 375
Index = 6
Left = 1320
TabIndex = 6
Top = 960
Width = 495
End
Begin VBCommandButton Command1
Caption = "5"
Height = 375
Index = 5
Left = 720
TabIndex = 5
Top = 960
Width = 495
End
Begin VBCommandButton Command1
Caption = "4"
Height = 375
Index = 4
Left = 120
TabIndex = 4
Top = 960
Width = 495
End
Begin VBCommandButton Command1
Caption = "3"
Height = 375
Index = 3
Left = 1320
TabIndex = 3
Top = 1440
Width = 495
End
Begin VBCommandButton Command1
Caption = "2"
Height = 375
Index = 2
Left = 720
TabIndex = 2
Top = 1440
Width = 495
End
Begin VBCommandButton Command1
Caption = "1"
Height = 375
Index = 1
Left = 120
TabIndex = 1
Top = 1440
Width = 495
End
Begin VBCommandButton Command1
Caption = "0"
Height = 375
Index = 0
Left = 720
TabIndex = 0
Top = 1920
Width = 495
End
End
Attribute VB_Name = "frmmain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Data As Double
Private lOperation As Long
Private fClear As Boolean
Private Sub cmdDot_Click()
If InStr(1, Text1Text, "") <= 0 Then Text1Text = Text1Text & ""
End Sub
Private Sub Command1_Click(Index As Integer)
If fClear = True Then Text1Text = "": fClear = False
Text1Text = Text1Text & Index
End Sub
Private Sub Command10_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1Text = Data + Val(Text1Text)
Case 2: Text1Text = Data - Val(Text1Text)
Case 3: Text1Text = Data Val(Text1Text)
Case 4: If Val(Text1Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1Text = Data / Val(Text1Text)
Case 5: Text1Text = Data ^ Val(Text1Text)
End Select
End If
Data = Val(Text1Text)
fClear = True
lOperation = 5
lblOperationCaption = "^"
End Sub
Private Sub Command2_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1Text = Data + Val(Text1Text)
Case 2: Text1Text = Data - Val(Text1Text)
Case 3: Text1Text = Data Val(Text1Text)
Case 4: If Val(Text1Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1Text = Data / Val(Text1Text)
Case 5: Text1Text = Data ^ Val(Text1Text)
End Select
End If
Data = Val(Text1Text)
fClear = True
lOperation = 1
lblOperationCaption = "+"
End Sub
Private Sub Command3_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1Text = Data + Val(Text1Text)
Case 2: Text1Text = Data - Val(Text1Text)
Case 3: Text1Text = Data Val(Text1Text)
Case 4: If Val(Text1Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1Text = Data / Val(Text1Text)
Case 5: Text1Text = Data ^ Val(Text1Text)
End Select
End If
Data = Val(Text1Text)
fClear = True
lOperation = 2
lblOperationCaption = "-"
End Sub
Private Sub Command4_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1Text = Data + Val(Text1Text)
Case 2: Text1Text = Data - Val(Text1Text)
Case 3: Text1Text = Data Val(Text1Text)
Case 4: If Val(Text1Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1Text = Data / Val(Text1Text)
Case 5: Text1Text = Data ^ Val(Text1Text)
End Select
End If
Data = Val(Text1Text)
fClear = True
lOperation = 3
lblOperationCaption = ""
End Sub
Private Sub Command5_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1Text = Data + Val(Text1Text)
Case 2: Text1Text = Data - Val(Text1Text)
Case 3: Text1Text = Data Val(Text1Text)
Case 4: If Val(Text1Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1Text = Data / Val(Text1Text)
Case 5: Text1Text = Data ^ Val(Text1Text)
End Select
End If
Data = Val(Text1Text)
fClear = True
lOperation = 4
lblOperationCaption = "/"
End Sub
Private Sub Command6_Click()
If fClear = False Then
Select Case lOperation
Case 0
Case 1: Text1Text = Data + Val(Text1Text)
Case 2: Text1Text = Data - Val(Text1Text)
Case 3: Text1Text = Data Val(Text1Text)
Case 4: If Val(Text1Text) = 0 Then MsgBox "除数不等于0": Exit Sub Else Text1Text = Data / Val(Text1Text)
Case 5: Text1Text = Data ^ Val(Text1Text)
End Select
End If
Data = Val(Text1Text)
fClear = True
lOperation = 0
lblOperationCaption = ""
End Sub
Private Sub Command7_Click()
If Val(Text1Text) <= 0 Then MsgBox "开方数大于等于0"
Text1Text = Sqr(Val(Text1Text))
Data = Val(Text1Text)
fClear = True
lOperation = 0
lblOperationCaption = ""
End Sub
Private Sub Command8_Click()
Text1Text = ""
lOperation = 0
lblOperationCaption = ""
End Sub
Private Sub Command9_Click()
If Len(Text1Text) > 0 Then Text1Text = Left(Text1Text, Len(Text1Text) - 1)
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("0"): Command1_Click 0
Case Asc("1"): Command1_Click 1
Case Asc("2"): Command1_Click 2
Case Asc("3"): Command1_Click 3
Case Asc("4"): Command1_Click 5
Case Asc("5"): Command1_Click 4
Case Asc("6"): Command1_Click 6
Case Asc("7"): Command1_Click 7
Case Asc("8"): Command1_Click 8
Case Asc("9"): Command1_Click 9
Case Asc(""): cmdDot_Click
Case Asc("+"): Command2_Click
Case Asc("-"): Command3_Click
Case Asc(""): Command4_Click
Case Asc("/"): Command5_Click
Case Asc("^"): Command10_Click
Case Asc("s"): Command7_Click
Case Asc("S"): Command7_Click
Case vbKeyBack: Command9_Click
Case Asc("`"): Command8_Click
Case Asc("="): Command6_Click
Case 13: Command6_Click
End Select
KeyAscii = 0
End Sub
Sqr 函数
返回一个 Double,指定参数的平方根。
Sqr 函数示例
本示例使用 Sqr 函数来计算某数的平方根。
Dim MySqr
MySqr = Sqr(4) ' 返回 2。
MySqr = Sqr(23) ' 返回 479583152331272。
MySqr = Sqr(0) ' 返回 0。
MySqr = Sqr(-4) ' 生成一个运行时错误(负数不能用此函数开平方根)。
Dim intArray(100) As Single, sglAverage As Single
Private Sub Command1_Click()
Randomize (Timer)
Dim i As Integer, intSum As Integer
MePicture1Cls
For i = 1 To 100
intArray(i) = Int(Rnd(i) 5 + 95)
MePicture1Print intArray(i);
If i Mod 10 = 0 Then MePicture1Print
intSum = intSum + intArray(i)
Next i
sglAverage = intSum / 100
Label1Caption = "Sum=" & intSum
Label2Caption = "Average=" & sglAverage
End Sub
Private Sub Command2_Click()
Dim i As Integer, k0 As Integer, k(4) As Integer
For i = 0 To 4
MePicture2(i)Cls
Next i
For i = 1 To 100
If Sqr((intArray(i) - sglAverage) ^ 2) >= 167 Then
k(0) = k(0) + 1
MePicture2(0)Print intArray(i);
If k(0) Mod 10 = 0 Then MePicture2(0)Print
ElseIf Sqr((intArray(i) - sglAverage) ^ 2) >= 133 And Sqr((intArray(i) - sglAverage) ^ 2) < 167 Then
k(1) = k(1) + 1
MePicture2(1)Print intArray(i);
If k(1) Mod 10 = 0 Then MePicture2(1)Print
ElseIf Sqr((intArray(i) - sglAverage) ^ 2) >= 1 And Sqr((intArray(i) - sglAverage) ^ 2) < 133 Then
k(2) = k(2) + 1
MePicture2(2)Print intArray(i);
If k(2) Mod 10 = 0 Then MePicture2(2)Print
ElseIf Sqr((intArray(i) - sglAverage) ^ 2) >= 067 And Sqr((intArray(i) - sglAverage) ^ 2) < 1 Then
k(3) = k(3) + 1
MePicture2(3)Print intArray(i);
If k(3) Mod 10 = 0 Then MePicture2(3)Print
Else
k(4) = k(4) + 1
MePicture2(4)Print intArray(i);
If k(4) Mod 10 = 0 Then MePicture2(4)Print
End If
Next i
For i = 0 To 4
MeLabel8(i)Caption = "共计" & k(i)
Next i
End Sub
dim i as integer
for i =1 to 100
if sqr(i) = Cint(sqr(i)) then
print i & "的开方数为整数!"
end if
next i
以上就是关于如何用VB做一个简单计算器 要求实现加减乘除乘方开方功能全部的内容,包括:如何用VB做一个简单计算器 要求实现加减乘除乘方开方功能、VB中的“开方”怎么大呀、vb编程实例 交互式窗口,输入100个数据,求平均值、平方后再开方和自动分类等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)