📄 practice8_8.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "在过程中使用数组做参数"
ClientHeight = 5925
ClientLeft = 60
ClientTop = 345
ClientWidth = 8220
BeginProperty Font
Name = "宋体"
Size = 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
LinkTopic = "Form1"
ScaleHeight = 5925
ScaleWidth = 8220
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox Text5
Height = 855
Left = 3360
MultiLine = -1 'True
ScrollBars = 1 'Horizontal
TabIndex = 9
Top = 4560
Width = 4215
End
Begin VB.CommandButton Command5
Caption = "删除某指定的元素"
Height = 735
Left = 360
TabIndex = 8
Top = 4680
Width = 2775
End
Begin VB.TextBox Text4
Height = 855
Left = 3360
MultiLine = -1 'True
ScrollBars = 1 'Horizontal
TabIndex = 7
Top = 3600
Width = 4215
End
Begin VB.CommandButton Command4
Caption = "删除某位置的元素"
Height = 735
Left = 360
TabIndex = 6
Top = 3600
Width = 2775
End
Begin VB.TextBox Text3
Height = 855
Left = 3360
MultiLine = -1 'True
ScrollBars = 1 'Horizontal
TabIndex = 5
Top = 2520
Width = 4215
End
Begin VB.CommandButton Command3
Caption = "插入数组元素"
Height = 735
Left = 360
TabIndex = 4
Top = 2520
Width = 2655
End
Begin VB.TextBox Text2
Height = 855
Left = 3360
MultiLine = -1 'True
ScrollBars = 1 'Horizontal
TabIndex = 3
Top = 1440
Width = 4215
End
Begin VB.CommandButton Command2
Caption = "添加数组元素"
Height = 735
Left = 360
TabIndex = 2
Top = 1440
Width = 2535
End
Begin VB.TextBox Text1
Height = 855
Left = 3360
MultiLine = -1 'True
ScrollBars = 1 'Horizontal
TabIndex = 1
Top = 360
Width = 4215
End
Begin VB.CommandButton Command1
Caption = "读入数组元素"
Height = 735
Left = 360
TabIndex = 0
Top = 240
Width = 2415
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim x() As Integer, n As Integer
Sub ReadArray(A() As Integer) ' 读入
For i = 1 To n
A(i) = Val(InputBox("请输入数组的第" & Str(i) & "个元素"))
Next i
End Sub
Sub AddArray(A() As Integer) ' 添加
A(n) = Val(InputBox("请输入要添加在数组最后的元素"))
End Sub
Sub InsertArray(A() As Integer, k, num) '插入
For i = n To k + 1 Step -1
A(i) = A(i - 1)
Next i
A(k) = num
End Sub
Sub FindArray(A() As Integer, num, pos) '查找
pos = 0
For i = 1 To n
If A(i) = num Then
pos = i
Exit For
End If
Next i
End Sub
Sub DeleteArray(A() As Integer, pos) '删除
For i = pos To n - 1
A(i) = A(i + 1)
Next i
End Sub
Private Sub Command1_Click() '读取数组
n = 20
ReDim x(1 To n) As Integer
Call ReadArray(x())
Text1.Text = ""
For i = 1 To n
Text1.Text = Text1.Text & Str(x(i))
Next i
End Sub
Private Sub Command2_Click() '附加元素
n = n + 1
ReDim Preserve x(1 To n)
Call AddArray(x())
Text2.Text = ""
For i = 1 To n
Text2.Text = Text2.Text & Str(x(i))
Next i
End Sub
Private Sub Command3_Click() '插入元素
num = Val(InputBox("请输入要插入的数"))
pos = Val(InputBox("请输入要插入的位置"))
n = n + 1
ReDim Preserve x(1 To n)
Call InsertArray(x(), pos, num)
Text3.Text = ""
For i = 1 To n
Text3.Text = Text3.Text & Str(x(i))
Next i
End Sub
Private Sub Command4_Click() '删除元素
pos = Val(InputBox("请输入要删除的元素位置"))
If pos >= 1 And pos <= n Then
Call DeleteArray(x(), pos)
n = n - 1
Text4.Text = ""
For i = 1 To n
Text4.Text = Text4.Text & Str(x(i))
Next i
Else
MsgBox "指定的位置超出范围"
End If
End Sub
Private Sub Command5_Click()
num = Val(InputBox("请输入要删除的元素"))
Call FindArray(x(), num, pos)
If pos >= 1 And pos <= n Then
Call DeleteArray(x(), pos)
n = n - 1
Text5.Text = ""
For i = 1 To n
Text5.Text = Text5.Text & Str(x(i))
Next i
Else
MsgBox "没有找到指定的元素"
End If
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -