浙江省计算机二级VB试题
综合实训(一)综合实训(一)(一)程序阅读题,阅读下列程序,写出运行结果(一)程序阅读题,阅读下列程序,写出运行结果1程序Private Function ff(a As Integer) As IntegerStatic c As IntegerDim b As Integerb = b + 1c = c + 1ff = a + b + cEnd FunctionPrivate Sub Form_Click()Dim a As Integer, I As Integera = 2For I = 1 To 3Print ff(a);Next IEnd Sub程序运行时单击窗体,窗体上的输出结果为。2程序Private Sub Form_click()Dim idec As Integer, i As IntegerDim iDecR(16) As IntegerDim strDecR As Stringidec = Val(InputBox(“输入数据 idec=?“)i = 0Do While idec = 0strDecR = strDecR + Trim(str(iDecR(i)i = i - 1LoopPrint strDecREnd Sub程序运行时单击窗体,由键盘输入一个整数 22,请写出窗体上的输出结果。3 程序Private Sub Form_Click()Dim a, i%, n%,t%n=InputBox(“Enter N=?“)a=array(1,2,3,4,5,6,7,8,9,10) 给数组赋值 a(0)=1,a(1)=2For i =1 To n2t=a(i) : a(i)=a(n-i+1) : a(n-i+1)=tNext IFor i=0 to Ubound(a)Print a(i);Next IPrintEnd Sub程序运行后,两次单击窗体,分别输入 8 和 6 ,则输出结果为:_。4程序Dim x As Integer, y As Integer, z As IntegerPrivate Sub Form_Click()Dim x As Integerx = 1: y = 2: z = 3Call FunA(y)z = FunB(x)Print x, y, zEnd SubPublic Sub FunA(z As Integer)x = x + zz = x * zEnd SubPublic Function FunB(ByVal y As Integer) As Integery = z + 1FunB = x + yEnd Function单击窗体后,窗体上输出的结果为:_。5. 程序Private Static Sub Command1_Click()Dim x As Integer, s As Integerx = Val(InputBox(“请输入一个正整数=“)If x = n1 f2 = f2 + n2: n2 = n2 - 1 Loop End Function Private Sub Command1_Click() Dim a As Integer, b As Integer Print f1(3) Print f1(5) a = 5: b = 8 Print f2(a, b) Print f2(b, a) End Sub 写出程序运行时,单击命令按钮控件 Command1 后窗体上的显示结果。19 程序Sub prnt(b() As String * 1, n As Integer) Dim i As Integer For i = 1 To n Print b(i); Next i Print End Sub Private Sub Form_Click() Dim a(7) As String * 1, i As Integer For i = 1 To 7 a(i) = Chr(Asc(“A“) + i - 1) Next i For i = 7 To 4 Step -1 Call prnt(a, i) Next i End Sub 写出程序运行时,单击窗体后窗体上的显示结果。 20程序Private Sub Command1_Click()Dim x As Long, i As Integerx = 60i = 2Do While x > 1If x Mod i = 0 Then Print i;: x = x i Else i = i + 1Loop End Sub 单击命令按钮,窗体上的显示结果为。21程序Sub f(a() As Single, n As Integer)Dim i As Integer, j As Integer, k As Integer, t As SingleFor i = 1 To n - 1k = iFor j = i + 1 To nIf Abs(a(j) > Abs(a(k) Then k = jNext jt = a(k): a(k) = a(i): a(i) = tNext iEnd SubPrivate Sub Command1_Click()Dim x(5) As Singlex(1) = 5: x(2) = -7: x(3) = 5: x(4) = 12: x(5) = -3Call f(x, 5)Print x(1); x(2); x(3); x(4); x(5)End Sub运行时单击命令按钮 Command1 后窗体上的显示结果是;如果将其中的语句“ t = a(k) : a(k) =a(i) : a(i)=t ”修改为“a(k) =a(i) : a(i) =a(k) ” ,则显示结果为 。 22程序Private Sub f(s As String)Dim i As Integer, n As Integer, c As Stringn = Len(s)If n Mod 2 = 1 Then c = Mid(s, n 2 + 1, 1)For i = 1 To Len(s) 2c = Mid(s, Len(s) + 1 - i, 1) + c + Mid(s, i, 1)Next is = cEnd SubPrivate Sub Command1_Click()Dim x As Stringx = InputBox(“x=“)Call f(x)Print xEnd Sub 单击 Command1 后输入”abc” ,窗体上显示结果为:单击 Command1 后输入”abcd” ,窗体上显示结果为:如果将 Sub 过程 f 首句改为 Private Sub f(ByVal s As String),单击后 Command1 后输入”abcd”,窗体上显示结果为:(二)程序填空,根据程序实现的功能,将程序填充完整。(二)程序填空,根据程序实现的功能,将程序填充完整。1【程序说明】:已知自然对数的底数 e 的级数表示如下:本程序利用函数过程 fact()求 e,其中绝对值小于 1E-8 的项被忽略。【程序】Private Function fact(m As Integer) As Single '求 m!的函数Dim x As Single, i As Integerx = 1For i = 1 To mx = Next ifact = xEnd FunctionPrivate Sub Form_Click()Dim e As Single, item As SingleDim n As Integere = 1: n = Don = n + 1item = e = e + itemLoop While Form1.Print “e=“; eLLLL!1 ! 31 ! 21 ! 111neEnd Sub2. 本程序求 3-100 之间的所有素数(质数)并统计个数;同时将这些素数从小到大依次写入顺序文件e:dataout.txt;素数的个数显示在窗体 Form1 上。【程序】Private Sub Command1_Click()Dim count As Integer, flag As BooleanDim t1 As Integer, t2 As Integercount = 0For t1 = 3 To 100flag = TrueFor t2 = 2 To Int(Sqr(t1)If Then flag = FalseNext t2count = count + 1End IfNext t1Close #1End Sub3.【程序说明】由输入对话框输入 n(设 n 为大于零且小于 30 的自然数),计算下列表达式的值,并在标签框 Label1上显示。【程序】Private Sub Form_Click()Dim n As Integer, sum As Double, k As Integern = Val(InputBox(“n=“, “请输入自然数 n(n>0 且 n = nLabel1.Caption = “sum=“ + Str(sum)End Sub4.【程序说明】) 1(1.431 321 211 nn本程序用于处理文本框 Text1.Text 中的内容,假设文本框中有偶数个字符。要求将文本框中的内容从头尾至中间依次各取字符,组成一个新的字符串 Str2,并在窗体上输出。例如: Text1.Text = “12345678“,则 Str2 = “18273645“【程序】Private Sub Form_Click()Dim Str1 As String, Str2 As StringStr1 = Text1.TextStr2 = “m = 0Do Str2 = Str2 + Str2 = Str2 + m = m + 1LoopForm1.Print Str2End Sub5 【程序说明】用近似公式求自然对数的底数 e 的值,要求计算结果的误差小于 10-9E=1+1/1! +1/2!+1/3!【程序】Private Sub Command1_Click()dim s as Double , t as double Dim i As Integeri = 2t = 1do while t = t + 1i = i + 1LoopPrint sEnd Sub6 【程序说明】下列过程的功能是为与形参 a 所对应的 n 个元素的实参数组送数,第一个元素赋值 n,其他元素为 2 位随机整数,要求这 n 个数各不相同。【程序】Private Sub creat ( a() as byte , n as byte)Dim i as byte , j as byte , x as bytea(1) = ni=1Do while i max Thenmax = Position = End IfNext iForm1.Print “最大值=“ one; “two=“; two; “five=“; fiveEnd If'* 错误 3 *Next one'* 错误 4 *Next twoForm1.Print “k=“; kEnd Sub4. MaxLine 过程用于查找一个 5 行 4 列的二维数组中行平均值最大的行,并将该行所有数据调整到第一行的位置。DataProduce 过程用于产生原始数据;Average 过程用于计算各行的平均值;PrintArray 过程用于打印二维数组和行