第7章習(xí)題
一、單選題
B 2. C 3.B
4.B 5. C 6. D
7. C 8. B
二、填空題
EF
2. 7; 1; 4
3. (1)10 (2)28 (3)6 (4) 6
4. Call Comd1_Click
5. (1) n>=16 (2) n\16 (3) Mid(ch, r+1, 1)
編程及上機調(diào)試
1. 在窗體上建立四個文本框Text1和一個命令按鈕Command1(“檢驗”)。文本框Text1~Text3用于輸入,Text4用于輸出結(jié)果。
Private Sub Command1_Click()
Dim a As String, b As String, c As String
a = Trim(Text1.Text)
b = Trim(Text2.Text)
c = Trim(Text3.Text)
If che(a) And che(b) And che(c) Then
Text4.Text = Val(a) + Val(b) + Val(c)
Else
Text4.Text = "存在非數(shù)字字符"
End If
End Sub
Function che(x As String) As Boolean '若存在非數(shù)字字符,函數(shù)值為假
che = True
For k = 1 To Len(x)
s = Mid(x, k, 1)
If s < "0" Or s > "9" Then
che = False
Exit For
End If
Next k
End Function
2.在窗體上建立七個文本框Text1~Text7和一個命令按鈕Command1(“比較”)。Text1~Text6用于輸入,Text7用于輸出結(jié)果。
Private Sub Command1_Click()
a = Val(Text1.Text):b = Val(Text2.Text):c = Val(Text3.Text)
d = Val(Text4.Text):e = Val(Text5.Text):f = Val(Text6.Text)
Call max(a, b, c, m)
Call max(m, d, e, m)
Call max(m, f, f, m)
Text7.Text = m
End Sub
Sub max(x, y, z, m) ‘標(biāo)準(zhǔn)模塊文件
If x > y Then
m = x
Else
m = y
End If
If m < z Then
m = z
End If
End Sub
3. (1) s<>"" (2) Mid(s, p+1) (3) n
4. 程序代碼如下:
Private Sub Form_Load()
Dim x As Single, n As Integer, y As Double
x = InputBox("x=", "求X的n次方")
n = InputBox("n=", "求X的n次方")
y = Power(x, n)
Show
Print x; "的"; n; "次方="; Format(y, "###,###.##")
End Sub
Function Power(x As Single, n As Integer) As Double
If n > 1 Then
Power = Power(x, n - 1) * x
Else
Power = x
End If
End Function
5. 在窗體上設(shè)置三個窗體Form1~Form3。Form1中建立了兩個文本框Text1(“用戶名”)和Text2(“密碼”),以及命令按鈕Command1(“判斷”)。Form2中建立了一個文本框Text1(其Text屬性值為“歡迎你使用本系統(tǒng)”)和命令按鈕Command1(“返回”)。Form3中建立了一個文本框Text1(其Text屬性值為“對不起,請向管理員查詢”)和命令按鈕Command1(“退出”)。
Dim n As Integer
Private Sub Command1_Click() ‘窗體Form1的“判斷”
usern = Trim(Text1.Text):passw = Trim(Text2.Text)
If usern = "username" And passw = "password" Then
Form1.Hide
Form2.Show
Else
n = n + 1:Text1.SetFocus
If n = 3 Then
Form1.Hide
Form3.Show
End If
End If
Text1.Text = "":Text2.Text = ""
End Sub
Private Sub Form_Activate()
Text1.Text = "":Text2.Text = ""
Text1.SetFocus
End Sub
Private Sub Command1_Click() ‘窗體Form2的“返回”
Form2.Hide
Form1.Show
End Sub
Private Sub Command1_Click() ‘窗體Form3的“退出”
Unload Form1
Unload Form2
Unload Form3
End
End Sub
6. (1) Form1窗體模塊程序代碼:
Private Sub Form_Load()
Randomize
End Sub
Private Sub Command1_Click() '"生成隨機數(shù)"
Dim s As String
s = ""
For i = 1 To 10
a(i) = Int(11 + 90 * Rnd)
s = s + Str(a(i)) + ","
Next i
Text1.Text = Left(s, Len(s) - 1)
End Sub
Private Sub Command2_Click() '"求最大數(shù)"
Text2.Text = Max()
End Sub
Private Sub Command3_Click() '"打開排序窗口"
Form1.Hide '隱藏主窗體
Form2.Show '顯示“排序”窗體
End Sub
Private Sub Command4_Click() '"結(jié)束"
Unload Form1 '隱藏主窗體
Unload Form2 '顯示“排序”窗體
End
End Sub
(2) Form2窗體模塊程序代碼:
Private Sub Command1_Click() '“排序”
Dim t As String
Call Sort(t)
Text1.Text = t
End Sub
Private Sub Command2_Click() '“返回”
Form2.Hide '隱藏“排序”窗體
Form1.Show '顯示主窗體
End Sub
(3) 標(biāo)準(zhǔn)模塊程序代碼:
Public a(1 To 10) As Single
Function Max() As Single '求最大值
Dim m As Single
m = a(1)
For i = 2 To 10
If m < a(i) Then
m = a(i)
End If
Next i
Max = m
End Function
Sub Sort(s As String) '排序
s = ""
For i = 1 To 9
For j = i + 1 To 10
If a(i) > a(j) Then
t = a(i): a(i) = a(j): a(j) = t
End If
Next j
s = s + Str(a(i)) + ","
Next i
s = s + Str(a(10))
End Sub