【正确答案】分析:在窗体上建立好控件后,先设置控件属性,再编写事件过程。
文本框显示的内容由Text属性设置:按钮的标题由Caption属性没置,单击命令按钮触发Click事件:在本题中涉及到文件的操作,读入顺序文件以顺序的方式打开,用Input#语句读取数据,另外需要注意的是对文件操作完后,一定要关闭文件。解题步骤:
第一步:建立界面并没置控件属性。程序中用到的控件及其属性见表4-4。
[*]
第二步:编写程序代码。
程序提供的代码。
标准模块代码:
Sub putdata(a() As Integer, n As Integer)
Dim sFile As String
sFile = "/result.txt"
Open App. Path& sFile For Output As #1
For i = 1 To n
Print #1, a(i) ;
Next
Close #1
End Sub
窗体代码:
Option Explicit
Dim i(1 To 100) As Integer
Private Sub Cmd1_Click()
Dim j As Integer
Open "in.txt" For Input As #1
For j = 1 To 100
' ? #1, i(j)
Text1.Text = Text1.Text & i(j) & Space(5)
Next
Close #1
End Sub
Private Sub Cmd2_Click()
Dim j As Integer
Dim k As Integer
Dim temp As Integer
Dim flag As Boolean
For j = 1 To 100
'flag = ?
For k = 1 To 100 - j
If i(k) > i(k + 1) Then
temp = i (k)
i(k) = i(k + 1)
i(k + 1) = temp
flag = True
End If
Next
If Not flag Then
Exit For
End If
Next
'Text1.? = ""
For j = 1 To 100
Text1.Text = Text1.Text & i(j) & Space(5)
Next
putdata i, 100
End Sub
参考代码:
Option Explicit
Dim i(1 To 100) As Integer
Private Sub Cmd1_Click()
Dim j As Integer
Open "in.txt" For Input As #1
For j = 1 To 100
Input #1, i(j)
Text1.Text = Text1.Text & i(j & Space(5)
Next
Close #1
End Sub
Private Sub Cmd2_Click()
Dim j As Integer
Dim k As Integer
Dim temp As Integer
Dim flag As Boolean
For j = 1 To 100
flag = False
For k = 1 To 100 - j
If i(k) > i(k + 1) Then
temp = i (k)
i(k) = i(k + 1)
i(k + 1) = temp
flag = True
End If
Next
If Not flag Then
Exit For
End If
Next
Text1.Text = ""
For j = 1 To 100
Text1.Text = Text1.Text & i(j) & Space(5)
Next
putdata i, 100
End Sub
第三步:调度并运行程序。
第四步:按题目要求存盘。
【答案解析】