关于VB中数组和文件读取操作的练习题代码
编写应用程序,要求如下:窗体上有两个文本框(Text1和Text2)都可以多行显示,还有3个命令按钮,标题分别为“取数”,“排序”,“存盘”。“取数”按钮的功能是把已有的...
编写应用程序,要求如下:窗体上有两个文本框(Text1和Text2)都可以多行显示,还有3个命令按钮,标题分别为“取数”,“排序”,“存盘”。“取数”按钮的功能是把已有的某.dat文件中的20个整数读到数组中,并在Text1中显示出来;“排序”按钮是对这20个整数安升序排列,并在Text2中显示出来;“排序”按钮的功能是对这20个整数按升序排列,并在Text2中显示出来;“存盘”按钮的功能是把排好序的20个整数存到某个.dat文件中。
展开
1个回答
展开全部
Dim a(1 To 20) As Integer '装20个数的数组
Private Sub command1_click() '取数
Dim i%
Open "***.dat" For Input As #1
For i = 1 To 20
Input #1, a(i)
Text1.Text = Text1.Text & a(i) & vbCrLf 'text1设置为多行显示
Next
Close #1
End Sub
Private Sub command2_click() '开始排序
Dim i%, j%, tmp%
For i = 1 To 19
For j = i + 1 To 20
If a(i) > a(j) Then
tmp = a(i)
a(i) = a(j)
a(j) = tmp
End If
Next j
Next i
For i = 1 To 20
Text2.Text = Text2.Text & a(i) & vbCrLf 'text2设置为多行显示
Next
End Sub
Private Sub command3_click() '存盘
Dim i%
Open "你想存的路径.dat" For Output As #1
For i = 1 To 20
Print #1, a(i)
Next
Close #1
End Sub
Private Sub command1_click() '取数
Dim i%
Open "***.dat" For Input As #1
For i = 1 To 20
Input #1, a(i)
Text1.Text = Text1.Text & a(i) & vbCrLf 'text1设置为多行显示
Next
Close #1
End Sub
Private Sub command2_click() '开始排序
Dim i%, j%, tmp%
For i = 1 To 19
For j = i + 1 To 20
If a(i) > a(j) Then
tmp = a(i)
a(i) = a(j)
a(j) = tmp
End If
Next j
Next i
For i = 1 To 20
Text2.Text = Text2.Text & a(i) & vbCrLf 'text2设置为多行显示
Next
End Sub
Private Sub command3_click() '存盘
Dim i%
Open "你想存的路径.dat" For Output As #1
For i = 1 To 20
Print #1, a(i)
Next
Close #1
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询