简答题
VB
题号 #2542659
打开考生文件夹下的sj5.vbp,在名称为Form1的窗体上建立一个名为Text1的文本框和两个命令按钮,名称分别为Command1和Command2,命令按钮上分别显示"读入"和"排序"(如图3-6所示)。程序运行时,单击"读入"按钮,将"in.txt"文件中的50个数读入,单击"排序"则把数组a中的数值按照降序排列。请在有"?"号的地方填正确内容,然后删除"?"及代码前的所有注释符(即'号)但不能修改其他部分。


图3-6
注意:存盘时不得改变文件名和文件夹。
参考答案
暂无
答案解析
根据题意,在窗体上添加一个Text控件和两个Command控件,Text的名称为Text1、Text属性为空、MultiLine属性为True、ScrollBar属性为2,Command的名称分别为Command1和Command2、Caption属性分别为“读入”和“排序”。双击Command1命令按钮,编写如下代码:
Dim a(50) As Long '全局变量
Private Sub Command1_Click()
Text1.Text = ""
Open App.Path&"\in.txt" For Input As #1 '打开"in.txt"文件
Dim i As Integer
Dim temp As Integer
For i = 1 To 50
Input #1, temp
a(i) = temp '按顺序读入到数组中
Next i
For i = 1 To 50
Text1.Text = Text1.Text + CStr(a(i)) + vbCrLf '将数组中的数放入Text中显示
Next i
Close #1
End Sub
Private Sub Command2_Click()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim temp As Integer
Dim str As String
str = ""
For i = 1 To 50 '降序排列
temp = a(i)
For j = 1 To i
If a(j ) < temp Then '读取的数据比原位置的数据小则偏移
For k = i To j + 1 Step -1
a(k) = a(k - 1) '数据向后偏移一个
Next k
a(j) = temp
Exit For
End If
Next j
Next i
For i = 1 To 50
str = str + CStr(a(i)) + vbCrLf '将排序好的数组写入字符串中
Next i
Text1.Text = str '在Text1中显示
End Sub
单击
按钮运行程序,并按要求保存。
Dim a(50) As Long '全局变量
Private Sub Command1_Click()
Text1.Text = ""
Open App.Path&"\in.txt" For Input As #1 '打开"in.txt"文件
Dim i As Integer
Dim temp As Integer
For i = 1 To 50
Input #1, temp
a(i) = temp '按顺序读入到数组中
Next i
For i = 1 To 50
Text1.Text = Text1.Text + CStr(a(i)) + vbCrLf '将数组中的数放入Text中显示
Next i
Close #1
End Sub
Private Sub Command2_Click()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim temp As Integer
Dim str As String
str = ""
For i = 1 To 50 '降序排列
temp = a(i)
For j = 1 To i
If a(j ) < temp Then '读取的数据比原位置的数据小则偏移
For k = i To j + 1 Step -1
a(k) = a(k - 1) '数据向后偏移一个
Next k
a(j) = temp
Exit For
End If
Next j
Next i
For i = 1 To 50
str = str + CStr(a(i)) + vbCrLf '将排序好的数组写入字符串中
Next i
Text1.Text = str '在Text1中显示
End Sub
单击
按钮运行程序,并按要求保存。








