9-1 9-2 9-1.1 9-1.2 9-1.3 9-2.1 9-2.2 9-2.3 9-3 Visual Basic 9-3.1 Visual Basic 9-3.2 9-4 9-4.1 9-4.2
2 II 9-1 GoTo http://noi.stinfo.net/ xbqj/xbqj_12.htm structured programming 9-1 9-1 a goto b c goto d b goto e d goto a a End a a e b b goto c End b
9 3 top-down programming programming modulized 9-1.1 9-2 9-2 ~ ~ ~ ~ ~ ~
4 II 9-1.2 module 9-3 9-3 1 2 3 n 50
9 5 cohesion coupling 50
6 II 10 A 9-4 9-4 A A A' B B F F ( )1. ( )2. (A) (B) (C) (D) (A) (B) (C) (D) GoTo ( )3. (A) (B) (C) (D)
9 7 9-1.3 GoTo 9-5 9-5 GoTo F T F T GoTo
8 II GoTo 9-6 GoTo GoTo 9-6 GoTo GoTo : a = 1 line:print a a = a + 1 if a <=10 Then GoTo line Print "end" GoTo GoTo ( )1. (A) (B) (C) (D) ( )2. (A) (B) (C) (D) GoTo ( )3. (A) (B) (C) (D) CPU ( )4. Visual Basic Loop (D)Select Case (A)GoTo (B)MsgBox (C)Do
9 9 9-2 Visual Basic subprogram user-defined function 9-2.1 http://jc.infor.org/ vb1/vb2_13.doc Sub call 9-7 9-7. Call. A Sub. A Sub [( 1 [As ], 2 [As ], )]
10 II Call [( 1, 2, )] Visual Basic 1. Visual Basic 2. Sub formal parameters Call actual parameters 3. Call Call 1 1 N Sub N_Sum(a As Integer) 'a Dim sum As Integer, i As Integer sum = 0 For i = 1 To a sum = sum + i Next i MsgBox "1 + 2 +... + " & a & " = " & sum,, " 1 N " Private Sub Form_Activate( ) Dim N As Integer N = InputBox(" N ", " ") Call N_Sum(N) 'N "Ch09-2a.vbp"
9 11 9-8 1 1. Call N = 5 N_Sum( ) 2. N_Sum( ) 1 sum 1 2 sum 2 3 sum 3 3. MsgBox sum 1 N 2 Sub Change_temp(F As Integer) 'F Dim C As Integer C = (F - 32) * 5 / 9 MsgBox " " & F & " = " & C & " ",, " " Private Sub Form_Activate( ) Dim F_temp As Integer F_temp = InputBox(" ", " ") Call Change_temp(F_temp) 'F_temp "Ch09-2b.vbp"
12 II 9-9 2 1. Call F_temp = 76 Change_temp( ) 2. Change_temp( ) = ( - 32) * 5 / 9 3. MsgBox C 3 Sub Cal_sw(S_sex As String, S_ht As Single) 'S_sex S_ht Dim sw As Single If S_sex = "M" Then ' sw = (S_ht - 80) * 0.7 ElseIf S_sex = "F" Then ' sw = (S_ht - 70) * 0.6 End If MsgBox " " & sw,, " " Private Sub Form_Activate( ) Dim ht As Single, sex As String * 1 sex = InputBox(" M F", " ") ht = InputBox(" (cm) ", " ") Call Cal_sw(sex, ht) 'sex ht "Ch09-2c.vbp"
9 13 9-10 3 1. Call sex ht = M 176 Cal_sw( ) 2. Cal_sw( ) If-ElseIf 3. MsgBox 4. Sub Cal_sw(S_sex As String, S_ht As Single) 's h Dim sw As Single, swt As Single If S_sex = "M" Then ' End If MsgBox " " & sw,, " " swt = InputBox(" (kg)", " ") If swt > sw Then MsgBox " ",, " " Else MsgBox " ",, " " End If
14 II 1. = * 9 / 5 + 32 Sub Change_temp(C As Integer) Dim F As Integer F = Print " " & C & " = " & F & " " Private Sub Form_Activate( ) Dim X As Integer X = InputBox(" ", " ") Call 2. 2 1 2 Sub Check_Id(S_id As String) Dim sex As Integer sex = Int(Mid(S_id, 2, 1)) If Then Print " " ElseIf Then Print " " End If Private Sub Form_Activate( ) Dim id As String id = InputBox(" ", " ") Call
9 15 = 600 9-1 A B C 2.5 3 2.5 A B C 1.5 2 1.5 9-11
16 II Step1 1. 9-12 9-12 2. 9-2 Form1 Caption Command1(0) Caption A Command1(1) Caption B Command1(2) Caption C Command1(3) Caption A Command1(4) Caption B Command1(5) Caption C Label1 Caption Label2 Alignment 2 Label2 BackColor &H80000005& Label2 BorderStyle 1 Label2 Caption Label3 Caption Label3 ForeColor &H00FF0000&
9 17 Step2 1. InputBox( ) 2. cal_price( ) 3. 9-13 cal_price( ) 9-14 9-13 cal_price( ) click
18 II 9-14 cal_price( ) i j i 0, 2 1 3, 5 4 A/C = B = A/C = B = price = * 2.5 price = * 3 price = * 1.5 price = * 2 price * j Command1_Click
9 19 Step3 1. A Command1(0) Command1_Click 10 20 30 Private Sub Command1_Click(Index As Integer) Dim count As Integer count = InputBox(" ", " ") Call cal_price(index, count) 'Index count 10~20 30~30 cal_price( ) Index count 2. cal_price( ) 40 50 60 70 Sub cal_price(i As Integer, j As Integer) Dim price As Integer, sum As Integer Dim area As String price = 600 Select Case i Case 0, 2 area = " A/C " price = price * 2.5 Case 1 area = " B " price = price * 3 Case 3, 5 area = " A/C " price = price * 1.5 '
20 II 80 90 100 Case 4 area = " B " price = price * 2 End Select sum = j * price Label3.Caption = j & " " & area & " " & sum & " " 40~60 price 70~80 Select Case area 90~100 = * 10 9 cal_price( ) If-Then-Else 10 0.9
9 21 ( )1. (A) (B) (C) (D) ( )2. Visual Basic (A)0 (B)512 (C)-1024 (D)1024 Sub SUM(X As Integer, Y As Integer) TOTAL = -X ^ Y Print TOTAL Private Sub Form_Activate( ) Call SUM(2, 10) ( )3. 1 TOTAL = -X ^ Y TOTAL = X ^ 2 * (-Y) (A)-40 (B)40 (C)-200 (D)200 ( )4. Visual Basic (A)1 + 2 + 3 + + x (B)(1 + 2 + 3 + + x) 2 (C)1 2 + 2 2 + 3 2 + + x 2 (D)1 x + 2 x + 3 x + + x x Sub Cal(x As Integer) Dim sum As Long, i As Integer sum = 0 For i = 1 To x sum = sum + i ^ 2 Next i Print sum ( )5. 3 Cal( ) (A)14 (B)15 (C)16 (D)36
22 II 1. (1) A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (2) InputBox( ) (3) Left( ) Select Case (4) MsgBox "Sp09- a.frm" "Sp09-a.vbp"
9 23 9-2.2 Visual Basic user-defined function built-in function Visual Basic 2-2 Int( ) Str( ) Function Function End 9-15 9-15... 100 V V = A( ). Function End Function A( ) As Integer. A( ) = 100 Function [( 1 [As ], 2 [As ], )] As End Function =
24 II = [( 1, 2, )] 1. Visual Basic 2. Function 3. Function 1 1 N Function N_Sum(a As Integer) As Integer Dim sum As Integer, i As Integer sum = 0 For i = 1 To a Step 2 sum = sum + i Next i N_Sum = sum End Function 'a Private Sub Form_Activate( ) Dim N As Integer, total As Integer N = InputBox(" N ", " ") total = N_Sum(N) 'N MsgBox "1 + 3 + 5 +... + " & N & " = " & total,, " 1 N " "Ch09-2d.vbp"
9 25 9-16 1 1. N_Sum( ) N = 10 N_Sum( ) 2. N_Sum( ) 1 sum 1 2 sum 3 3 sum 5 3. sum 1 N sum total 2 Function Cal_Circle(r As Single) As Single Const pi = 3.14159 Cal_Circle = r ^ 2 * pi End Function 'r Private Sub Form_Activate( ) Dim X As Single Dim area As Single X = InputBox(" ", " ") area = Cal_Circle(X) 'X MsgBox " " & X & " = " & area,, " " "Ch09-2b.vbp" "Ch09-2e.vbp"
26 II 9-17 2 1. Cal_Circle( ) X = 10 Cal_Circle( ) 2. Cal_Circle( ) 3 Function Blood_Type(blood As String) As String Select Case blood Case "OO": Blood_Type = "O " Case "OA", "AO": Blood_Type = "O A " Case "OB", "BO": Blood_Type = "O B " Case "OAB", "ABO": Blood_Type = "A B " Case "AA": Blood_Type = "A O " Case "AB", "BA": Blood_Type = "A B O AB " Case "AAB", "ABA": Blood_Type = "A B AB " Case "BB": Blood_Type = "O B " Case "BAB", "ABB": Blood_Type = "A B AB " Case "ABAB": Blood_Type = "A B AB " End Select End Function Private Sub Form_Activate( ) Dim parent_blood As String Dim u_blood As String
9 27 parent_blood = InputBox(" :OO _ O ", " ") u_blood = Blood_Type(parent_blood) MsgBox " " & u_blood,, " " "Ch09-2f.vbp" 9-18 3 1. Blood_Type( ) parent_ blood Blood_Type( ) 2. Blood_Type( ) Select Case 3. 9-3 O A B AB O O v O A v v O B v v O AB v v A A v v A B v v v v A AB v v v B B v v B AB v v v AB AB v v v
28 II 1. Function Cal_Cycle(r As Single) As Const pi = 3.14159 Cal_Cycle = 2 * pi * r End Function Private Sub Form_Activate( ) Dim x As Single, p As Single x = InputBox(" ", " ") MsgBox " " & x & " = " & p 2. Function Cal_Rnd(lower As Integer, upper As Integer) As Cal_Rnd = Int(Rnd * (upper - lower + 1)) + lower End Function Private Sub Form_Activate( ) Dim n1 As Integer, n2 As Integer Dim n As Integer n1 = InputBox(" ", " ") n2 = InputBox(" ", " ") Print n
9 29 9-19 9-4 88 88 0.13 188 188 0.12 288 288 0.11 588 588 0.09 9-19 Step1 1. 9-20 9-20
30 II 2. 9-5 Form1 Caption Command1 Caption Combo1 Text Image1 Picture.jpg Image1 Stretch True Label1 Caption Label2 Caption Label3 Caption Label4 Caption Text1 Text Step2 9-21 1. AddItem Combo1 2. Mbill( ) 3. Mbill( ) Select Case If-Then-Else Mbill( ) bill 4. 9-21 9-22 Click
9 31 9-22 Mbill( ) F_sec F_case F_case 0 1 2 3 = 0.13 = 88 = 0.12 = 188 = 0.11 = 288 = 0.09 = 588 rate * F_sec T < F Command1_Click
32 II Step3 1. Form_Load 10~40 2. Command1_Click 50~100 10 20 30 40 Private Sub Form_Load( ) Combo1.AddItem " 88 " Combo1.AddItem " 188 " Combo1.AddItem " 288 " Combo1.AddItem " 588 " 50 60 70 80 90 100 Private Sub Command1_Click( ) Dim bcase As Integer, sec As Integer Dim bill As Integer bcase = Combo1.ListIndex 'bcase sec = Int(Text1.Text) * 60 'sec bill = Mbill(bcase, sec) Label4.Caption = " " & bill & " " 10~40 Combo1 50~80 Combo1 bcase 60 sec 90~90 Mbill( ) bcase sec bill 100~100 Label4 Caption
9 33 3. Mbill( ) 110 120 130 140 150 160 170 180 190 200 Function Mbill(F_case As Integer, F_sec As Integer) As Integer Dim rate As Single, fee As Integer Dim total As Integer Select Case F_case ' Case 0 rate = 0.13 fee = 88 Case 1 rate = 0.12 fee = 188 Case 2 rate = 0.11 fee = 288 Case 3 rate = 0.09 fee = 588 End Select total = rate * F_sec ' = If total < fee Then Mbill = fee Else Mbill = total End If End Function 110~140 Select Case 150~200 If-Then-Else (1) 288 0.11 (2) MsgBox 288/0.11/60 43
34 II ( )1. (A) 1 N (B) 1 N (C) 2 N (D) N! Sub Calculate(N As Integer) Dim i As Integer Dim power As Long power = 1 For i = 1 To N power = power * 2 Next i ( )2. Visual Basic (A)-2 (B)-1 (C)0 (D)1 Sub test (m As Integer) m = m ^ (m + 2) Private Sub Form_Activate( ) Dim C As Integer C = -2 Call test(c) Print C ( )3. Visual Basic FNA(4,18) (A) 9 (B)10 (C)16 (D)32 Sub FNA(X As Integer, Y As Integer) ANS = (X ^ 2 + Y Mod 7) / 2 Print ANS
9 35 1. "Ex09-b.vbp" 92 95 98 (1) / 18.5 92 23.2 95 23.9 98 25.4 (2) Select Case (3) "Sp09-b.vbp" 9-2.3 http://www.geog. ntu.edu.tw/course/ vb/.files/ frame.htm VB pass by value Visual Basic Visual Basic pass by reference
36 II Visual Basic Visual Basic 9-23 "ByRef" a 25 a 5 25 9-23 1 5 Click Private Sub Command1_Click( ) 10 Dim a As Single 20 a = Text1.Text 30 Label1.Caption = a 40 Call cal(a) 50 Label4.Caption = a 30 60 2 a 0006 80 "Ch09-2g.vbp" 3 0006 a (5) 25 50 Sub cal(byref x As Single) 60 Label2.Caption = x 70 x = x ^ 2 80 Label3.Caption = x 5 25 0005 0006 0007 0008 a x 0006
9 37 1. salary = 28000 grade = 80 Sub Raise(ByRef s As Integer, g As Integer) Select Case g Case 85 To 100: s = s * 1.10 Case 75 To 84: s = s * 1.05 Case Is <= 60: s = s * 0.95 End Select Private Sub Form_Activate( ) Dim salary As Integer, grade As Integer salary = InputBox(" ", " ") grade = InputBox(" ", " ") Call Raise(salary, grade) Print salary 2. salary = 18500 sale = 10000 Sub Income(ByRef s1 As Integer, s2 As Integer) As Integer Select Case s2 Case Is >= 10000: s1 = s1 + s2 * 0.08 + 1200 Case Is >= 6500: s1 = s1 + s2 * 0.06 + 800 Case Else: s1 = s1 + s2 * 0.02 End Select End Function Private Sub Form_Activate( ) Dim salary As Integer, sale As Integer salary = InputBox(" ", " ") sale = InputBox(" ", " ") Call Income(salary, sale) Print salary
38 II Visual Basic "ByVal" Call cal((a)) 9-24 a 5 a 5 9-24 1 5 Click Private Sub Command1_Click( ) 10 Dim a As Single 20 a = Text1.Text 30 Label1.Caption = a 40 Call cal(a) 50 Label4.Caption = a 30 60 80 2 a 5 x "Ch09-2h.vbp" 3 50 x 25 Sub cal(byval x As Single) 60 Label2.Caption = x 70 x = x ^ 2 80 Label3.Caption = x 5 5 25 0005 0005 0007 0008 0009 000A 000B a x
9 39 1. kg = 6 Sub Conversion(ByVal S_kg As Integer) S_kg = S_kg * 10 / 6 Print S_kg & " ", Private Sub Form_Activate( ) Dim kg As Integer kg = InputBox(" ", " / ") Call Conversion(kg) Print " " & kg & " " 2. n n = 5 Sub N_sum(ByVal An As Integer) Const a1 = 5 'a1 Const d = 2 'd An = a1 + (An - 1) * d 'A n n Print An, Private Sub Form_Activate( ) Dim n As Integer n = InputBox(" n ", " An") Call N_sum(n) Print "n = " & n
40 II 9-6 9-6 ByRef ByVal Visual Basic 1 Call Sort(N( )) ' N( ) 2 Call Sort(N(5)) ' N(5) Call Sort((N(3))) ' N(3)
9 41 ( )1. Visual Basic (A) x y z a b (B) x y a b (C) a b x y (D) a b x y z Sub test(byref x As Integer, y As Integer) Const z = 2 x = x Mod 3 + y * z 'ByRef Private Sub Form_Activate( ) Dim a As Integer, b As Integer a = 15: b = 6 Call test(a, b) Print a * b ( )2. (A)36 (B)54 (C)72 (D)90 1. Sub Cal_bonus( x As Integer, ByVal y As Integer) x = x + y / 20 MsgBox " " & x & " " Private Form_Activate( ) Dim bonus As Integer, bill As Integer bonus = InputBox(" ") bill = InputBox(" ") Call Cal_bonus(bonus, bill) Select Case bonus '.
42 II ( )1. Visual Basic (A) (B) 2 (C) (D) Call ( )2. Visual Basic Test(9) (A)139 (B) 123 (C)45 (D)13 Function Test(n As Integer) As Integer Dim i As Integer For i = 1 To n If n Mod i = 0 Then Print i Next i End Function ( )3. Visual Basic (A)24 (B)20 (C)16 (D)12 Sub Fact(ByVal x As Integer) Dim i As Integer, k As Integer k = 1 For i = 1 To x k = k * i Next i Print k 'ByVal Private Sub Form_Activate( ) Dim a As Integer a = 4 Call Fact(a)
9 43 ( )4. VB a (A)-1 (B)0 (C)1 (D)2 Sub Good(s As Integer) s = s + 1 Sub Bad(ByVal t As Integer) t = t - 2 Private Sub Form_Activate( ) Dim a As Integer a = 0 Call Good(a) Call Bad(a) Print a ( )5. T (A)40 (B)30 (C)20 (D)10 Function FNP(X) As Long FNP = X * X + X End Function Private Sub Form_Activate( ) Dim K As Integer, T As Integer For K = 1 To 4 T = T + FNP(K) Next K Print T
44 II 9-3 Visual Basic Visual Basic project Visual Basic 9-3.1 Visual Basic 9-25 Visual Basic Visual Basic 3 9-25 "Form" *.frm Form1. frm Visual Basic 9-1.2 9-26
9 45 9-26 method Show Visual Basic Hide "Ex09-3a2.frm" Form1 1 Step "Ex09-3a.vbp" 2 Step /
46 II Step 3 4 Step "Ex" frm" "Ex09-3a2. Step 5 6 Step "Form2(Ex09-3a2.frm)" Form2 7 Step Command1_Click Step 8 9 Step "Sp09-3a1.frm" "Sp09-3a2.frm" "Sp09-3a.vbp" end
9 47 "Sp09-3a.vbp" 9-27 / *.bas Module1.bas "Module" 9-27
48 II Visual Basic Main 9-28 /Project1 Project1 Visual Basic 1 9-28 Visual Basic "Class" *.cls Class1.cls ( )1. (D)4 (A)1 (B)2 (C)3 ( )2. (A)1 (B)2 (C)3 (D)4 ( )3. Visual Basic (A) *. frm (B) (C) *.cls (D)
9 49 9-3.2 Visual Basic procedure Visual Basic "Private" Cyl( ) 9-29 Form_Activate 9-29 Public Sub Cir( r As Single) Dim area As Single Const pi = 3.14159. Cir( ) Form_Activate( ) Private Sub Form_Activate( ) Dim a As Integer. Cyl( ) Activate( ) Form_ Call Cir(a) Call Cyl(a). Private Sub Cyl( r As Integer) Dim leng As Single. Form1 Module1
50 II Visual Basic "Public" power( ) 9-30 9-30 Public Sub power(n As Integer) ' 2 N Dim a As Integer. Print a Private Sub Form_Activate( ) Call power(5) Visual Basic 9-31( ) ( ) Command1_Click 9-31( ) Show1( ) Show2
9 51 9-31 Public Sub Show1( ) Label1(0).Caption = "F" 1 Private Sub Show2( ) Label1(1).Caption = "F" Private Sub Command1_Click( ) Call Show1 Call Show2 Call Module1.Show1 Call Module1.Show2 2 3 4 Public Sub Show1( ) Form1.Label1(2).Caption = "M" Private Sub show2( ) Form1.Label1(3).Caption = "M" Form1 Module1 Command1_Click Private 1 2 3 4 Command1_ Click Show2( ) "Ch09-3a.vbp"
52 II ( )1. (B)Const (C)Public (D)Private Sub (A)Dim ( )2. Visual Basic Form1 (A)0 (B)1 (C)2 (D)3 Public Function Add(x As Integer, y As Integer) As Long Add = x + y End Function Private Function Subs(x As Integer, y As Integer) As Integer Subs = x - y End Function Private Sub Command1_Click(Index As Integer) Dim a As Integer Dim b As Integer a = Int(Text1.Text) b = Int(Text2.Text) If Index = 0 Then ' (+) Call Add(a, b) ElseIf Index = 1 Then ' (-) Call Subs(a, b) End If ( )3. (A) Add( ) (B) Subs( ) (C) Command1_Click (D)
9 53 ( )1. Visual Basic (A) (B) (C) (D) ( )2. (A) (B) (C) (D) ( )3. (A) (D) (B) *.cls (C) Module ( )4. Visual Basic (A)Sub (B)Public (C)Private (D)End ( )5. Visual Basic Form1 Module1 Share( ) Form_Click Share( ) (A)Call Share (B) Call Module1 (C)Call Form1.Share (D)Call Module1.Share ( )6. Form_Click Share( ) (A)Call Share (B)Call Form1 (C)Call Module1 (D) Call Module1.Share ( )7. Visual Basic (A)Big (B)Small (C)Equal (D) ' Form1 ' Module1 Private Sub Form_Activate( ) Private Sub test(x%, y%) Dim a As Integer If (x - y) > 0 Then Dim b As Integer Form1.Print "Big" a = 10 ElseIf (x - y) < 0 Then b = 6 Form1.Print "Small" Call Module1.test(a, b) Else Form1.Print "Equal" End If
54 II 9-4 Visual Basic Dim Private Public 9-4.1 scope Visual Basic global variable local variable "Public" Public As Public grade As Integer
9 55 9-32 fgv mgv mp( ) fgv Form1 fgv mgv Form1 Module1 9-32 Module1 Public mgv As Integer mgv fgv Sub mp( ) Form_Activate mgv = mgv + 1 Form1.fgv = Form1.fgv + 1 3 2 Form1.Print mgv; Form1.fgv Form1 Public fgv As Integer Private Sub Form_Activate( ) fgv = 1 1 mgv = 2 2 Call mp mgv = mgv + 1 4 fgv = fgv + 1 3 Print mgv; fgv Print Print "Ch09-4a.vbp"
56 II 9-33 9-33 Private flv flv Private sub fp( ) Dim f f End sub Private Dim As As 1 2 Private zipcode As String Dim tel As String "Private" "Dim" Dim As Dim sex As Boolean
9 57 9-34 flv mlv mlv flv 9-34 Module1 Dim mlv As Integer mlv flv Form_Activate Sub mp( ) Dim flv As Integer mlv = mlv + 1 flv = flv + 1 Form1.Print mlv; flv 0 1 0 1 Form1 Dim flv As Integer mtv flv Private Sub Form_Activate( ) Dim mlv As Integer flv = 1 mlv = 2 Call mp mlv = mlv + 1 flv = flv + 1 Print mlv; flv 2 3 1 2 Print Print "Ch09-4b.vbp"
58 II Option Explicit Visual Basic Option Explicit Dim Private Public ReDim Static 9-35 9-35 / j
9 59 ( )1. Visual Basic (C) (D) (A) (B) ( )2. Visual Basic (A)5 8 (B)5 3 (C)3 5 (D)8 5 ' Form1 Private Sub form_activate( ) Dim i As Integer i = i + 5 Print i; Call Test ' Module1 Dim i As Integer Public Sub Test( ) Dim j As Integer j = 3: i = i + j Form1.Print i 1. Visual Basic (1) (2) Print ' Form1 Private Sub form_activate( ) Dim a As Integer, b As Integer, c As Integer a = 3 b = 2 c = a + b Call power(a, b) Print c ' Module1 Dim c As Long Sub power(x As Integer, y As Integer) Dim k As Integer k = 10 c = x + y * (k - 1) Form1.Print c
60 II 9-4.2 Visual Basic life time 9-36 9-36 Public x As Integer Sub add( ) x Sub min( ) 9-37 y 9-37 z
9 61 9-37 Public x As Integer Private y As Date Sub add( ) Dim z As String z y Sub min( ) 9-37 add( ) z static variable Timer Static Static sub xxx( ) Static As 9-38 Timer1_Timer d s Timer1_Timer
62 II d 0 s s 9-39 9-38 Private Sub Timer1_Timer( ) Dim d As Integer Static s As Integer Print Print "d = " & d, "s = " & s d = d + 1 s = s + 1 Print "d = " & d, "s = " & s Print 'd 's ' 9-39 Timer1_Timer 1 Timer1_Timer 2 Timer1_Timer 3 Timer1_Timer "Ch09-4c.vbp" 9-38 s 9-7
9 63 9-7 VB VB Public Dim Private Static Dim ( )1. Visual Basic (B) (C) (D) (A) ( )2. (A) (B) (C) (D) ( )3. (A) (B) (C) (D)
64 II ( )1. Visual Basic (A) (B) (C) (D) ( )2. Visual Basic (A)x (B)ck (C) sex (D)name Dim name As String Function ring( ) As Integer Dim id As String Dim sex As Boolean Private Sub Command1_Click( ) Static ck As Integer, x As Single x = ring(ck) ( )3. Visual Basic (A)Dim (B)Static (C)Public (D)Private ( )4. (A) (B) (C) (D) 1. Visual Basic Timer1 Interval 1000 1 Timer1_Timer Private Sub Timer1_Timer( ) min As Integer sec As Integer If sec < 60 Then sec = sec + 1 Else min = min + 1 sec = 0 End If Cls Print " " & min & " " & sec & " "
9 65 2. 1 Form1 ' Form1 ' Module1 Private y As Integer Public x As Integer Sub add( ) Sub chx( ) x = x + 1 x = x + 10 y = y + 1 Private Sub Command1_Click( ) Call chx Call add Print x + y 3. 100 N As Integer Const value = 100 Sub Utility(cnt As Integer) N = value - cnt If N > 0 Then Print " N = " & N End If Private Sub Form_Click( ) count As Integer count = count + 1 Call Utility(count) 5
66 II ( )1. Visual Basic (A)8 30 (B)30 8 (C)83 (D)38 Private Sub Form_Activate( ) Dim x As Integer, y As Integer x = 8: y = 30 Call change(x, y) Print x;y Sub change(a As Integer, b As Integer) Dim temp As Integer If a < b Then temp = a a = b b = temp End If ( )2. (A) (B) (C) (D) ( )3. Function (A)Option (B)Public (C)Private (D)Static ( )4. (A) Visual Basic (B) (C) (D).frm.bas ( )5. Public Private (A) Public (B) Private (C) Public (D) Private
9 67 ( )6. (A) (B) (C) (D) Visual Basic Dim Static ( )7. A B x C x P x x (A) C x P (B) P x P x x (C) P A x A.x (D) B C C.x ( )8. Visual Basic (A)205 (B)75 (C)15 (D)125 ' Form1 ' Module1 Public x As Integer Public x As Integer Sub fp( ) Sub mp(y As Integer) x = 5 x = y / Form1.x Private Sub Form_Activate( ) Dim x As Integer x = 100 Call fp Call mp(x) Print x + Module1.x + Form1.x
68 II 1. "Ex09-c.vbp" game( ) (1) game( ) 60~90 (2) game( ) x 0 1 2 (3) "Sp09-c.frm" "Sp09-c.vbp" 2. "Ex09-d.vbp" Run( ) (1) Run( ) String( ) (2) If-Then-Else Label2 (3) "Sp09-d.frm" "Sp09-d.vbp"