1、上机操作练习题 2011-111 某数组有 20 个元素,其值为随机产生的两位整数。要求将前 10 个元素与后 10 个元素对换。即第 1 个元素与第 20 个元素互换,第 2 个数与第 19 个元素互换,第 10 个元素与第 11个元素互换。使用 Picture 控件输出数组原数组值和对换后各元素值。Private Sub Command1_Click()Dim a(20), i As Integer, b As IntegerPicture1.Print “原数组为:“Picture1.PrintFor i = 1 To 20 随机产生两位数的整数a(i) = Int(Rnd * 90 +
2、 10)Picture1.Print a(i);Next iPicture1.PrintFor i = 1 To 10 元素互换b = a(i): a(i) = a(21 - i): a(21 - i) = bNext iPicture1.Print “互换后的数组为:“Picture1.PrintFor i = 1 To 20 互换后元素输出Picture1.Print a(i);Next iEnd Sub2从数组中删除一个数。先定义一个一维数组,大小、数组元素值自定。从键盘上输入一个数,若这个数在数组当中,就把它删除;若这个数不在数组中,提示“该数不在数组中”注意:1)如果数组中存在多个与
3、该数相同的元素,每一个都能删除;2)能实现多次删除Option ExplicitOption Base 1Dim a()Private Sub Command1_Click()Dim n As Integer, i As Integer, j As Integer, c As Integern = Text1.TextFor i = 1 To UBound(a)If n = a(i) ThenFor j = i To UBound(a) - 1a(j) = a(j + 1)Next ji = i - 1c = c + 1End IfNext iPrintIf c = 0 ThenMsgBox
4、(“您输入的数不在该数组中“)ElseReDim Preserve a(UBound(a) - c)Print “删除元素后的数组为: “For i = 1 To UBound(a)Print a(i);Next iEnd IfEnd Sub赋值Private Sub Form_Load()Dim i As Integera = Array(10, 20, 30, 30, 40, 50, 60, 70, 80, 90)Print “原数组为: “For i = 1 To UBound(a)Print a(i);Next iEnd Sub3数组 a 中存放着升序排列的数据,使用 array赋值,
5、将 inputbox 输入的新数 x 插入到数组中,插入后数组仍有序。注意:要求利用动态数组,能够实现多次插入Private Sub Command1_Click()Dim b As Integer, c As Integer, i As Integer, j As Integer, k As Integerj = 1 定义 j=1Option ExplicitOption Base 1Dim a()Private Sub Command1_Click()Dim i As Integer, j As Integer, n As Integer, find As Booleann = Input
6、Box(“请输入数 n:“)PrintPrint “插入元素后的数组为 “PrintFor i = 1 To UBound(a)If n max Thenmax = gs(i).score(j)ElseIf gs(i).score(j) 0 ThenText4.Text = e - fText5.Text = e + fElseIf d 65 And x 97 And x 48 And x Z azBY by即第 1 个字母与第 26 个字母互换,第 i 个字母与第(26-i+1) 个字母互换。在文本框中任意输入一个字符串,调用该函数将密码显示在另一个文本框中。Private Sub Comm
7、and1_Click()Dim i As Integer, n As Integer, a As Integern = Len(Text1.Text)For i = 1 To na = Asc(Mid(Text1.Text, i, 1)If a 64 And a 96 And a 0n = m: m = r:r = n Mod mLoopgy = mEnd FunctionPrivate Function gb(ByVal n As Integer, ByVal m As Integer)gb = m * n / gy(n, m)End Function10. 编写计算阶乘的 Function
8、 过程,利用 ex 的下列公式计算 e(直到最后一项小于 10-6 为止)的近似值。ex1+x/1!+x 2/2!+x3/3!+xn/n!Public Function n(ByRef m As Integer) As LongDim i As Integern = 1For i = 1 To mn = n * iNext iEnd FunctionPrivate Sub Command1_Click()Dim m As Integer, e As Doublem = 1e = 1Do While (1 / n(m) 10 (-6)e = e + 1 / n(m)m = m + 1LoopPr
9、int eEnd Sub11输入整数 N,显示具有 N 行的杨辉三角形,要求按正三角形的形式在图片框中显示。Private Sub Command1_Click()Dim n As Integer, e As Integer, g As Integer, i As Integer, j As Integer, a()Picture1.Clsn = InputBox(“请输入一个数:“)ReDim a(n, n)e = 80For i = 1 To ne = e - 5: g = eFor j = 1 To nIf j i Thena(i, j) = 0ElseIf i = j Thena(i,
10、 j) = 1ElseIf i j Thena(i, j) = a(i - 1, j) + a(i - 1, j - 1)End IfIf a(i, j) = 0 ThenExit ForElsePicture1.Print Tab(g); a(i, j);g = g + 10End IfNext jPicture1.PrintNext iEnd Sub12编写一个过程,输出以下菱形图形:ABBBCCCCCDDDDDDDCCCCCBBBA主程序输入一个奇数(3) ,调用过程在窗体上输出图形。Private Sub Form_Load()Dim i As Integer, j As Intege
11、r, k As Integer, n As Integer, g As Integer, a As IntegerWindowState = 2Shown = InputBox(“请输入 n“)g = 50For i = 1 To 2 * nIf i n Thena = a + 1g = g + 1Print Tab(g + 1);For k = 2 * (n - a) - 1 To 1 Step -1Print Chr(64 + n - a);Next kEnd IfNext iEnd Sub13编写一个将十进制转换为二进制字符串输出的函数。调用函数计算用户输入一个十进制数,在文本框中输出二
12、进制值。注意:输入数据为负数,应该将符号位输出如 -13 结果 -1101Public Function erjinzhi(n As Double) As StringDim r As Integern = Abs(n)r = n Mod 2Do While n = 1erjinzhi = Str(r) “计算数列 1, ,1,2,3,5,8前 40项之和为:“End Sub19. 单击读数按钮从文件 in.txt 中读入一个整数(先用记事本创建一个 in.txt 文件,其中输入任意一个素数,如图 3 中输入的是 101) ,单击计算按钮则计算出大于该数的第 1 个素数,单击存盘将结果保存到文
13、件 out.txt 中,如图 5。运行界面如图 13-3 所示。图 3 图 4 图 5 Option ExplicitPublic m As LongPrivate Sub Command1_Click()Open “in.txt“ For Input As #2Input #2, mClose #2End SubPrivate Sub Command2_Click()Dim b As Integer, j As Integerb = 1Do While b 0m = m + 1For j = 2 To m / 2If m Mod j = 0 ThenExit ForElseb = 0End
14、IfNext jLoopEnd SubPrivate Sub Command3_Click()Open “out.txt“ For Output As #3Print #3, mClose #3End Sub20. 如图 6 所示。当选中某一个单选按钮并单击“计算”按钮时,则计算出该单选按钮标题所指明的所有素数之和,并在文本框中显示出来;单击“存盘”按钮将计算所得的素数之和以 out.txt 文件名存入“c:exam”目录中。判断一个数是否为素数的过程自己编写,存盘过程已经给出。编写两个命令按钮的Click 事件过程。图 6Option ExplicitDim sum As LongPriva
15、te Sub Command1_Click()Dim a As Integer, b As Integer, c As Boolean, j As Integer, sum As LongIf Option1 = True Thena = 100: b = 200Elsea = 200: b = 400End IfFor j = a To bIf isprime(j) = True Thensum = sum + jEnd IfNext jText1.Text = sumEnd SubPrivate Sub Command2_Click()Dim out As Stringout = “out
16、.txt“Call putdata(out, Text1.Text)End SubSub putdata(t_FileName As String, T_Str As Variant)Open “c:exam“ & t_FileName For Output As #1Print #1, T_StrClose #1End SubFunction isprime(i As Integer) As BooleanDim j As Integerisprime = TrueFor j = 2 To i - 1If i Mod j = 0 Thenisprime = FalseExit ForEnd IfNext jEnd Function