1、【实验一参考答案】【完善题】1、任意读入一个字符串,统计其中小写字母的个数。 【提示:使用求串长函数 Len、获取串中字符函数 Mid。 】Private Sub Command1_Click()Dim s As String, c As String * 1, k As Integer, n As Integers = Text1n = 0For k = 1 To len(s)c =mid(s, k, 1)If c = “a“ And c a(mid) And low n ThenPrint “数组 a 中没有与“; x; “相等的数!“Exit SubElse删除数组中与 x 等值的数:待
2、删元素的下标是 i,将其后的元素一一前移For j = i + 1 To na(j - 1) = a(j)Next j将数组元素的个数减少一个ReDim Preserve a(n - 1)End IfPrint “删除“; x; “ 后的数组是:“For i = 1 To n - 1Print a(i);Next iPrintEnd Sub二、编程题将以下 23 矩阵转置成 32 矩阵:6 36 5 4 5 23 2 1 4 1Option Base 1Private Sub Command1_Click()Dim a%(2, 3), b%(3, 2), i%, j%, k%k = 6For
3、i = 1 To 2For j = 1 To 3a(i, j) = kb(j, i) = a(i, j)k = k - 1Next jNext iFor i = 1 To 3For j = 1 To 2Print b(i, j);Next jPrintNext iEnd Sub三、思考题(提示:每个数据占 6 列,输出一行换两行,每行数据在输出前要输出若干空格)编程以“金字塔形式”输出杨辉三角形的前 10 行。 (输出到图片框中,效果见下图)Option Base 1Private Sub Command1_Click()Const n% = 10Dim a%(n, n), i%, j%For
4、 i = 1 To na(i, i) = 1: a(i, 1) = 1Next iFor i = 3 To nFor j = 2 To i - 1a(i, j) = a(i - 1, j - 1) + a(i - 1, j)Next jNext iFor i = 1 To nFor j = n - i To 1 Step -1Picture1.Print “ “;Next jFor j = 1 To iPicture1.Print CStr(a(i, j) Next jPicture1.PrintPicture1.PrintNext iEnd Sub【实验四参考答案】一、完善题:调用求最大公约
5、数函数,求得两正整数的最小公倍数。 【提示:两数最小公倍数=两数乘积/最大公约数】以下函数求两正整数的最大公约数Private Function GYS(_Byval x%,byval y%_) As IntegerDim r%If x 0x = yy = r_ r = x Mod y _Loop_GYS=y_End FunctionPrivate Sub Command1_Click()Dim a%, b%, gbs%a = InputBox(“a“)b = InputBox(“b“)If GYS(a, b) a(k) Then k = jNext jIf k m Then m = a(i)
6、Next iPrint “ MAX=“; mn = 0 n 记录最大值的个数For i = 1 To sIf a(i) = m Then_xb = i xb 记录最大值元素的下标Call SC(a(), s, xb) 找到一个最大值就删除一个End IfNext iEnd SubPrivate Sub SC(a%(), ByVal s%, ByVal xb%) 删除一个最大值Dim i%For i = xb + 1 To s_Next iEnd Sub【实验六参考答案】一、完善题:1、以下程序能计算 1+2+3+100 的和。请填空。Option ExplicitPrivate Sub Com
7、mand1_Click()Dim s%, i%For i = 1 To 100s = fun(i)Next iPrint sEnd SubPrivate Function fun(ByVal n%) As IntegerStatic sum as Integersum = sum + nfun=sumEnd Function2、使用全局变量、地址传递、函数返回值 3 种方法,求得任意生成的 10 个两位正整数的平均值、最大值和最小值。参考界面如下:Option ExplicitOption Base 1Dim max%Private Sub Command1_Click()Dim min%,
8、ave!, i%Dim a%(10)List1.Clear 清空列表框 1List2.Clear 清空列表框 2RandomizeFor i = 1 To 10a(i) = Int(Rnd * (99 - 10 + 1) + 10)List1.AddItem a(i)Next iave = fun(a, 10, min)List2.AddItem “平均值为:“ & aveList2.AddItem “最大值为:“ & maxList2.AddItem “最小值为:“ & minEnd SubPrivate Function fun(x%(), ByVal n%, ByRef min%) As
9、 SingleDim i%max = x(1): min = x(1): fun = x(1)For i = 2 To nfun = fun + x(i)If x(i) max Thenmax = x(i)ElseIf x(i) Maxv ThenMaxv = a(i): maxp = iEnd IfNext iCall move_f(a, maxp)End SubPrivate Sub move_f(a() As Integer, k As Integer)Dim i As IntegerFor i = k + 1 To UBound(a)a(i) = a(i+1)Next iReDim a
10、(UBound(a) - 1)End Sub【答案】Option ExplicitOption Base 1Dim a() As Integer, n As IntegerPrivate Sub Command1_Click()Dim i As Integern = InputBox(“请输入数组个数 “, , 10)ReDim a(n)For i = 1 To na(i) = Int(Rnd * 100) + 1Text1 = Text1 & Str(a(i)Next iCall Lookup(a)For i = 1 To n 1 Text2 = Text2 & Str(a(i)Next i
11、End SubPrivate Sub Lookup(a() As Integer)Dim Maxv As Integer, maxp As Integer, i As IntegerMaxv = a(1): maxp = 1For i = 2 To nIf a(i) Maxv ThenMaxv = a(i): maxp = iEnd IfNext iCall move_f(a, maxp)End SubPrivate Sub move_f(a() As Integer, k As Integer)Dim i As IntegerFor i = k + 1 To UBound(a) a(i - 1) = a(i) Next iReDim Preserve a(UBound(a) - 1) End Sub