vb课件7.3剖析

上传人:今*** 文档编号:107692556 上传时间:2019-10-20 格式:PPT 页数:36 大小:290KB
返回 下载 相关 举报
vb课件7.3剖析_第1页
第1页 / 共36页
vb课件7.3剖析_第2页
第2页 / 共36页
vb课件7.3剖析_第3页
第3页 / 共36页
vb课件7.3剖析_第4页
第4页 / 共36页
vb课件7.3剖析_第5页
第5页 / 共36页
点击查看更多>>
资源描述

《vb课件7.3剖析》由会员分享,可在线阅读,更多相关《vb课件7.3剖析(36页珍藏版)》请在金锄头文库上搜索。

1、7.5 递归过程,1.递归的概念,在程序中调用一子过程,而在子过程中又调用另外的子过程,这种程序结构称为过程的嵌套。过程的嵌套调用执行过程如下图:,过程的嵌套调用,如果在嵌套调用的过程中调用的是过程自身,这种过程也叫递归过程. 递归是一种十分有用的程序设计技术。由于很多的数学模型和算法设计方法本来就是递归的。比如:求阶乘,求指数等. 用递归过程描述它们比用非递归方法 简洁易读,可理解性好, 算法的正确性证明也比较容易,2. 递归算法的结构,递归算法的表达式通常由一个递归式和一个初始(或边界)条件式组成。 例如: 求N!,1, 当n=0或n=1时 n! = n*(n-1)!,当n1时,递归式,初

2、始条件式,裴波拉契数列,1 n=1 F(n)= 1 n=2 F(n-2)+F(n-1) n3,3. (重点)递归算法的执行过程分析以阶乘为例,Private Function fact(ByVal n As Integer) As Long If n = 0 Or n = 1 Then fact = 1 Else fact = n * fact(n - 1) End If End Function,初始条件式,递归式,调用阶乘的程序,Private Sub Form_Click( ) Dim N as Integer, F as Long N = InputBox(“输入一个正整数”) F=f

3、act(N) Print N;”!=“;F End Sub,练一练:1 Private Function f (ByVal n As Integer) As Long If n = 0 Or n = 1 Then f = 1 Else f = f(n-2)+ f (n - 1) End If Print f End Function 主调程序中:xf(3)窗体打印几行,每行结果是什么?,1 1 1 2 3,Private Sub Command1_Click() Dim n As Integer n = 1 Call dg(n) End Sub Private Sub dg(n As Inte

4、ger) If n 3 Then Exit Sub Else dg (n + 1) End If print n End Sub,3 2 1,3,课堂练习:,题目:求数列前n项之和.数列表达式为,0 n=1 F(n)= 1 n=2 2F(n-1)-F(n-2) n3,Private Function f(n As Integer) As Long If n = 1 Then f = 0 ElseIf n = 2 Then f = 1 Else f = 2 * f(n - 1) - f(n - 2) End If End Function Private Sub Command1_Click()

5、 Dim n%, i%, s% n = Val(Text1.Text) For i = 1 To n s = s + f(i) Next i Text2.Text = Str(s) End Sub,5. 无限递归,Private Sub Command1_Click() Dim n As Integer n = 1 Call dg(n) End Sub Private Sub dg(n As Integer) call dg (n + 1) End Sub,Private Sub Command1_Click() Dim n As Integer n = 1 Call dg(n) End Su

6、b Private Sub dg(n As Integer) If n 10 Then Exit Sub Else dg (n + 1) End If End Sub,递归真题实例,Option Explicit Private Sub Form_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 End Sub,23 47,Private

7、Sub Command1_Click() fun 3 End Sub Public Function fun(m As Integer) As Integer Dim value As Integer If m = 0 Then value = 3 Else value = fun(m - 1) + 5 End If fun = value Print m, value End Function,0 3 8 13 3 18,7.6 变量的作用域,变量的作用域,“变量的作用域”指的是变量的“有效范围”或“作用范围”。只有在该范围内,变量才有意义。 根据说明变量的语句及说明位置,变量可分为: 过程

8、级变量(局部变量) 模块级变量 全局变量,7.6.1 过程级变量,在过程中声明的变量是过程级的变量。 Private Function Local_Variable(N As Integer) As Integer Dim X As Integer, Y As Integer, Z As Integer X = N * 3 Y = X + 4 Z = X + Y Local_Variable = X + Y Z End Function,程序每次调用此函数 ,vb都为局部变量x,y,z分配空间当函数运行结束,vb会释放分配给它们的空间,局部变量便不再存在了,因为局部变量只在声明它们的过程中才能

9、被访问或改变该变量的值,别的过程不可访问。所以可以在不同的过程中声明相同名字的局部变量而互不影响。,例: Private Sub Form_Load() Dim n% n= 10 End Sub Private Sub From_Click() Dim n% Print “n=“;n End Sub,程序运行后单击窗体,其输出结果是什么?,n 0,7.6.2 模块级变量,“模块级变量”是指在模块的通用部分用Dim或Private语句说明的变量.,Private s As String Dim a As Integer,其作用范围是整个模块,即模块内的所有过程 都可引用。,例: Dim n% P

10、rivate Sub Form_Load() n= 10 End Sub Private Sub From_Click() Print “n=“;n End Sub,程序运行后单击窗体,其输出结果是什么?,n= 10,Option Explicit Dim TestString As String Private Sub Form_ Activate () Debug.Print “在Form_Activate 事件过程中“; TestString Call ShowTestString End Sub Private Sub Form_Load() TestString = “测试变量作用域

11、“ End Sub Private Sub ShowTestString() Debug.Print “在过程ShowTestString 中“; TestString End Sub,7.6.3 全局变量,“全局变量”是指在窗体模块或标准模块的通用部分用Public语句说明的变量。全局变量在整个工程内有效。 标准模块中说明的全局变量,任何一个过程都可直接引用;引用其他窗体模块中说明的全局变量,则应使用 窗体名.变量名 的形式。,例子,教材P147 全局变量的缺点:容易造成错误,7.6.4 同名变量,1、不同作用域的变量可以同名。 2、引用在不同模块中说明的同名全局变量时,可通过在变量名前增加

12、来加以区分; 3、如果过程级变量与模块级或全局变量同名,在过程执行时,则使用过程级变量。(县官不如现管、天高皇帝远),Option Explicit Public X As Integer, Y As Integer, Z As Integer Private Sub Form_Activate() Conflict_X Debug.Print “X,Y 和 Z 是“, X, Y, Z End Sub Private Sub Form_Load() X = 10 : Y = 20 : Z = 35 End Sub Private Sub Conflict_X() Dim X As Intege

13、r X = 135 Debug.Print “X,Y 和 Z 是“, X, Y, Z End Sub,20 35 10 20 35,同名变量,Private Sub Form_Click() Dim a As Integer, b As Integer a = 1: b = 2 Call proc1(a, b) Print a, b End Sub Private Sub proc1(c As Integer, d As Integer) Dim a As Integer, b As Integer c = a + b: d = a - b Print c, d End Sub,0 0 0 0

14、,Dim a As Integer, b As Integer Private Sub Form_Click() a = 1: b = 2 Call proc1(a, b) Print a, b End Sub Private Sub proc1(c As Integer, d As Integer) c = a + b: d = a - b Print c, d End Sub,3 1 3 1,Dim x As Integer, y As Integer Private Sub Command1_Click() Dim a As Integer, b As Integer a = 5 b =

15、 3 Call sub1(a, b) Print a, b Print x, y End Sub,Public Function fun1(a As Integer, b As Integer) As Integer x = a + b y = a - b Print x, y fun1 = x + y End Function,Public Sub sub1(ByVal m As Integer, n As Integer) Dim x As Integer, y As Integer x = m + n: y = m n: m = fun1(x, y): n = fun1(y, x) En

16、d Sub,10 6 10 -6 5 4 10 -6,7.6.5 静态变量,在过程中,使用Static语句说明的变量称为“静态变量”。 静态变量在过程执行完后,其存储空间依然保留,并可继续保持其值。,例: 有一个人编了下面一段程序,想用变量n记录单击窗体的次数。 Private Sub Form_Click() Dim n As Integer n = n + 1 Print “已单击次数:“; n & “次“ End Sub,.分析程序运行后多次单击窗体的输出结果:,.要记录单击窗体次数,如何实现?,已单击次数1次,Static n as integer,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) D

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号