3、3 -1 4、7 0 实验6-7 【题目一】
1、 Sqr(2 + t)
2、 Abs(a - 1) < eps 【题目二】
1、 Dim X As Integer 2、 \3、 (j * i + 1)
习题参考答案
一 选择题
1~5: BDCCD 16~20:BDCDB
6~10:BBBAD 21~25:CCBAD
11~15:DAAAB 26~28:DAB
二、填空
1(1) 形式 、(2) 实在 2(1) 按地址传递 、(2) ByVal 3(1) 窗体模块 、(2) 标准模块 、(3) 类模块 4(1) 6 5(1)4 20 101 6(1) 6 26 、(2) -2 98 7(1) 10 6 、(2) 5 -5 8(1) 6 5 、(2) 8 7 9(1) 11 、(2) 7 10(1) Fun(i) 、(2) Fun=p 11(1) 4 、(2) 20.7 12(1) 29 、(2) 28.7 13(1) k = j 、(2) b() As Integer 14(1) gec 、(2) ge 、(3) 3
15(1) p1 = True And p2 = True 或 p1 And p2 、(2) p1 = p2 – 2 、 (3) Isp = True 、(4) m Mod i = 0 16(1) Convert(st) 、(2) Len(s) 、
(3) \ 、(4) k = k + p * h 17(1) isualVi 、(2) sualVisu 18(1) s + a 、(2) n + 1 、 (3) x As Single, n As Integer 、(4) p * (x + i) / ((2 * i - 1) * i * x)
三、改错题
1、
Option Base 1
Private Sub Command1_Click()
Dim A() As Integer, I As Integer, J As Integer, Logic As Boolean Dim K As Integer ReDim A(1) A(1) = 50
———————— 'K=1 For I = 51 To 149 K = 1 '位置错 Logic = False
Call Sub1(A, I, Logic) If Logic Then K = K + 1
ReDim A(K) 'ReDim Preserve A(K) A(K) = I End If Next I
For I = 1 To UBound(A)
Text1 = Text1 & Str$(A(I))
If I Mod 5 = 0 Then Text1 = Text1 & vbCrLf Next I End Sub
Private Sub Sub1(A() As Integer, N As Integer, F As Boolean) Dim I As Integer, J As Integer, Ub As Integer Ub = UBound(A) For I = 1 To Ub
For J = 2 To A(I)
If A(I) Mod J = 0 And N Mod J = 0 Then Exit For 'Exit Sub End If Next J F = True Next I End Sub
2、
Option Explicit Option Base 1
Private Sub Command1_Click()
Dim I As Integer, Flg As Boolean, S1 As Integer, S2 As Integer Dim J As Integer, K As Integer, P() As Integer ReDim P(1) P(1) = 2
For I = 3 To 150 Step 2 For J = 2 To Sqr(I)
If I Mod J = 0 Then Exit For Next J
If J > Sqr(I) Then
ReDim Preserve P(UBound(P) + 1)
P(UBound(P) + 1) = I 'P(UBound(P)) = I End If Next I
For I = 80 To 125
If fun(I, P, S1, S2) Then 'If fun(I, P, S1, S2)
Text1 = Text1 & Str$(I) & \ End If Next I End Sub
Private Function fun(N As Integer, P() As Integer, S1 As Integer, S2 As Integer) As Boolean Dim I As Integer, J As Integer For I = 1 To UBound(P)
For J = 1 To UBound(P) - 1 If N = P(I) * P(J) Then S1 = P(I) S2 = P(J)
'fun = True Exit Function fun = True '位置错 End If Next J Next I End Function 3、
Option Explicit
Private Sub Get_Chess(Chess() As Integer, idx As Integer, Over As Integer) 'ByVal idx As
Integer
Dim Ub As Integer, Counter As Integer, K As Integer Ub = UBound(Chess) Chess(idx) = 0 Counter = 1 K = 0 '位置错 Do Until Counter = Ub - 1 Do Until K = 2 idx = idx + 1
If idx > Ub Then idx = 1 K = K + Chess(idx) Loop
Chess(idx) = 0
Counter = Counter + 1 Loop
For Over = 1 To Ub
If Chess(Over) <> 0 Then Exit For Next Over End Sub
Private Sub Command1_Click()
Dim Chess(32) As Integer, I As Integer, J As Integer Dim K As Integer, idx As Integer For I = 1 To 32
For J = 1 To 32 Chess(J) = 1 Next J
Call Get_Chess(Chess, I, K) If K = 23 Then
Text1 = \从第\号棋子开始取\ Exit For End If Next I End Sub 5、
Option Explicit
Private Sub Form_Click()
Dim S As String, Flg As Boolean Do
S = InputBox(\输入一个自然数\ If S = \
MsgBox \请重新输入\
Else
Exit Do End If Loop
judge(S, Flg) ' Call judge(S, Flg) 或 judge S, Flg If Flg Then
Print S; \是回文数\ Else
Print S; \不是回文数\ End If End Sub
Private Sub judge(Ch As String, F As Boolean) Dim L As Integer, I As Integer L = Len(Ch) F = True ' F = False For I = 1 To L
If Mid$(Ch, I, 1) <> Mid$(Ch, L + 1 - I, 1) Then Exit Sub Next I F = False ' F = True End Sub
四、编程题
1、编程验证任一大于2的偶数可以表示成两个素数之和。 Private Function Prime(a As Integer) As Boolean Dim i As Integer For i = 2 To Sqr(a)
If a Mod i = 0 Then Exit Function Next i
Prime = True End Function
'方法一
Private Sub Command1_Click() Dim m As Integer, i As Integer
m = InputBox(\请输入一个大于2 的偶数\
If m <= 2 Or m Mod 2 <> 0 Then MsgBox \请输入大于2的偶数\ If Prime(2) And Prime(m - 2) Then Print m & \ Else Do
i = i + 1