江苏省高校计算机等级考试命题研究院 江苏省高校计算机等级考试辅导
江苏省计算机等级考试二级vb过程讲解

8   过程

教学要求

掌握VB的两种过程:事件过程和通用过程

 掌握Sub过程的定义

 掌握Function过程的定义

 掌握过程的调用

 掌握参数的传递——按值传递和按地址传递

 掌握递归算法

 掌握变量的作用域

8.1   Sub过程
   8.1.1 事件过程

当对象识别某事件后,进行的操作处理——以代码的形式存储在事件过程中。

分类:

窗体事件过程

控件事件过程

1  定义事件过程

[形式]

Private Sub Form_事件名([参数列表]

      [局部变量和常数声明]

      语句块

End Sub

Initialize(初始化)事件——配置窗体时触发

Load(加载)事件——窗体装入内存时触发

Activate(激活)事件——窗体被激活时触发

Gotfocus事件(得到焦点)事件——窗体成为当前焦点时触发

窗体上无可视控件时,触发窗体的Gotfocus事件;

窗体上有可视控件时,触发控件的Gotfocus事件;

执行次序:

       Initialize          Load           Activate           Gotfocus

窗体的InitializeLoad事件发生在窗体被显示之前,其中可放置系统初始化命令,但其中语句有所限制;

窗体加载后,只要不被卸载,就不会再执行InitializeLoad事件,Activate事件会多次发生;

当访问另一窗体上的非可视数据或调用其中定义的全局过程时,只会触发该窗体的Initialize事件,Load事件不触发;

当访问另一窗体上的可视数据时,会同时触发该窗体的InitializeLoad事件;

3  定义控件的事件过程

[一般形式]

Private Sub 控件名_事件名([参数列表]

        [局部变量和常数声明]

        语句块

End Sub

8.1.2  通用过程

完成某一特定功能的程序段

                    ——通用过程(自定义)

必须显式调用方可执行;

分为:

公有过程(Public

私有过程(Private

1  通用Sub过程的定义

[一般形式]

  [Private|Public] [static] Sub <过程名>([<参数列表>])

         <过程体>

   End Sub

[说明]

(1) Sub开头,End Sub结束,,中间是过程体——包括变量声明和语句块;

(2) Private为前缀的过程是模块级的,Public为前缀的过程是应用程序级的,缺省默认是Public

(3) Static 选项说明过程中的局部变量是静态变量;

(4) 过程名的命名规则和变量名相同,在同一个模块中,过程名必须是唯一的;

5  参数列表中的参数称为形式参数,可以没有,但无参数时圆括号不能省略;有多个参数时,参数之间用逗号间隔;

6)参数说明格式:

    [Optional][ByVal|ByRef] <变量名>[()][As <数据类型>]

    其中:

    a) 若参数是数组,则在变量名后面加一对圆括号,但无维界定义

b) ByVal:指明参数传递方式是传值;

c) ByRef:指明参数传递方式是传地址,为缺省值;

d) 若参数是字符型的,必须是不定长字符串;

e)Optional:参数是可选的,必须定义在必选参数后面。

7 当过程调用结束,即执行到End Sub语句,系统自动返回调用程序的调用语句处,执行调用语句的下一条语句;

 8 过程不能嵌套定义,但可嵌套调用;

 9Exit Sub语句的功能是提前退出过程调用,返回调用语句。

:      Private  Sub  Exchange(x As Integer,y As Integer)

                     Dim Temp As Integer

                     Temp=x : x=y : y=Temp

            End Sub

该通用过程实现交换功能,包含两个参数,均是ByRef形式的参数。

3  Sub过程调用

      必须在事件过程或其它通用过程中显示调用。

Sub过程调用

[格式一]  Call  <过程名>[<实在参数表>]

[格式二]  <过程名>  [<实在参数表>]

[功能]:对已定义的过程进行调用。

如:

Call  Fact(x)

   Fact  x

1)调用的过程必须是已经定义的,否则系统会出现“子程序或函数未定义”的信息提示;

2)实在参数可以是常量、变量或表达式;

3)实在参数的数目及类型要和定义时必选参数保持一致,否则系统会出现“参数不可选”的信息提示,参数之间用逗号间隔;

4)若子程序没有参数,则格式一中的括号可以省略;

5)格式一和格式二的区别在于:格式二的参数表无须括号,而是和过程名之间用空格隔开;

8-2  试编写一个找出任意一个正整数的因子的程序

Option Explicit

Private Sub Command1_Click()

Dim inta As Integer, st As String

 inta = Text1

 Call factor(inta, st)

 Text2 = st

End Sub

Private Sub factor(ByVal n As Integer, s As String)

 Dim i As Integer

 For i = 1 To n - 1

        If n Mod i = 0 Then s = s & Str(i)

  Next i

End Sub

优点:程序即Sub过程的使用不仅可以缩短程序的长度,还能够使程序的结构更加清楚。

:定义一个通用过程用以求一维数组中的最小值

Private Sub value(a() As Integer, min As Integer)

    Dim i As Integer

    min = a(1)

    For i = 2 To UBound(a)

         If a(i) < min Then min = a(i)

    Next i

End Sub

8 . 2   Function 过程

Function过程的特点是返回一个值,因此我们通常也称之为自定义函数。通常我们利用Function过程得到一个数值或一个字符串或一个逻辑值。

 [格式]

 [Private|Public] Function <函数名>([参数列表]) [As <数据类型>]

                  <函数体>

 End Function

1 Function开头,End Function结束,中间是函数体;

2)函数名命名规则、参数列表的表示都和Sub过程相同;

3As 数据类型:函数过程将由函数名返回一个值,值的类型由[As 数据类型]定义;

4 函数体中一定要有对函数名赋值的语句——函数名=表达式,否则返回相应类型的初值;

5)函数体内可有Exit Function语句——无条件退出函数过程,返回主程序。

6Function过程不能嵌套定义,但可嵌套调用。

例:定义函数:

Private  Function f(ByVal x As Integer) As Integer

          f = 3*x^3-2*x^2+6*x-1

End Function

8-1:编写一个求n!的函数。

Private  Function Fact(Byval n As Integer) As Long

    Dim K As Integer

    Fact=1

    If  n=0 Or n=1 Then

               Exit Function

    Else

               For K=1 To N

                     Fact=Fact*K

              Next K

    End If

End Function

2  调用 Function 过程

[格式]<函数名>[实在参数表]

[功能]:返回一个函数值。

[说明]

1)一般情况下,函数的调用出现在赋值语句中,并且在赋值号的右侧;

2)若函数没有参数,函数名后的括号不能省略;

3)虽然VB允许象调用Sub过程那样调用Function过程,但这样调用时系统不返回函数值,建议大家不要使用这种调用方法。

Call  fact(x) 

Fact   x      这两种方法均放弃函数的返回值

:用函数实现求一维数组中的最小值,对比前例(SUB)

Private Function min( a( ) As Integer ) As Integer

    Dim i As Integer

    min = a(1)

    For i = 2 To UBound(a)

         If a(i) < min Then min = a(i)

    Next i

End Sub

:定义函数用以判断一个数是否是素数

Private Function Prime(N As Integer) As Boolean

Dim i As Integer

Prime = False        

For i = 2 To N-1

        If N Mod i = 0 Then Exit For

Next i  

If i =N then  Prime = True

End Function

[]S=1!+2!+…+10!

Private Sub Form_Click()

    Dim S As Long, i As Integer

    For i = 1 To 10

          

    Next i

    Print ”S="; S

End Sub

Private Function Fact(n As Integer) As Long

   Dim i As Integer

   Fact = 1

   For i = 1 To n

           Fact = Fact * i

   Next i

End Function

8-3:利用函数过程编写一个求两个正整数的最大公约数的程序

Private Sub Form_Click( )          主调过程

    Dim N As Integer, M As Integer, G As Integer

    N = InputBox("输入N")

    M = InputBox("输入M")

    G = Gcd(N, M)

    Print N; ""; M; "的最大公约数是:"; G

End Sub

Private Function Gcd(ByVal A As Integer, ByVal B As Integer) As Integer

    Dim R As Integer

    R = A Mod B

    Do While R <> 0

        A = B

        B = R

        R = A Mod B

    Loop

    Gcd = B

End Function

2、调用标准模块中的公有过程

[格式]Call   [<标准模块名>.]<过程名>[<实参表>]

[功能]:调用其他标准模块中定义的公有过程。

[说明]

若公有过程唯一,则直接调用,不加模块名。

若存在同名的公有过程,则:

调用本模块中过程:直接调用,不加模块名

调用其它模块中过程:必须加模块名。

被调用的函数和过程必须是公有的;

函数也可以这样调用。

8.4  参数的传递

8.4.1 形式参数和实在参数

形式参数:

 过程定义时,在过程名后面的圆括号里的一系列变量;

 过程被调用执行时,系统才给形参分配存储空间;

 可以是除定长字符串外的任一简单变量;

 可以是数组,变量名后接括号;

 简称“形参”

实在参数

 主调程序中,调用语句中,出现在过程名后面圆括号里的变量,是实在参数,可以是常量、变量或表达式;

  过程调用传递参数时,实参按位置和形参结合;

  实在参数和形式参数,要求个数一样,位置对应,类型一致,否则会出错;

  定长字符串变量可以作为实在参数;

 简称“实参”

举例:写出下列程序执行的结果

Private   Sub   ExamSub( x as integer,y as integer)

       x=x+10 :  y=y-10

End Sub

Private Sub Form-click()

       Dim x as integer,  y as  integer

       x=10:y=100

       Call  ExamSub( y ,x )

       Print  “x=” ; x ;  “y=” ; y

End Sub

8.4.2  按值传递参数

[格式]ByVal  <变量说明>

[传递方式 ]

        调用时,系统为形参分配一个临时存储单元,并将实参的值存储到该临时单元中。

       若在被调用的过程体中改变了形参的值,只是改变了临时存储单元中的数据,对实参的值无任何影响。

[参数传值传递]举例:

Private Sub Form_Click()

    Dim M As Integer, N As Integer

    M = 15: N = 20

    Call Value_change(M, N)

    Print "M="; M, "N="; N

End Sub

Private Sub Value_change(ByVal x As Integer, ByVal y As Integer)

    x = x + 20

    y = y + 20

    Print "X="; x, "Y="; y

End Sub

传值是单向的:

调用时,实参将值传递给形参,两者就无任何关联。过程中形参的值发生变化,对实参无任何影响。

8.4.3 按地址传递参数

[格式] ByRef  <变量说明>

[传递方式]

        系统在调用执行过程时,为形参分配临时存储单元,并将实参的内存单元地址传送给形参,存储在临时存储单元中

        在被调用的过程体中一切对形参的操作,都是直接对地址保存在形参中的内存单元中的数据进行的,而实参就是保存在此内存单元中的数据,所以任何对形参的操作也就是对实参的操作;

        按地址传递时,形式参数和实在参数共用同一内存单元

[参数传址传递]举例:(对比前例)

Private Sub Form_Click()

      Dim M As Integer, N As Integer

      M = 15: N = 20

      Call Value(M, N)

      Print "M="; M, "N="; N

End Sub

Private Sub Value(x As Integer, y As Integer)

      x = x + 20

      y = y + 20

      Print "X="; x, "Y="; y

End Sub

传地址是双向的:

举例:计算5+4+3+2+1

Private Sub Form_Click()

    Dim Sum As Integer, I As Integer

    For I = 5 To 1 Step -1

        Sum = Sum + Fact(I)

    Next I

    Print "Sum="; Sum

End Sub

Private Function Fact(n As Integer) As Integer

    Fact = 1

    Do While n > 0

         Fact = Fact * n

         n = n - 1

    Loop

End Function

将传地址修改为传值的方式为:

方法一:在形参n前加ByVal

方法二:将调用语句中fact(i)改为fact( ( i ) )

参数有两种传递方式:传值(ByVal)和传地址(ByRef)。

        若调用时实参为常量或表达式,这两种方式没有区别,无论形参定义的是按值传递还是按地址传递,系统都是按传值方式传递

       系统在调用时为形参分配一个临时存储单元,将表达式的值计算出来,存储到该临时单元;调用结束,系统收回临时单元。

        调用函数或过程时,将单个变量转换成表达式的方法:将变量放在一对括号中。如用Fact((I))的方式调用函数,系统会按传值来处理。

按地址传递时,当实参是变量时,实参与形参必须类型完全一致;

        按地址传递时,当实参是常量或表达式时,VB会自动进行类型转换,然后再传递相应的值(即类型无须完全一致);

        在算术表达式中,函数的优先级最高,若表达式中有函数的实参,而函数的参数又是按地址传递的,则函数中有可能改变了实参的值,即改变了表达式中变量的值,会引起混淆。

Private Sub Form_Click()

     Dim S As Single

     S=125.5

     Call Convert((S),”12”+”.5”)

End Sub

Private Sub Convert(Inx As Integer,Sing As Single)

      Inx=Inx*2

      Sing=Sing+23

      Print “Inx=”;Inx,”Sing=”;Sing

End Sub

8.4.4 数组参数

       VB允许把数组作为形式参数,声明数组的格式是:

                         <数组名>() As   <数据类型>

[注意]

1)数组参数只能按地址传递,即不能用ByVal来修饰数组参数

2)定义数组参数时无须说明数组的维数和下标变化范围;

3)调用过程时,对应的实在参数也必须是数组,但只需要数组名,无须后跟括号,且数据类型也要一致。`

4)在过程体或函数体中无须对数组参数再次说明;

5)若实参是动态数组,在过程体或函数体中可以使用重定义语句修改数组的维界;

举例

Private Sub Form_Click()

    Dim a() As Integer, i As Integer

    Dim n As Integer

    n = InputBox("请输入数组的初始大小")

    ReDim a(n)

    For i = 1 To n

           a(i) = i

    Next i

    Call PrintArray(a)

    Call Array1(a)

    Call PrintArray(a)

End Sub

Private Sub PrintArray(x() As Integer)

   Dim i As Integer

   For i = LBound(x) To UBound(x)

             Print x(i);

    Next i

    Print

End Sub

Private Sub Array1(a() As Integer)

    Dim m As Integer, n As Integer

    n = UBound(a)

    m = InputBox("请输入新大小")

    ReDim Preserve a(m)

    Dim i As Integer

    For i = n + 1 To m

            a(i) = 0

    Next i

End Sub

输入58,程序执行结果为:

举例:自定义一个将一维数组按从小到大排序的通用过程

Private Sub Command1_Click()

    Dim i As Integer, j As Integer

    Dim a(10) As Integer

    For i = 1 To 10

        a(i) = Int(8 * Rnd)

        Picture1.Print a(i);

    Next i

    Call sort(a)

     For i = 1 To 10

       Picture2.Print a(i);

    Next i

End Sub

Private Sub sort(a() As Integer)

    Dim i As Integer, j As Integer

    For i = 1 To UBound(a) - 1

        For j = i + 1 To UBound(a)

            If a(i) > a(j) Then

                   temp = a(i) 

                   a(i) = a(j)   

                   a(j) = temp

            End If

        Next j

    Next i

End Sub

本程序的功能是找出100~200之间的所有素数。

Private Sub Form_Click()

    Dim i As Integer, x As Integer

    For i = 100 To 200

             If   prime(i)        Then Print i

    Next i

End Sub

Private Function prime(   byval n  as integer      ) As Boolean

    Dim i As Integer

    For  i = 2 To Sqr(n)

            If n Mod i = 0 Then

    Next  i

    prime = True

End Function

8.5 递归过程

        所谓递归,就是在过程定义中,调用过程本身。

例:使用递归函数求N!  [提示]N!=N*(N-1)!

Private Sub Form_Click()

    Dim n As Integer

    n = InputBox("输入一个正整数")

    Print n; "!="; fact(n)

End Sub

Private Function fact(ByVal n As Integer) As Long

    If  n = 1 Then

          fact = 1

    Else

           fact = n * fact(n - 1)

    End If

End Function

总结:

         使用递归算法,最重要的是要有一个结束递归的条件,可以使递归得以返回,即终止条件或边界条件。

例:使用递归算法计算裴波拉挈数列。

Private Sub Form_Click()

    Dim n As Integer

    n = InputBox("求数列的第几项")

    Print “数列中第”; n; “项是”;   Cal(n)

End Sub

Private Function Cal(n As Integer)

    If n = 1 Or n = 2 Then

            Cal = 1

    Else

            Cal = Cal(n - 1) + Cal(n - 2)

    End If

End Function

递推法利用未知项与已知项之间存在的某种关系,从已知项逐项推出未知项的方法。

Private Sub Form_Click()

     Dim fb() As Integer

     Dim i As Integer,, n as integer

     n = InputBox(“求数列中第几项的值”)

     Redim fb(n)

     fb(1)=1:fb(2)=1

     For i = 3 To n

          fb(i)=fb(I-1)+fb(I-2)       

     Next i

     Print "数列中的第"; n; "项是"; fb(n)

End Sub

[8-9]编写一个递归函数,求任意两个整数的最大公约数。

Private Sub Command1_Click()

    Dim Gcdvalue As Long,  M As Long, N As Long

    If M < > 0 And N < > 0 Then

            Gcdvalue = Gcd(M, N)

            Text3.Text = CStr(Gcdvalue)

    End If     

End Sub

Private Function Gcd(ByVal X As Long, ByVal Y As Long)

    Dim R As Long

    R = X Mod Y

    If R = 0 Then

          Gcd = Y

    Else

           X = Y Y = R  Gcd = Gcd(X, Y)

    End If

End Function

 

写出下列程序运行的结果:

Private Sub Command1_Click()

       Dim a As Integer

       a = 2

       Call sub1(a)

End Sub

Private Sub sub1(x As Integer)

       x = x * 2 + 1

       If x < 10 Then

             Call sub1(x)

       End If

       x = x * 2 + 1

       Print “x=”;x

End Sub

Private Sub Command1_Click()

       Dim a As Integer

       a = 2

       Call sub1(a)

End Sub

Private Sub sub1(x As Integer)

       x = x * 2 + 1

       If x < 10 Then

             Call sub1(x)

       End If

       x = x * 2 + 1

       Print “x=”;x

End Sub

8.6 变量的作用域

8.6.1 过程级变量

          在一个过程内部声明的变量,只在过程内部有效,又称为局部变量;

例:

Private Sub Command1_Click()

    Dim i As Integer

    i = i + 1

    Print i

End Sub

Private Sub Command2_Click()

    i = i - 1

    Print i

End Sub

过程级变量,过程开始执行时,系统分配给其存储单元,而一旦过程运行结束,系统就收回存储空间。在其它过程中,不可访问。

8.6.2  模块级变量

        在模块的通用声明段中用PrivateDim声明的变量,模块中任何一个过程都可以访问;

Dim i As integer

Private Sub Command1_Click()

            i = i + 1

    Print i

End Sub

Private Sub Command2_Click()

    i = i - 1

    Print i

End Sub

模块级变量,在模块加载时,即分配给其内存存储单元,模块中任意过程均可对该变量进行操作,直到模块卸载时,系统才收回存储空间。但在其它模块中,不可访问。

8.6.4  关于同名变量

        当变量的作用域不同时,允许变量的名称相同。

 例如:

Dim i As integer

Private Sub Command1_Click()

    i = i + 1

    Print i

End Sub

Private Sub Command2_Click()

    Dim i As integer

    i = i + 10

    Print i

End Sub

说明:当变量名称相同而作用域不同时,优先访问局限性大的变量,即作用范围小的变量屏蔽作用范围大的变量。

Option explicit

Public x as integer,y as integer,z as integer

 

Private sub form_activate()

    Conflict_x

    Debug.print “X,YZ是:”,x,y,z

End sub

Private sub  form_load()

    X=10 : y=20 : z=35

End sub

Private sub  conflict_x()

   Dim x as integer

   x=135

   Debug.print “X,YZ是:”,x,y,z  

End sub

8.6.5  静态变量

  在过程内的变量定义时,Static 关键字定义的变量。

  所谓静态变量,是指仅在过程第一次执行时,分配给该变量内存单元,到过程执行结束时,并不象其它局部变量一样收回存储空间,系统并不收回分配给静态变量的存储空间,其内的内容得以保留,当再次调用执行该过程时,静态变量中保留了上次运行时的数据,本次执行可直接访问。

  静态变量只在过程内有效,即是局部变量

Private Sub Command1_Click()

    Static   i  As integer

    i = i + 1

    Print i

End Sub

举例:

Option explicit

Private sub command1_click()

    Dim k as integer

    K=5

   Call static-variable(k)

   Debug.print “第一次调用:k=”;k

   K=5

   Call static-variable(k)

   Debug.print “第二次调用:k=”;k

End sub

Private sub static_variable(n as integer)

    Static  sta as integer

    Sta=n+sta

    N=sta+n

End sub

说明:    

 因二次过程调用时,STA的初值不同,所以虽然实参一样,但运行结果不同。

 

Dim a As Integer, b As Integer               模块级变量

Private Sub Form_click()

    Dim x As Integer, y As Integer

    a = 5: b = 3

    x = x + a: y = y + b

    Print fun1(x, y)

    Print fun1(x, y)

End Sub

Private Function fun1(m As Integer, n As Integer) As Integer

    Static k As Integer                                   静态变量

    Dim a As Integer, b As Integer               同名的过程级变量

    a = a + m

    b = b - n

    k = k + a + b

    fun1 = k

End Function

写程序运行结果

 

上课习题讲解

1执行下面的程序,单击Commandl,则窗体上第一行显示的是  (______)  ,第二行显示的是(______________) 

    Option Explicit

    Dim X As Integer

    Private Sub Commandl_Click()

       Dim Y  as  Integer

       X=10Y=2

       Call process(Y)

       Print X,Y

       Call process((Y))

       Print X,Y

    End Sub

    Private Sub process(n As Integer)

       Dim Y As Integer

       If n>0 Then

         X=X-n

         Y=X

       Else

         X=X+n

         Y=X+2  

       End If

       n=-n

    End Sub

2.执行下面的程序,连续三次单击命令按钮Commandl之后,A数组共有  (________)  个元素;数组元素A(2)的值是(____________)  ,A(4)的值是.(_______________) 

    Option Explicit

    Option Base 1

    Private Sub Commandl_Click()   

        Static A()As Integer,n As Integer

        Dim i As Integer,k As Integer

        k=n   

        n=n +2

        ReDim  Preserve  A(n)

        For i=k+1 To  n   

A(i)=i*n+1

        Next i

        For i=1 To n

           Print A(i)

        Next i

        Print

    End Sub

3.执行下面的程序,单击Commandl,在窗体界面上显示的第一行是  (___________) ,第二行是 (___________)  ,第三行是(_______________)  ,第四行是(____________) 

    Option Explicit

    Private Sub Commandl_Click()

         Dim a As Integer,b As Integer,i As Integer

         i=1218

         a=i\100

         b=i Mod 100

         If b<>0 Then

            Print a

            Print b

          Print Lcd((a),(b))ab

            Print Lcd(a,b)ab

        End If

    End Sub

    Private Function Lcd(x As Integer,Y As Integer)As Integer

         Dim d As Integer   

         If x<Y Then

             d=xx = YY = d

         End If

         d=X

         Do

            If x Mod Y=0 Then

              Lcd=x

              Exit Do

            Else

             X=x+d

            End If

         Loop

End Function

4.本程序的功能是利用无穷级数求cos(x)的近似值,已知:

当第n项的绝对值小于等于10^-7时计算终止。

    Option Explicit

    Private Sub Commandl_Click()

       Dim X As Single,n As Integer,sum As Single

       Dim a As Single

       x=Textl   

        (  ______________  )

       a=1

       n=1

       Do

         a=-a

         a=(   _________________   )

         sum=sum+a

         n=n+1

       Loop Until(  __________________ )

       Text2=sum

    End Sub

7.定义方阵的一种范数为该方阵各列元素的绝对值之和中的最大值。以下程序的功能

是求一个4×4方阵的范数。该方阵的数据是随机生成的-2020之间的整数。

    Option Explicit

    Option Base 1

    Private Sub Commandl_Click()

      Dim a(4,4) As Integer

      Dim i As Integer,j As Integer

      For i=1 T0 4

        For j=1 To 4

          a(i,j)=  ( _________ )

          PicturelPrint Right(" "&Str(a(i,j)),4)

        Next j

        PicturelPrint

     Next i

     Textl.Text=  (______________ )

    End Sub

    Private Function fan(a()As Integer)As Integer

         Dim b(4) As Integer,max As Integer

         Dim i As Integer,j As Integer

         For i=1 To  4

For j=1 To 4

                 b(j)=(    ______________  )

              Next J

         Next i

    max=b(1)

         For i=2 To 4

           If max<b(i) Then max=b(i)

         Next i

         fan=max

    End Function  

8.下面程序的功能是:找出仅由数字1234组成的4位素数,要求每个素数由4个不 同数字组成。算法提示:函数Validate用于验证一个4位数是否由4个不同数字组成。在函 数中用A数组的各个元素分别对应数字09,只要某数字出现在四位数中,无论几次,均将该数字对应的数组元素值置为1

Option Explicit

   Private Sub Commandl_Click()

      Dim i As Integer,Flg As Boolean

      For i=1234 To 4321

          (  ________________ )

          Call Prime(i,Flg)

          If Flg Then

             If Validate(i)Then

                 Textl=Textl & i & vbCrLf

          End If

          End If

      Next i

    End Sub

    Private Sub Prime(n As Integer,f As Boolean)

      Dim k As Integer

      For k=2 To Sqr(n)

          (   __________________   )

      Next k

      f=True

    End Sub

    Private Function Validate(n As Integer)As Boolean

       Dim A(0 To 9)As Integer,s As String,i As Integer

       Dim sl As String*1

     (   ____________________   )

    For i=1 To Len(s)

       sl=Mid(s,i,1)

      (    ____________________   )

    Next i

      If A(1)+A(2)+A(3)+A(4)=4 Then

       (   ________________________   )

      End If

    End Function

    9.下面程序的功能是:首先生成一个由小到大已排好序的整数数组,再输入一个数据,单击“插入”按钮会自动把这个数据插人到原数组适当的位置,并保持数组的有序性。

  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

      Textl=Textl & Str(a(i))

    Next i

    Text2.SetFocus

End Sub

Private Sub Commandl_Click()

  Dim n As Integer,i As Integer

  n=Text2

  For i=1 To UBound(a)

    If (   __________________   )Then Exit For

  Next i

  (   ______________________   )

    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

    (   ____________________   )

    For i=UBound(P)-l To k Step -1

    (   ____________________   )

    Next i

    P(k)=n

End Sub

 

课后习题:

1.运行下面的程序,单击commandl,在窗体上显示的变量A的值为( _____ ),变量B的值为( _______ )

  Option Explicit

  Dim A As Integer   

  Private Sub Command1_Click()   

    Dim B As Integer

    B=3   

    A=4

    B=Fun(Fun(A,B+2),A)+B

    Print A,B

  End Sub

  Private Function Fun(N As Integer,K As Integer)  

    N=N+A  

    K=N-K

    Fun=N+K+A

  End Function

2执行下面的程序,单击command1,窗体上显示的第一行是(_____ ),第二行是( _____ ),第四行是( _______ ),最后一行是( ________ )

    Option Explicit

    Private Sub Command1_Click()

     Dim n As Integer   

     n=5   

     Call test(n)   

     Print n

    End Sub   

    Private Sub test(ByVal n As Integer)

      Dim i As Integer,S As String

      If n>0 Then   

        For i=1 To n   

          S=S & CStr(i)

        Next i

        Print S   

        Call test(n-2)   

     Else

       Print "OVER"

     End If

    End Sub

  3.运行下列程序,单击Command1,在窗体上显示的第一行内容是 (______  ),第二行的内容是(________ ),第三行的内容是( ________ )("A"ASCII码是65,"z"90)

    Option Explicit

    Private Sub Command1_Click()

      Dim i As Integer, st As String, n As Integer, p As String * 1

      p = "B"

    For  i = 3 To 1 Step -1

        n = Asc(p) - i

        If n < 65 Then n = n + 26

       st = Chr(n)

       st = st & F(i)

       Print st

    Next i

    End Sub

    Private Function F(n As Integer)

       Dim i As Integer

       Static S As Integer

       For i = 1 To n

        S = S + i

       Next i

       F = S

    End Function

 6•用以下公式求f(x)的值。当通项的绝对值小于10^-7时停止计算,x的值由键盘输入。

    f(x)=a1x1-a2x2+a3x3-+(-1)n+1anxn+  |X|<l其中  a1=1,a2=2,an= ,n=3,4,5…

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=la2=2

  x=text1

  If  (______________ ) Then

    MsgBox("x必须在-1---1之间")

    ExitSub

  End If

  fx=a1*x-a2*x*x

    t=(-1)*x*x

  DO

    a=1/(al+a2)

    t=(-1)*x*x

    fx=(________________ )

    al=a2

    ( ________________ )

  Loop Until Abs(a*t)<0.0000001

  Text2=fx

 End Sub

7.学生的某次课程测验中,选择题的答案已记录在列表框list1,其数据行格式是:学号为6个字符长度,2个空格,选择题的答案为15个字符长度,程序根据标准答案进行批改,每答对一题给1,并将得分存放到列表框list1,标准答案存放在变量Exact中。

Option Explicit

Private Sub Command1_click()

  Dim  Anw as string , StudId as string

  Dim Scor as Integer, Exact as string

  Dim I as Integer, J as integer

  Exact=”ABCCBAACBBDCCDA”

  For I=(   ____________   )

Anw=  _______________ 

StudId=Left(Anw,6)

Anw=Right(Anw,Len(Anw)-8)

(  _______________  )

For J=1 to  Len(Anw)

   If  ________________ Then

      Scor=Scor+1

   End If

   Next J

   List2.AddItem StudId & “  “ & Scor

 Next I

End Sub

8.下面程序的功能是:统计存放在数组A中的N个数有多少个是不同的。具体做法是:变量Left指向要被处理的数(从第2个元素开始),Right指向数组最后一个元素。若A(Left)与排在它前面的某个数组元素值相同,就用数组元素A(Right)的值来替换A(Left)的值,同时将变量Right的值减1;否则将变量Left的值加1,处理数组下一个元素,重复以上过程,直到Left>Right为止。Right的值即为不同的数的个数。

 Option Explicit

 OptionBase 1  

 Private Sub Commandl_Click()

Dim A() As Integer,N As Integer,I AS Integer

Dim OP As Integer

N=InputBox("请输入数据个数", ,10)

ReDim A(N)   

Randomize

For I=1 To N   

   A(I)=Int(Rnd*10)+1

   Text1=Text1 & A(1) & " "

Next I

Call Statistic(A,OP)

Text2=""  & oP & "个不同的数:"

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

    Right=( __________ )

    Left=2

    Do While Left<=Right

       K=( ___________ )

       For I=K To 1 Step -1

         If A(Left)=A(I) Then Exit For

       Next I

       If ( _______________ )Then

         Left=Left+1

     Else

         A(Left)=A(Right)

        ( ______________ )

       End If

   Loop

End Sub

9.在计算机中用一个字节(8),来存储一个字符的Ascii,其中低7位二进制数对应字符的编码,每个字节的最高位一般保持为0”,在数据传输时可用作奇偶校验位,传输时Ascii代码转换成传输码的方法是:7Ascii码有偶数个1,最高位为1,有奇数个1,则最高位为0,例如字母”A”Ascii码十进制表示为65,二进制表示为00100001”A”的传输码则为”1010 0001”,本程序的功能就是把Ascii码转换成这种传输码

Option Explicit

Private Sub Command1_Click()

   Dim i As Integer,S As String

   Dim str As String,ch As String

   str=Text1.text

   For i=1 To Len(str)   

    ( __________________ )

    Call convert(ch,s)

    List1.AddItem ch & "==>" & S

  Next i

End Sub

Private Sub convert(ch As String,S As String)

  Dim m As Integer,k As Integer,n As Integer,iAs Integer

  s= ( _______________ )

  n=Asc(ch)

  Do While n>0

    (  ____________  )

    S=m & S

    If m=1 Then

       k=k+1

    End If

    n=n\2

    Loop

    For i=1 To 7-Len(s)  '将字符的二进制代码补足7

    S="0" & S

    Next i

    If k Mod 2=0 Then

         ( __________________ )

    Else

      S="0" & S

    End If

  End Sub