三、程序填空_共16题(选:10空*2’=20分)——完形填空:把题目中空的程序代码行补齐 第1题
'本程序的功能是把由4个\分隔的十进制数表示的IP地址转换为由32位二进制数组成 '的IP地址。 例如十进制表示的IP地址为202.119.191.1,其中每个十进制数对应一个8位 '的二进制数,合起来构成一个32位二进制的IP地址11001010011101111011111100000001。 '过程Tiqu用于提取十进制IP地址中每个用\分隔的十进制数;
'过程Convert用于将十进制数转换为相应的8位二进制数。程序界面参见下图。 Option Explicit
Private Sub Command1_Click()
Dim str1 As String, str2 As String Dim a(4) As Integer, i As Integer str1 = Text1.Text Call Tiqu(str1, a) For i = 1 To 4
If a(i) < 0 Or a(i) > 255 Then MsgBox(\地址错误!\ Exit Sub Else
'**********SPACE********** 【?】 End If Next i
Text2.Text = str2 End Sub
Private Sub Tiqu(st As String, a() As Integer)
Dim n As Integer, k As Integer, s As String, d As String * 1, i As Integer n = Len(st): k = 0: s = \ For i = 1 To n
'**********SPACE********** 【?】 If d = \ k = k + 1 a(k) = Val(s) '**********SPACE********** 【?】 Else
s = s & d End If Next i a(4) = s End Sub
Private Function convert(ByVal n As Integer) As String Dim b As Integer, i As Integer, s As String Do While n > 0 b = n Mod 2 n = n \\ 2
'**********SPACE********** 【?】 Loop
For i = 1 To 8 - Len(s) s = \ Next i
'**********SPACE********** 【?】 End Function
第2题
'下面程序的功能是:统计存放在数组A中的N个数有多少个是不同的。 '具体做法是:变量Left指向要被处理的数(从第2个元素开始),
'Right指向数组最后一个元素。若A(Left)与排在它前面的某个数组元素值相同, '就用数组元素A(Right)的值来替换A(Left)的值,同时将变量Right的值减1;
'否则将变量Left的值加1,处理数组下一个元素,重复以上过程,直到Left>Right为止。 'Right的值即为不同的数的个数。程序界面参见下图所示。 Option Explicit Option Base 1
Private Sub Command1_Click()
Dim A() As Integer, N As Integer, I As Integer Dim OP As Integer
N = InputBox(\请输入数据个数\ ReDim A(N) Randomize For I = 1 To N
A(I) = Int(Rnd * 10) + 1 Text1 = Text1 & A(I) & \ Next I
Call statistic(A, OP)
Text2 = \有 \个不同的数: \ For I = 1 To OP
Text2 = Text2 & A(I) & \ Next I End Sub
Private Sub statistic(A() As Integer, Right As Integer) Dim Left As Integer, K As Integer, I As Integer '**********SPACE********** Right=【?】 Left = 2
Do While Left <= Right '**********SPACE********** K= 【?】
For I = K To 1 Step -1
If A(Left) = A(I) Then Exit For Next I
'**********SPACE********** If 【?】 Then
Left = Left + 1 Else
A(Left) = A(Right) '**********SPACE********** 【?】 End If Loop End Sub
第3题
'下面程序的功能是随机生成小于100的诸素数组成的数据。程序界面如下图所示。 Option Explicit
Private Sub Command1_Click()
Dim p() As Integer,i As Integer,n As Integer Dim st As String,k As Integer Randomize
k =InputBox(\元素个数;\ ReDim p(k) Do
n =Int(Rnd*90)+10 '**********SPACE********** If 【?】 Then i =i+1 p(i) =n End If
'**********SPACE********** Loop Until【?】 For i =1 To UBound(p) st =st & Str(p(i))
If i Mod 10 =0 Then st =st & vbCrLf Next i Text1 =st End Sub
Private Function prime(n As Integer) As Boolean Dim i As Integer Prime =True For i =2 To Sqr(n) If n Mod i =0 Then '**********SPACE********** 【?】 Exit For End If Next i End Function
第4题
'用以下公式求f(x) 的值。当通项的绝对值小于10^-7时停止计算,x的值由键盘输入。
'程序界面和公式参考图片所示: Option Explicit
Private Sub Command1_Click() Dim x As Single, fx As Single
Dim a As Single, a1 As Single, a2 As Single Dim t As Single a1 = 1: a2 = 2 x = Text1
'**********SPACE********** If 【?】Then MsgBox (\必须在-1~1之间\ Exit Sub End If
fx = a1 * x - a2 * x * x t = (-1) * x * x Do
a = 1 / (a1 + a2) t = (-1) * t * x '**********SPACE********** fx = 【?】 a1 = a2
'**********SPACE********** 【?】 Loop Until Abs(a * t) < 0.0000001 Text2 = fx End Sub
第5题
'下面程序的功能是:首先生成一个由小到大已排好序的整数数组,再输入一个数据, '单击\插入\按钮会自动把这个数据插人到原数组适当的位置,并保持数组的有序性。 '程序界面参见下图。
Option Explicit Dim a( ) As Integer
Private Sub Form_Activate() '生成有序数组 Dim i As Integer ReDim a(10) For i=1 To 10 a(i)=(i-1)*10+1
Text1=Text1 & Str(a(i)) Next i
Text2.SetFocus
End Sub
Private Sub Command1_Click() Dim n As Integer,i As Integer n=Text2
For i=1 To UBound(a) '**********SPACE**********
If 【?】Then Exit For '确定插入的位置 Next i
'**********SPACE********** 【?】
For i=1 To UBound(a) Text3=Text3 & Str(a(i)) Next i End Sub
Private Sub inst(P() As Integer,n As Integer,k As Integer) '数组元素移位并实现插入 Dim i As Integer '**********SPACE********** 【?】
For i=UBound(P)-1 To k Step -1 '**********SPACE********** 【?】 Next i P(k)=n End Sub
第6题
'下面程序的功能是:将输入的2位16进制数的每一位转换为一个4位的二进制数, '再组合成为一个8位的二进制数。程序界面参见下图。 Option Explicit
Private Sub Command1_Click()
Dim h As String, h1 As String * 1,h2 As String * 1 h=Text1
h1=Left(h,1): h2=Right(h,1) Text2=h2b(h1) & h2b(h2) End Sub
Private Function h2b(p As String) As String Dim st As String, i As Integer st=″0123456789ABCDEF″ For i=1 To Len(st) '**********SPACE********** If 【?】 Then Exit For Next i i=i-1 Do
'**********SPACE********** h2b=【?】 i=i\\2
Loop Until i<=0