过程程序设计.docVIP

  • 25
  • 0
  • 约8.56千字
  • 约 6页
  • 2017-08-11 发布于重庆
  • 举报
过程程序设计.doc

实验十二 过程程序设计(一) (实验教程的实验十三) (1)掌握sub子过程的概念和使用方法。 (2)掌握形参和实参的概念以及在调用过程时两者的对应关系。(3)掌握调用sub子过程的两种方法。 尝试编写下列程序 【12-1】编写sub子过程,输出如图12-1所示图形。 Private Sub Form_click() Call tu ‘子过程调用 Call tx Call tu End Sub Sub tu() ‘子过程的定义 Print ********** End Sub Sub tx() ‘子过程的定义 Print hello End Sub 【12-2】编写过程,功能是把三个整数按从大到小的次序排列并显示出来。程序运行界面如图12-2所示。 Public Sub sort(x As Integer, y As Integer) 通用过程 Dim Temp As Integer If x y Then Temp = x: x = y: y = Temp ‘Temp为中间变量,用于完成x于y值的互换 End If End Sub Private Sub Command1_Click() Dim a As Integer, b As Integer, c As Integer a = Val(Text1.Text) b = Val(Text2.Text) c = Val(Text3.Text) Call sort(a, b) 或 Sort a, b 调用过程实现排序 Call sort(a, c) 或 Sort a, c Call sort(b, c) 或 Sort b, c Cls Print a, b, c End Sub Private Sub Command2_Click() End End Sub 【思考】将Sub子过程的头改为下面形式,运行结果有变化吗? Public Sub sort(ByVal x As Integer, ByVal y As Integer) (举一反三 【12-2】输出三个数中的最大数。 ******* Begin ************ Private Sub sort(x As Integer, y As Integer, z As Integer, m As Integer) If x y Then m = x Else m = y If z m Then m = z End Sub ******* End ************ Private Sub Command1_Click() Dim a As Integer, b As Integer, c As Integer,max as Integer a = Val(Text1.Text) b = Val(Text2.Text) c = Val(Text3.Text) Call sort(a, b,c,max) Print max End Sub 【12-3】编写过程找出所有水仙花数,打印“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”,153=13+53+33。程序运行界面如图12-3所示。 图12-3水仙花数程序运行界面 源代码: Private Sub shuixian(n As Integer) Dim x As Integer, y As Integer, z As Integer x = Int(n / 100) 分离出百位数 y = Int(n / 10) Mod 10 分离出十位数 z = n Mod 10 分离出个位数字 If n = x ^ 3 + y ^ 3 + z ^ 3 Then Print Str(x) + ^3 + +; Str(y) + ^3 + + + Str(z) + ^3 + = + Str(n) + 是水仙花数 End If End Sub Private Sub Form_Click() Dim n As Integer For n = 100 To 999 Call shuixian(n) Next n End Sub (举一反三【12-3】从键盘任意输入一个正整数n,输出2~n之间所有能被3或7整除的数。 Private Sub fac(n As Integer) ******* Begin ************

文档评论(0)

1亿VIP精品文档

相关文档