2007年(秋)上机 Visual Basic(VB02)答案
一、改错
【题目】本程序的功能是将混杂在字符串中的英文词汇挑出来,重组成正常的英文句子。 说明:输入的字符串以非英文字符结束。(程序界面参见右图) Option Explicit Option Base 1
Private Sub Command1_Click()
Dim st As String, words() As String, i As Integer st = LCase(Text1) Call choice(st, words) st = \
For i = 1 To UBound(words) - 1 'err UBound(words) st = st & words(i) & \ Next i
st = st & words(i) & \
Text2 = UCase(Left(st, 1)) & Mid(st, 2) End Sub
Private Sub choice(s As String, ws() As String) Dim st As String * 1, i As Integer, k As Integer Dim p As String For i = 1 To Len(s) st = Mid(s, i, 1)
'k = k + 1 '位置错 If st >= \ p = p & st ElseIf p <> \ k = k + 1
ReDim Preserve ws(k) 'err 无Preserve ws(k) = p p = \ End If Next i End Sub
二、编程题
【题目】编写程序,输入数据范围[m, n],求出[m, n]之间所有的素数,并按每行五个的形式输出到多行文本框中。 Option Explicit
Private Sub Command1_Click()
Dim m As Integer, n As Integer, st As String Dim i As Integer, k As Integer m = Text1: n = Text2
For i = m To n
If prime(i) Then k = k + 1
If k Mod 5 <> 0 Then st = st & Str(i) Else
st = st & Str(i) & vbCrLf End If End If Next i Text3 = st End Sub
Private Function prime(n As Integer) As Boolean Dim i As Integer For i = 2 To Sqr(n)
If n Mod i = 0 Then Exit Function Next i
prime = True End Function
Private Sub Command2_Click()
Text1 = \ Text1.SetFocus End Sub
Private Sub Command3_Click() End End Sub