数组中删除一个指定的数据 Option explicit Public Sub Del()
'该过程从一个含有10个数组元素的数组中删除一个指定的数据,若该数据不存在,则给出提示。
'先产生10个[1,100]的随机整数
'从键盘上输入一个数,将该数从数组中删除 Randomize
Dim x As Integer Dim a() As Integer
Dim i As Integer, pos As Integer ReDim a(10) As Integer For i = 1 To 10
a(i) = Int(Rnd * 100) + 1 Next i
Form1.Print \原始数据:\ For i = 1 To 10
Form1.Print a(i); \ \ Next i
Form1.Print
x = Val(InputBox(\输入删除的数x=\ pos = 0
For i = 1 To 10
If x = a(i) Then
------1----- ‘pos=i Exit For End If Next i
If pos <> 0 Then
For i = -----2------ To 9 ‘pos -------3------- ‘a(i)=a(I+1) Next i
ReDim Preserve a(9) '保留原数组中的9个数 Form1.Print \删除后数据:\ For i = 1 To 9
Form1.Print a(i); \ \ Next i
Form1.Print Else
MsgBox \数组中未找到\删除不成功\ End If End Sub
数组的移动
Public Sub p(a%())
'该过程是用于整理数组a,使其中小于零的元素移到数组的前端, '大于零的元素移到数组的后端,等于零的元素留在数组的中间。 Dim i%, low%, high%, t% low = 0 i = 0
high = UBound(a) - 1 do While ----1---- If a(i) < 0 Then t = a(i)
a(i) = a(low) a(low) = t ----2----
i = i + 1
ElseIf a(i) > 0 Then t = a(i)
a(i) = a(high) a(high) = t -----3----- Else
----4---- End If Loop End Sub 参考答案:
Public Sub p(a%())
'该过程是用于整理数组a,使其中小于零的元素移到数组的前端, '大于零的元素移到数组的后端,等于零的元素留在数组的中间。 Dim i%, low%, high%, t% low = 0 i = 0
high = UBound(a)-1 Do While i <= high If a(i) < 0 Then t = a(i)
a(i) = a(low) a(low) = t low = low + 1 i = I+1
ElseIf a(i) > 0 Then t = a(i)
a(i) = a(high) a(high) = t
high = high - 1 Else i = i + 1 End If Loop End Sub
Private Sub Form_Click() Dim a(4) As Integer For i = 0 To 3
a(i) = InputBox(\ Print a(i); Next i Call p(a) Print
For i = 0 To 3 Print a(i); Next i End Sub 匹配的次数
Option Explicit
Public Sub findstr()
'该过程通过调用matchCount函数计算子串s2在母串s1中匹配的次数 Dim s1 As String, s2 As String
s1 = \母串 s2 = \子串 Form1.Print ----1---- ‘matchCount(s1, s2) End Sub
Function matchCount(str1 As String, str2 As String) As Integer '本函数计算子串str2在母串str1中的匹配次数
Dim num As Integer, i As Integer, pos As Integer num = 0
For i = 1 To ----2---- '从第1个字符开始循环找 ‘Len(str1)
pos = ----3---- ‘InStr(Right(str1, Len(str1) - i + 1), str2) If pos > 0 Then '找到了指定的字符串 num = num + 1 '次数加1
----4---- '继续向前找 ‘i = i + pos - 1 Else
Exit For '没找到,退出 End If Next i
matchCount = num
End Function
字符数组移动m个位置
Public Sub MoveStr(a$(), m%, Tag As Boolean)
'该过程是把字符数组移动m个位置,当Tag为True左移,则前m个字符移到字符数组尾。
Dim i%, j%, t$ Dim c
If Tag Then
For i = 1 To m c = a(0)
for j=0 to ----1---- a(j) = a(j + 1) Next j
-----2----- Next i Else
For i = 1 To m ------3------
for j=Ubound(A) -----4------ a(j) = a(j - 1) Next j a(0) = c Next i End If End Sub 参考答案:
Option Explicit
Public Sub MoveStr(a$(), m%, Tag As Boolean)
'该过程是把字符数组移动m个位置,当Tag为True左移,则前m个字符移到字符数组尾。
Dim i%, j%, t$ Dim c
If Tag Then
For i = 1 To m c = a(0)
For j = 0 To UBound(a) - 1 a(j) = a(j + 1) Next j
a(UBound(a)) = c Next i Else
For i = 1 To m c = a(UBound(a))
For j = UBound(a) To 1 Step -1
a(j) = a(j - 1) Next j a(0) = c Next i End If End Sub
Private Sub Form_Click() Dim a(5) As String For i = 0 To 5
a(i) = InputBox(\ Print a(i); Next i Print
Call MoveStr(a, 3, False) For i = 0 To 5 Print a(i); Next i End Sub
杨辉三角形图案 Option Explicit Public Sub prt()
'该过程是输出由数字组成的如下所示杨辉三角形图案 ' 1 ' 1 1 ' 1 2 1 ' 1 3 3 1 ' 1 4 6 4 1 ' 1 5 10 10 5 1 ' 1 6 15 20 15 6 1 Const n = 8
Dim a(n + 1) As Byte, i As Integer, j As Integer Dim s As String '给数组a赋初值 For i = 1 To n + 1 a(i) = 0 Next i
'计算并打印第一个数 a(1) = 1
Form1.Print Space(2 * n + 2); '计算数字前的空格数 Form1.Print a(1)
'计算其余行的数并打印 For i = 2 To ----1---- ‘n + 1 a(i) = a(i - 1)
For j = ----2---- To 2 Step –1 ‘i - 1
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库VB新的上机等级考试程序调试题及答案在线全文阅读。
相关推荐: