简答题
VB
题号 #2537291
在考生文件夹中有工程文件sj5.vbp及其窗体文件sj5.frm,该程序是不完整的,请在有"?"的地方填入正确内容,然后删除"?"及代码前的所有注释符(即'号)但不能修改其他部分。存盘时不得改变文件名和文件夹。
本题描述如下:
在名称为 Form1的窗体上有一个文本框,名称为Text1,MultiLine属性为True,ScrollBars属性为2;两个命令按钮,名称分别为Command1和Command2、标题分别为"读入数据"和"计算保存",运行界面如图1-5所示。要求程序运行后,如果单击"读入数据"按钮,则读入"in.txt"文件中的50个整数,放入一个数组中(数组下界为1),同时在文本框中显示出来;如果单击"计算保存"按钮,则先进行升序排列,计算前25个数的和与后25个数的和的差,并把求得的结果在文本框Text2 中显示出来,同时把该结果存入考生文件夹中的文件"out.txt"中。(在考生文件夹下的标准模块mode.bas中的writedata过程可以把结果存入指定的文件)


图1-5
注意:文件必须存放在考生文件夹下,窗体文件名为sj5.frm,工程文件名为sj5.vbp,计算结果存入out.txt文件,否则没有成绩。
参考答案
暂无
答案解析
根据题意,将两个Text控件和两个Command控件添加到窗体上,Text的名称分别为Text1和Text2,Text属性都为空,其中Text1的MultiLine属性为True,ScrollBar属性为2,Command的名称分别为Command1和Command2,Caption属性分别为"读入数据"和J"计算保存"。双击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
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
Dim total1 As Long
Dim total2 As Long
total1 = 0
total2 = 0
For i = 1 To 25
total1 = total1 + a(i) '计算前25个数据的和
Next i
For i = 26 To 50
total2 = total2 + a(i) '计算后25个数据的和
Next i
Text2.Text = total1 - total2
Writedata(total1 - total2)
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
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
Dim total1 As Long
Dim total2 As Long
total1 = 0
total2 = 0
For i = 1 To 25
total1 = total1 + a(i) '计算前25个数据的和
Next i
For i = 26 To 50
total2 = total2 + a(i) '计算后25个数据的和
Next i
Text2.Text = total1 - total2
Writedata(total1 - total2)
End Sub
单击
按钮运行程序,并按要求保存。