7.3 递归与变量作用域

上传人:豆浆 文档编号:52179477 上传时间:2018-08-18 格式:PPT 页数:40 大小:364.50KB
返回 下载 相关 举报
7.3 递归与变量作用域_第1页
第1页 / 共40页
7.3 递归与变量作用域_第2页
第2页 / 共40页
7.3 递归与变量作用域_第3页
第3页 / 共40页
7.3 递归与变量作用域_第4页
第4页 / 共40页
7.3 递归与变量作用域_第5页
第5页 / 共40页
点击查看更多>>
资源描述

《7.3 递归与变量作用域》由会员分享,可在线阅读,更多相关《7.3 递归与变量作用域(40页珍藏版)》请在金锄头文库上搜索。

1、上节课复习知识要点1、过程的调用 sub过程调用 function过程调用 调用其他模块中的过程 2、参数的传递 形参与实参 按值传递参数 按地址传递参数 数组参数 对象参数7.5 递归过程(难点)1.递归的概念 在程序中调用一子过程,而在子过程中又调用另外的子过程,这种程序结构称为过程的嵌套。过程的嵌套调用执 行过程如下图:过程的嵌套调用如果在嵌套调用的过程中调用的是过程自身,这种过程也叫递归过程。递归是一种十分有用的程序设计技术。由于很多的数学模型和算法设计方法本来就是递归的。比如:求阶乘,求指数等。用递归过程描述它们比用非递归方法简洁易读,可理解性好,算法的正确性证明也比较容易 2. 递

2、归算法的结构递归算法的表达式通常由一个递归式和一个初始(或 边界)条件式组成。例如:求N! 1, 当n=0或n=1时 n! = n*(n-1)!,当n1时递归式初始条件式裴波拉契数列 1 n=1 F(n)= 1 n=2 F(n-2)+F(n-1)n33. (重点)递归算法的执行过程分析以阶乘为例Private Function fact(ByVal n As Integer) As LongIf n = 0 Or n = 1 Thenfact = 1Elsefact = n * fact(n - 1)End If End Function初始条件式递归式调用阶乘的程序Private Sub F

3、orm_Click( )Dim N as Integer, F as LongN = InputBox(“输入一个正整数”)F=fact(N)Print N;”!=“;FEnd SubFact(3)If N = 1thenFact = 1ElseFact = 3 * Fact(3- 1)End If End Function Fact(2)If N = 1thenFact = 1ElseFact = 2 * Fact(2-1)End If End Function Fact(1)If N = 1thenFact = 1ElseFact =1 * Fact(1-1)End If End Func

4、tion Fact(3)Fact(2)Fact(1)练一练:1 Private Function f (ByVal n As Integer) As LongIf n = 0 Or n = 1 Thenf = 1Elsef = f(n-2)+ f (n - 1)End IfPrint f End Function主调程序中:xf(3)窗体打印几行,每行结果是什么 ?1 1 1 2 3Private Sub Command1_Click()Dim n As Integern = 1Call dg(n) End Sub Private Sub dg(n As Integer)If n 3 Then

5、Exit SubElsedg (n + 1)End Ifprint n End Sub321练一练:2课堂练习: 题目:求数列前n项之和。数列表达式为 0 n=1 F(n)=1 n=2 2F(n-1)-F(n-2) n3Private Function f(n As Integer) As LongIf n = 1 Thenf = 0 ElseIf n = 2 Thenf = 1 Elsef = 2 * f(n - 1) - f(n - 2) End If End Function Private Sub Command1_Click()Dim n%, i%, s%n = Val(Text1.

6、Text)For i = 1 To ns = s + f(i) Next i Text2.Text = Str(s) End Sub4. 无限递归Private Sub Command1_Click()Dim n As Integern = 1Call dg(n) End Sub Private Sub dg(n As Integer)call dg (n + 1) End SubPrivate Sub Command1_Click()Dim n As Integern = 1Call dg(n) End Sub Private Sub dg(n As Integer)If n 10 Then

7、Exit SubElsedg (n + 1)End If End SubPrivate Sub Command1_Click()Dim n As Integern = 1Call dg(n) End Sub Private Sub dg(n As Integer)print nIf n 3 ThenExit SubElsedg (n + 1)End If End SubPrivate Sub Command1_Click()Dim n As Integern = 1Call dg(n) End Sub Private Sub dg(n As Integer)If n 3 ThenExit Su

8、bElsedg (n + 1)End Ifprint n End Sub递归真题实例Option Explicit Private Sub Form_Click()Dim a As Integera = 2: Call sub1(a) End Sub Private Sub sub1(x As Integer)x = x * 2 + 1If x 来加以区分;3、如果过程级变量与模块级或全局变量同名,在过程执行时,则使用过程级变量。(县官不如现管、天高皇帝远)7.6.4 同名变量Option Explicit Public X As Integer, Y As Integer, Z As Int

9、eger Private Sub Form_Activate()Conflict_XDebug.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 IntegerX = 135Debug.Print “X,Y 和 Z 是“, X, Y, Z End Sub 135 20 3510 20 35同名变量Private Sub Form_Click()Dim a As Integer, b As Integ

10、era = 1: b = 2Call proc1(a, b)Print a, b End SubPrivate Sub proc1(c As Integer, d As Integer)Dim a As Integer, b As Integerc = a + b: d = a - bPrint c, d End Sub0 00 0Dim a As Integer, b As Integer Private Sub Form_Click()a = 1: b = 2Call proc1(a, b)Print a, b End SubPrivate Sub proc1(c As Integer,

11、d As Integer)c = a + b: d = a - bPrint c, d End Sub3 13 1Dim x As Integer, y As Integer Private Sub Command1_Click()Dim a As Integer, b As Integera = 5b = 3Call sub1(a, b)Print a, bPrint x, y End SubPublic Function fun1(a As Integer, b As Integer) As Integerx = a + by = a - bPrint x, yfun1 = x + yEn

12、d Function Public Sub sub1(ByVal m As Integer, n As Integer)Dim x As Integer, y As Integerx = m + n: y = m n: m = fun1(x, y): n = fun1(y, x)End Sub10 610 -65 410 -6在过程中,使用Static语句说明的变量称为“静态变量”。静态变量在过程执行完后,其存储空间依然保留,并可继续保持其值。7.6.5 静态变量例: 有一个人编了下面一段程序,想用变量n记录单击窗体的次数。 Private Sub Form_Click()Dim n As I

13、ntegern = n + 1Print “已单击次数:“; n KK = 5Call Static_Variable(K)Debug.Print “第二次调用:K =“; K End Sub Private Sub Static_Variable(ByRef N As Integer)Static Sta As Integer 改成dim之后的结果又如何呢 ?Sta = N + StaN = Sta + N End Sub第一次调用: K=10第二次调用: K=15static例题 Option Explicit Private Sub Command1_Click()Dim k As In

14、tegerStatic i As Integeri = i + 1k = 5Call static_variable(k)Print “第“; i; “次调用:k=“; kEnd Sub Private Sub static_variable(ByRef n As Integer)Static sta As Integersta = n + stan = sta + n End Sub点击按钮command1三次后结果是多少?第1次调用:k=10第2次调用:k=15第3次调用:k=20总结:变量、过程的作用域1、过程的作用域 作用范围围模块级块级全局级级窗体标标准模块块窗体标标准模块块定义义方

15、式过过程名前加Private 例:Private Sub my1(形参表)过过程名前加Pubilc或默认认 例: Pubilc Sub my2(形参表)能否被本 模块块其他 过过程调调 用能能能能能否被本 应应用程序 其他模块块 调调用不能不能能,但必须须在 过过程名前加窗体 名。例:Call 窗 体名. My1(实实 参表)能,但过过程名必 须须唯一,否则则需 要加标标准模块块名 。例:Call 标标准模 块块名.My2(实实参 表)2、变量的作用域 作用范围围局部变变量窗体/模块级变块级变 量全局变变量 窗体标标准模块块声明方式Dim/Static Dim、PrivatePublic声明位置在过过程中窗体/模块块的“通 用声明”段窗体/模块块的“通 用声明”段能否被本模块块 其他过过程存取不能能能能否被其他模 块块存取不能不能能,但在 变变量名前 加窗体名能3、静态变量用Static声明的静态变量,在每次调用过程时保持原来的值,不重新初始化。而用Dim声明的变量,每次调用过程时,重新初始化4、同名变量对不同范围内出现的同名变量,可以用模块名加以区别。一般情况下,当变量名相同而作用域不同时,优先访问局限性大(局部变量)的变量。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

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