1、第七章答案第七章 习题答案1在过程中声明的局部变量其生命期只到本次过程执行结束就终止了,但是在过程中声明的静态变量其生命期是只有当包含此过程的窗体被终止的时候才终止。2有三种形式,传值、传址和利用全局变量向过程传递数据。 传值:给过程传递的是变量的值,不会有副作用。 传址:给过程传递的是变量的地址,有副作用 利用全局变量:有副作用,3Private Sub C1_Click() Dim i As Integer, j As Integer i = 8: j = 8 Call aa(i, j) Print i Print jEnd SubPrivate Sub aa(ByVal m As Int
2、eger, ByRef n As Integer) m = m + 1 n = n + 1End Sub 4(1) BASIC (2) 第一行: 6 26;第二行: 2 62;第三行: -2 98 (3) 第一行: 23 ; 第二行: 47 (4) 第一行: 10 8; 第二行:12 -10;第三行:5 2;第四行:12 -105Private Sub Command1_Click() Dim ss As Single, hs As Single ss = Text1 Text2 = tra(ss)End SubPrivate Sub Command2_Click() Text1.Text =
3、 Text2 = End SubPrivate Sub Command3_Click() EndEnd SubPublic Function tra(ByVal C As Single) As Single Dim F As Single F = (C * 9) / 5 + 32 tra = FEnd Function6. Private Sub Command1_Click() Dim data_one As Integer data_one = InputBox(请输入要判断的数:, 素数判断) If Prime(data_one) Then sushu = Str(data_one) +
4、 是素数 else sushu = Str(data_one) + 不是素数 End IfEnd SubPrivate Sub Command2_Click() Text1 = End SubPrivate Sub Command3_Click() EndEnd SubPublic Function Prime(ByVal N As Integer) As Boolean Dim k As Integer For k = 2 To Sqr(N) If N Mod k = 0 Then Exit Function Next k Prime = TrueEnd Function7.Private
5、Sub Command1_Click() Dim ss As String Dim i As Integer For i = 1 To 100 If Prime(i) And Prime(i + 2) Then ss = ss & Chr(13) & Chr(10) & i & Str(i + 2) End If Next i Text1 = ssEnd SubPublic Function Prime(ByVal N As Integer) As Boolean Dim k As Integer For k = 2 To Sqr(N) If N Mod k = 0 Then Exit Fun
6、ction Next k Prime = TrueEnd Function8. Option ExplicitOption Base 1Private Sub Form_Click() Dim a As Integer, n As Integer Dim i As Integer Randomize Dim aa(20) As Integer a = Int(100 * Rnd() * 1) n = 1 Do For i = 1 To n - 1 If a = aa(i) Then Exit For End If Next i If i = n Then aa(n) = a n = n + 1
7、 End If a = Int(100 * Rnd() * 1) Loop While n Max Then Max = s maxi = i End If Next i Print Str(a(maxi) & + & Str(a(maxi + 1) & + & Str(a(maxi + 2) & + & Str(a(maxi + 3) & = & Str(Max)End Sub10. Private Sub Command1_Click() Dim a(5, 5) As Integer Randomize For i = 1 To 5 For j = 1 To 5 a(i, j) = Int
8、(100 * Rnd) + 1 Print a(i, j); Next j Print Next i Call fmax(a)End SubPrivate Sub fmax(a() As Integer) Max = a(1, 1) maxi = 1 maxj = 1 For i = 1 To 5 For j = 1 To 5 If a(i, j) Max Then Max = a(i, j) maxi = i maxj = j End If Next j Next i Print 最大元素为:a( + Str(maxi) + , + Str(maxj) + )= + CStr(Max)End
9、 Sub11 . Private Sub Command1_Click() Dim i As Integer For i = 1 To 6 Text1 = Text1 & Str(fab(i) Next iEnd SubPrivate Function fab(n As Integer) As Long If n = 1 Or n = 2 Then fab = 1 Else fab = fab(n - 1) + fab(n - 2) End IfEnd Function12 .Private Sub Command1_Click() Dim i As Integer a = InputBox(
10、请输入一个大于2的偶数:) n = Val(a) If n = 4 Then Print 4 = 2 + 2 Exit Sub End If For i = 3 To n - 1 Step 2 If prime(i) And prime(n - i) Then Print Str(n) + = + Str(i) + + + Str(n - i) Exit Sub End If Next i Print 歌德巴赫猜想不对!End SubPrivate Function prime(ByVal n As Integer) As Boolean For i = 2 To n - 1 If n Mod
11、 i = 0 Then prime = False Exit Function End If Next i prime = TrueEnd Function13.Private Sub Command1_Click() Dim i As Integer For i = 10 To 9999 Call arm(i) Next iEnd SubPrivate Sub arm(j As Integer) a = CStr(j) n = Len(a) For k = 1 To n b = Mid(a, k, 1) s = s + Val(b) n Next k If s = j Then Print
12、j End IfEnd Sub14.Private Sub Command1_Click() Dim n As Integer, m As String, b As Integer n = InputBox(请输入原始数的进制(2-16):) m = InputBox(请输入原始数:) Label1.Caption = Str(n) + 进制数 Text1 = m b = trans(m, n) Text2 = Str(b)End SubPrivate Function trans(m As String, n As Integer) As Integer Dim k As Integer,
13、t As String, r As Integer, s As Integer k = Len(m) For i = 1 To k t = UCase(Mid(m, i, 1) If t = A And t Max Then Max = b(i) If b(i) Max Then Max = b(i) If b(i) Min Then Min = b(i) Sum = Sum + b(i) Next i Sum = Sum - Max - Min Text1 = Str(Sum / 4)End Sub16.Private Sub Command1_Click() Dim i As Intege
14、r, k As Integer, m As Integer Dim s As Single, t As Single i = 1 Do t = 1 For k = 1 To i m = 2 * k t = t * m Next k s = s + 1 / t If 1 / t 0.00001 Then Exit Do i = i + 1 Loop Print sEnd Sub17.Private Sub Command1_Click() Dim n As Integer, t As Single, s As Single, x As Single n = 2: s = 0 x = InputB
15、ox(input x:) Do t = (x f(n) / (f(n - 1) * f(n) s = s + t If t sort(2) Then temp = sort(1) sort(1) = sort(2) sort(2) = temp End If For i = 3 To ub flg = False flg1 = False temp = sort(i) If temp sort(1) Then flg = True mid = 0 End If left = 1 right = i - 1 Do While left = right And Not flg mid = Int(
16、left + right) / 2) If sort(mid) = temp Then flg1 = False Exit Do ElseIf sort(mid) 25 Then Exit Do aa(2, num) = 0 cnt = 0 End If ct = ct + 1 Do While ct cnt num = num + 1 If num 25 Then num = num Mod 25 cnt = aa(2, num) + cnt Loop Loop XXX = bb(25)End Function20.Option ExplicitDim ss As StringPrivate
17、 Sub C1_Click() Call great_zs T1 = ssEnd Sub产生6个不同的数Public Sub great_zs() Dim shu_1 As Integer, shu_2 As Integer, shu_3 As Integer, shu_4 As Integer, shu_5 As Integer, shu_6 As Integer Dim shu As Single For shu_1 = 1 To 9 For shu_2 = 1 To 9 For shu_3 = 1 To 9 For shu_4 = 1 To 9 For shu_5 = 1 To 9 Fo
18、r shu_6 = 1 To 9 shu = Val(CStr(shu_1) & CStr(shu_2) & CStr(shu_3) & CStr(shu_4) & CStr(shu_5) & CStr(shu_6) If lala(shu) And zhishu(shu) Then ss = ss + Str(shu) 把满足要求的数记录到变量 ss 中 Next shu_6 Next shu_5 Next shu_4 Next shu_3 Next shu_2 Next shu_1End Sub判断相邻的两个数是否为素数Public Function zhishu(ByVal zss As
19、 Single) As Boolean Dim i As Integer, jud As Integer For i = 1 To 5 jud = Val(Mid(CStr(zss), i, 1) + Val(Mid(CStr(zss), i + 1, 1) If Not Prime(jud) Then Exit Function Next i If Not Prime(Val(Mid(CStr(zss), 6, 1) + Val(Mid(CStr(zss), 1, 1) Then Exit Function zhishu = TrueEnd Function判断素数Public Functi
20、on Prime(ByVal N As Integer) As Boolean Dim k As Integer For k = 2 To Sqr(N) If N Mod k = 0 Then Exit Function Next k Prime = TrueEnd Function判断数列中的数是否各不相同Public Function lala(ByVal la As Single) As Boolean Dim i As Integer, j As Integer For i = 1 To 6 For j = 1 To 6 If Mid(CStr(la), i, 1) = Mid(CStr(la), j, 1) And i j Then Exit Function Next j Next i lala = TrueEnd Function21.Private Sub Command1_Click() Dim i As Intege
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1