1 / 35

Visual Basic 程序设计教程

Visual Basic 程序设计教程. 第 7 章 过 程. 7.4.1 过程的嵌套 7.4.2 过程的递归 习题七. 7.1 Sub 过程 7.1.1 事件过程与通用过程 7.1.2 通用过程的创建 7.1.3 通用过程的调用 7.2 Function 过程 7.2.1 Function 过程的定义 7.2.2 Function 过程的调用 7.2.3 查看过程 7.3 向过程传递参数 7.3.1 形式参数与实际参数 7.3.2 按值 传递与 按地址 传递 7.3 . 3 使用参数

oki
Download Presentation

Visual Basic 程序设计教程

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Visual Basic 程序设计教程

  2. 第7章 过 程 7.4.1 过程的嵌套 7.4.2 过程的递归 习题七 7.1Sub过程 7.1.1 事件过程与通用过程 7.1.2 通用过程的创建 7.1.3 通用过程的调用 7.2Function过程 7.2.1 Function过程的定义 7.2.2 Function过程的调用 7.2.3 查看过程 7.3 向过程传递参数 7.3.1 形式参数与实际参数 7.3.2 按值传递与按地址传递 7.3.3 使用参数 7.3.4 传递数组 7.4过程的嵌套与递归调用

  3. 7.1.1 事件过程与通用过程 1. 事件过程 控件事件过程的语法为 Private Sub〈控件名〉_〈事件名〉([〈形参表〉]) [〈语句组〉] End Sub 窗体事件过程的语法为 Private Sub Form_〈事件名〉([〈形参表〉]) [〈语句组〉] End Sub 7.1.2 通用过程的创建 1. 在“代码”编辑窗口中输入 [ Private | Public ][ Static ] Sub〈过程名〉( [〈形参表〉] ) [〈语句列〉] [ Exit Sub ] [〈语句列〉] End Sub

  4. 7.1.3 通用过程的调用 调用Sub过程有两种方法: =使用Call语句:Call〈过程名〉( [〈实参表〉] ) =直接使用过程名:〈过程名〉[〈实参表〉] 【例7-1】在一个窗口中包含3个命令按钮,当用户单击其中一个时,要求其他个别按钮不能使用, (1) 双击窗体的空白区,打开代码编辑窗口。 (2) 编写通用过程代码:

  5. Public Sub Button_Manage(Buttom As Object) Select Case Buttom Case Command1 Command2.Enabled = False Case Command2 Command1.Enabled = False Case Command3 Command1.Enabled = True Command2.Enabled = True End Select End Sub

  6. (3) 运行工程。 【例7-2】计算阶乘5!、6!、8!,以及阶乘的和5! + 6! + 8!(如图7-6所示)。 fact通用子过程为 Sub fact(m As Integer, total As Long) ' 计算阶乘子过程 Dim i As Integer total = 1 For i = 1 To m total = total * i Next i End Sub

  7. 命令按钮组的Click事件代码为 Private Sub Command1_Click(Index As Integer) Dim a As Integer, b As Integer, c As Integer, s As Long, tot As Long n = Index Select Case n Case 0 a = 5 Call fact(a, tot) Label1.Caption = a & "! = " & tot Case 1 a = 6 Call fact(a, tot) Label1.Caption = a & "! = " & tot Case 2 a = 8 Call fact(a, tot)

  8. Label1.Caption = a & "! = " & tot Case 3 a = 5: b = 6: c = 8 Call fact(a, tot) s = tot Call fact(b, tot) s = s + tot Call fact(c, tot) s = s + tot Label1.Caption = a & "! + " & b & "! + " & c & "! = " & s End Select End Sub

  9. 7.2 Function过程 7.2.1 Function过程的定义 [ Private | Public ][ Static ] Function〈函数名〉( [〈形参表〉] ) [ As〈类型〉] [〈语句列〉] [〈函数名〉=〈表达式〉] [ Exit Function ] [〈语句列〉] [〈函数名〉=〈表达式〉] End Function

  10. 【例7-3】计算任意整数n的阶乘的Function过程fact:【例7-3】计算任意整数n的阶乘的Function过程fact: Function fact(x As Integer) As Long Dim p As Long, i As Integer p = 1 For i = 1 To x p = p * i Next i fact = p End Function 【例7-4】计算圆面积的Function过程cir: Function cir(r As Single) As Single Const pi As Single = 3.1415926 cir = pi * r ^ 2 End Function

  11. 【例7-5】已知直角三角形两直角边,计算第3边(斜边)的Function过程:【例7-5】已知直角三角形两直角边,计算第3边(斜边)的Function过程: Function Hypotenuse( a As Integer, b As Integer ) As Single Hypotenuse = Sqr( a^2 + b^2 ) End Function 【例7-6】求3到10的阶乘之和。 Private Sub Command1_Click() Dim sum As Long, i As Integer For i = 3 To 10 sum = sum + fact(i) Next i Label2.Caption = sum End Sub

  12. 【例7-7】编写求两数最大公约数的Function过程。在主程序中输入3个整数,调用Function过程求出3个整数的最大公约数。【例7-7】编写求两数最大公约数的Function过程。在主程序中输入3个整数,调用Function过程求出3个整数的最大公约数。 Function Hcf(m As Long, n As Long) As Long Dim r As Long, c As Long If m < n Then c = m: m = n: n = c End If r = m Mod n Do While r <> 0 m = n n = r r = m Mod n Loop Hcf = n End Function

  13. Private Sub Command1_Click() Dim l As Long, m As Long, n As Long Dim p As String l = Val(Text1.Text) m = Val(Text2.Text) n = Val(Text3.Text) If l * m * n = 0 Then Exit Sub p = "3个数的最大公约数是:" & Str(Hcf(Hcf(l, m), n)) Label1.Caption = p End Sub 1. 按地址传递参数 【例7-8】利用【例7-7】中的Function过程,求任意两数的最大公约数。 应用程序用户界面的建立与对象属性的设置参见图7-9 Function过程参见【例7-7】,下面给出命令按钮的事件代码:

  14. Private Sub Command1_Click() Dim x As Long, y As Long temp = InputBox("请输入第1个整数:") x = Val(temp) temp = InputBox("请输入第2个整数:") y = Val(temp) If x * y = 0 Then Exit Sub Label1.Caption = x & "," & y & "的最大公约数是:" & Str(Hcf(x, y)) Label2.Caption = x & "," & y & "的最大公约数是:" & Str(Hcf(x, y)) End Sub

  15. 2. 按值传递参数 【例7-9】在例7-8中使用传值调用Function过程,求两个整数的最大公约数和最小公倍数。 应用程序用户界面的建立与对象属性的设置参见图7-10,Function过程参见例7-7,下面给出命令按钮的事件代码: Private Sub Command1_Click() Dim m As Long, n As Long temp = InputBox("请输入第1个整数:") m = Val(temp) temp = InputBox("请输入第2个整数:") n = Val(temp) If n * m = 0 Then Exit Sub Label1.Caption = m & "," & n & "的最大公约数是:" & Str(Hcf((m), (n))) Label2.Caption = m & "," & n & "的最小公倍数是:" & Str(m * n / Hcf((m), (n))) End Sub

  16. 1. 使用可选的参数 【例7-10】两个命令按钮的事件代码调用同一个过程,一个传递两个参数,而另一个值传递一个参数,如图7-11所示。 Sub过程ListText将传递过来的参数值添加到列表框中,其中第2个参数使用Optional关键字来声明: Private Sub ListText(x As String, Optional y As String) If IsMissing(y) Then temp = Format(x, "@@@@@@@@@@") Else temp = Format(x, "@@@@@@@@@@") & Format(y, "@@@@@@@@@@") End If List1.AddItem temp End Sub

  17. 传递2个参数的命令按钮Command1的Click事件代码:传递2个参数的命令按钮Command1的Click事件代码: Private Sub Command1_Click() Dim a As String, b As String a = InputBox("") b = InputBox("") Call ListText(a, b) End Sub 传递1个参数的命令按钮Command2的Click事件代码: Private Sub Command2_Click() Dim a As String a = InputBox("") Call ListText(a) End Sub

  18. 2. 提供可选参数的缺省值 也可以给可选参数指定缺省值。 【例7-11】在上例中,可以为未传递参数指定一个缺省值,如图7-12所示。 只需修改Sub过程ListText的代码: Private Sub ListText(x As String, Optional y As String = "*********") temp = Format(x, "@@@@@@@@@@") & Format(y, "@@@@@@@@@@") List1.AddItem temp End Sub

  19. 3. 使用不定数量的参数 【例7-12】改写例7-10中的Sub过程ListText,使之可以接收任意多个参数,如图7-13所示。 Sub过程ListText的代码改为 Private Sub ListText(ParamArray strx()) For Each x In strx temp = temp & Format(x, "|@@@@@@") Next List1.AddItem temp End Sub

  20. 传递3个参数的命令按钮Command1的Click事件代码:传递3个参数的命令按钮Command1的Click事件代码: Private Sub Command1_Click() Dim a As String, b As String, c As String a = Left(InputBox(""), 6) b = Left(InputBox(""), 6) c = Left(InputBox(""), 6) Call ListText(a, b, c) End Sub 传递2个参数的命令按钮Command2的Click事件代码: Private Sub Command2_Click() Dim a As String, b As String a = Left(InputBox(""), 6) b = Left(InputBox(""), 6) Call ListText(a, b) End Sub

  21. 7.3.4 传递数组 【例7-13】在例6-3中编写排序Sub过程,将存有随机数的数组作为参数传递到Sub过程。 窗体界面与例6-3相同,增加一个Sub过程pai_xu( ),并且改写排序按钮的Click事件代码,其余过程代码不变。 Sub过程pai_xu( )的代码如下: Sub pai_xu(p() As Integer) ' 如果形式参数定义了类型,实际参数 ' 也须定义相同的类型 For i = 1 To 9 For j = i + 1 To 10 If p(i) > p(j) Then t = p(i): p(i) = p(j): p(j) = t End If Next Next End Sub

  22. 排序按钮Command2的Click事件代码如下: Private Sub Command2_Click() p = "" Call pai_xu(a()) For i = 1 To 10 p = p & Str(a(i)) & "," Next Label2.Caption = LTrim(Left(p, Len(p) – 1)) End Sub 在模块的通用段定义了与形式参数相同数据类型的数组: Dim a(1 To 10) As Integer

  23. 【例7-14】随机产生三组整数,并求出每组的最大数,如图7-14所示。【例7-14】随机产生三组整数,并求出每组的最大数,如图7-14所示。 设计步骤如下: (1) 设计窗体界面以及设置对象属性。 (2) 编写程序代码。 考虑到要在不同的过程中使用数组,所以首先在模块的通用段声明数组: Dim a(1 To 10) As Integer, b(1 To 10) As Integer, c(1 To 10) As Integer 产生随机数组并将10个数连成一个字符串返回的Fuction过程:

  24. Function sui_ji(p() As Integer) As String temp = "" Randomize For i = 1 To 10 Do x = Int(Rnd * 90) + 10 yes = 0 For j = 1 To i - 1 If x = p(j) Then yes = 1: Exit For Next Loop While yes = 1 p(i) = x temp = temp & LTrim(Str(p(i))) & "," Next sui_ji = temp End Function

  25. 找出数组10个数中最大数并返回的Fuction过程: Function da(p() As Integer) As Integer Max = p(1) For i = 2 To 10 If Max < p(i) Then Max = p(i) Next da = Max End Function 两个命令按钮的Click事件代码: Private Sub Command1_Click() Form_Load End Sub Private Sub Command2_Click() Dim p As String p = "第1组数中的最大者是:" Frame1(0).Caption = p & da(a()) p = "第2组数中的最大者是:" Frame1(1).Caption = p & da(b()) p = "第3组数中的最大者是:" Frame1(2).Caption = p & da(c()) End Sub

  26. 随机整数的生成由窗体的Load事件代码完成: Private Sub Form_Load() Dim p As String p = sui_ji(a()) Frame1(0).Caption = "第1组随机整数:" Label1(0).Caption = LTrim(Left(p, Len(p) - 1)) p = sui_ji(b()) Frame1(1).Caption = "第2组随机整数:" Label1(1).Caption = LTrim(Left(p, Len(p) - 1)) p = sui_ji(c()) Frame1(2).Caption = "第3组随机整数:" Label1(2).Caption = LTrim(Left(p, Len(p) - 1)) End Sub

  27. 7.4 过程的嵌套与递归调用 【例7-15】输入参数n,m,求组合数 的值。 求阶乘Function过程fact的代码: Private Function fact(x) p = 1 For i = 1 To x p = p * i Next i fact = p End Function

  28. 求组合数用Function过程Comb的代码: Private Function comb(n, m) comb = fact(n) / (fact(m) * fact(n - m)) End Function 等号按钮Command1的Click事件的代码: Private Sub Command1_Click() m = Val(Text1(0).Text): n = Val(Text1(1).Text) If m > n Then MsgBox "请保证参数的正确输入!" ' 参数输入时必须保证n >= m Exit Sub End If Text2.Text = Format(comb(n, m), "@@@@@@@@@@@") End Sub

  29. 【例7-16】利用递归调用计算n!。 求阶乘的递归Function过程fact的代码: Private Function fact(n) As Double If n > 0 Then fact = n * fact(n - 1) Else fact = 1 End If End Function n = Val(Text1.Text0 If n < 0 Or n > 20 Then MsgBox ("非法数据!"): Exit Sub m = fact(n) Text2.Text = Format(m, "!@@@@@@@@@@") Text1.SetFocus End If End Sub

  30. 文本框的按键(KeyPress)事件代码: Private Sub Text1_KeyPress(KeyAscii As Integer) Dim n As Integer, m As Double If KeyAscii = 13 Then n = Val(Text1.Text0 If n < 0 Or n > 20 Then MsgBox ("非法数据!"): Exit Sub m = fact(n) Text2.Text = Format(m, "!@@@@@@@@@@") Text1.SetFocus End If End Sub

  31. 【例7-17】契比雪夫多项式定义如下。 求契比雪夫多项式的递归Function过程fnt的代码: Private Function fnt(n As Integer, x As Single) As Single If n = 0 Then fnt = 1 ElseIf n = 1 Then fnt = x Else fnt = 2 * x * fnt(n – 1, x) - fnt(n – 2, x) End If End Function

  32. 计算按钮Command1的Click事件代码: Private Sub Command1_Click() Dim m As Integer, y As Single m = Val(Text1.Text) y = Val(Text2.Text) Text3.Text = fnt(m, y) End Sub 习题七 7.1 编制判断素数的Sub过程或Function过程,验证哥德巴赫猜想:一个不小于6的偶数可以表示为两个素数之和。例如:6 = 3 + 3,8 = 3 + 5,10 = 3 + 7,…(参见第5章习题)。 7.2 编写输入一个0~6数字,显示汉英对照输出星期的Function过程。 7.3 编制随机整数Function过程,输出30个1~100之内的随机数。 7.4 编制求两数中的较大数Function过程,求多个数的较大数。 7.5 编制求较大数Function过程,使用不定数量的参数,求任意多数中的最大数。

  33. 7.6 编制判断奇偶数Function过程。输入一个整数,判断其奇偶性。 7.7 编制判断是否同时被17与37整除的Function过程。输出1000~2000之间所有能同时被17与37整除的数。 7.8 使用Timer函数设计用来暂停指定时间(秒)的Sub过程。 7.9 编写计算阶乘的Function过程,利用ex的下述近似公式计算e(直到最后一项小于10-6为止)。 7.10 编写Function过程返回指定字符、长度的字符串,实现在窗体上输出如图7-20所示的图形。

  34. 7.11 移动数组元素。将数组中某个位置的元素移动到指定位置。 7.12 向数组添加元素。将新添加的元素放到数组的最后位置。 7.17.13 向数组中的指定位置插入新元素,即将新添加的元素放到数组的指定位置。 7.14 删除数组中指定位置的元素。 7.15 编写自定义的Sub过程或Function过程重做第6章习题:把两个按升序(即从小到大)排列的数列a(1),a(2),…,a(n)和b(1),b(2),…,b(m),合并成一个仍为升序排列的新数列。 7.16 有5个人坐在一起,问第5个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第3个人,又说比第2个人大2岁。问第2个人,说比第1个人大2岁。最后问第1个人,他说是10岁。请问第5个人有多大岁数。

  35. 7.17 Hanoi塔问题:传说印度教的主神梵天创造世界时,在印度北部佛教胜地贝拿勒斯圣庙里,安放了一块黄铜板,板上插着三根针,在其中一根针上自下而上放着由大到小的64个金盘。这就是所谓的梵塔(Hanoi)。梵天要僧侣们坚定不移地按下面规则把64个盘子移到另一根针上: 7.16 有5个人坐在一起,问第5个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第3个人,又说比第2个人大2岁。问第2个人,说比第1个人大2岁。最后问第1个人,他说是10岁。请问第5个人有多大岁数。 7.17 Hanoi塔问题:传说印度教的主神梵天创造世界时,在印度北部佛教胜地贝拿勒斯圣庙里,安放了一块黄铜板,板上插着三根针,在其中一根针上自下而上放着由大到小的64个金盘。这就是所谓的梵塔(Hanoi)。梵天要僧侣们坚定不移地按下面规则把64个盘子移到另一根针上:

More Related