vb实验H__数据.doc

上传人:cl****1 文档编号:544877386 上传时间:2023-12-20 格式:DOC 页数:14 大小:131.51KB
返回 下载 相关 举报
vb实验H__数据.doc_第1页
第1页 / 共14页
vb实验H__数据.doc_第2页
第2页 / 共14页
vb实验H__数据.doc_第3页
第3页 / 共14页
vb实验H__数据.doc_第4页
第4页 / 共14页
vb实验H__数据.doc_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《vb实验H__数据.doc》由会员分享,可在线阅读,更多相关《vb实验H__数据.doc(14页珍藏版)》请在金锄头文库上搜索。

1、实验H 数据文件一、实验目的1掌握顺序文件、随机文件及二进制文件的特点和使用。2掌握各类文件的打开、关闭和读/写操作。3学会在应用程序中使用文件。二、实验内容 1编写如图H1所示的应用程序。若单击“建立文件”按钮,则分别用Print#和Write#语句将三个同学的学号、姓名和成绩写入文件Scoredat和Scoreldat;若单击“读取文件”按钮,则用Line Input语句按行将两个文件中的数据送往相应的文本框。要求:学号和姓名是字符串类型,成绩是整型。Private Sub Command1_Click() Open C:UsersAdministratorDesktop第八章H1scor

2、e For Output As #1 Print #1, 051023, 王海涛, 66 Print #1, 052498, 周文英, 88 Print #1, 050992, 陈建东, 77 Open C:UsersAdministratorDesktop第八章H1score1 For Output As #2 Write #2, 051023, 王海涛, 66 Write #2, 052498, 周文英, 88 Write #2, 050992, 陈建东, 77 Close i1 = Shell(NOTEPAD.exe + c:score, vbNormalNoFocus) i2 = Sh

3、ell(NOTEPAD.exe + c:score1, vbNormalNoFocus)End SubPrivate Sub Command2_Click() Dim no As String, name As String, s As Integer Open C:UsersAdministratorDesktop第八章H1score For Input As #1 Do While Not EOF(1) Line Input #1, linedata List1.AddItem linedata Loop Open C:UsersAdministratorDesktop第八章H1score

4、1 For Input As #2 Do While Not EOF(2) Input #2, no, name, s List2.AddItem no & name & s Loop Close End Sub 2将斐波那契数列的前1 0项写入文件Fb .dat,然后从该文件将数据读取出来并计算合计和平均数,最后送入列表框。要求:文件数据格式如图H2所示,列表框中项目格式如图H3所示。Private Sub Command1_Click() Dim fib%(0 To 9), i% Open C:UsersAdministratorDesktop第八章H2Fb.dat For Output

5、As #1 For i = 0 To 9 If i = 0 Or i = 1 Then fib(i) = i Else fib(i) = fib(i - 1) + fib(i - 2) End If Print #1, Fib( & i & ), & fib(i) Next i Close #1 i = Shell(NOTEPAD.exe + C:UsersAdministratorDesktop第八章H2Fb.dat, vbNormalNoFocus) Close #1End SubPrivate Sub Command2_Click() Dim st$, n%, sum% Open C:U

6、sersAdministratorDesktop第八章H2Fb.dat For Input As #1 Do While Not EOF(1) Input #1, st, n sum = sum + n List1.AddItem st & = & n Loop Close #1 List1.AddItem 合计: & sum List1.AddItem 平均: & sum / 10 End Sub3设计一个如图H4所示的应用程序。要求: 单击“打开文件”按钮弹出一个通用对话框,选择文件后显示在文本框中。 单击“保存文件”按钮后弹出通用对话框,确定文件名后保存。 单击“查找下一个”按钮后在文本

7、文件中查找单词“VB”,找到后以高亮度显示。若再单击“查找下一个”按钮,则继续查找。Private Sub Command1_Click() CommonDialog1.Action = 1 Text1.Text = Open CommonDialog1.FileName For Input As #1 Do While Not EOF(1) Line Input #1, inputdata Text1.Text = Text1.Text + inputdata + vbCrLf Loop Close #1End SubPrivate Sub Command2_Click() CommonDi

8、alog1.FileName = Default.Txt CommonDialog1.DefaultExt = Txt CommonDialog1.Action = 2 Open CommonDialog1.FileName For Output As #1 Print #1, Text1.Text Close #1 End SubPrivate Sub Command3_Click() Static j% Text1.SetFocus j = InStr(j + 1, Text1, VB) If j 0 Then Text1.SelStart = j - 1 Text1.SelLength

9、= 2 j = j + 1 Else MsgBox 找不到 End IfEnd SubPrivate Sub Command4_Click() EndEnd Sub 4编写一个随机文件程序。 要求: 建立一个具有5个学生的学号、姓名和成绩的随机文件( Random. dat)。 读出Randomdat文件中的内容,然后按成绩排序,最后按顺序写入另一个随机文件( Randomldat)。 再一次读出文件的内容,按文件中的顺序将学生的信息显示在屏幕上,检查正确性。Private Type Studtype no As String * 4 name As String * 6 mark As Si

10、ngleEnd TypeDim Student As Studtype, Stud(1 To 5) As Studtype, t As StudtypePrivate Sub Command1_Click() Open C:UsersAdministratorDesktop第八章H4score.dat For Random As #1 Len = Len(Student) With Student .no = 0001 .name = 小王 .mark = 66 End With Put #1, 1, Student With Student .no = 0002 .name = 俊杰 .ma

11、rk = 99 End With Put #1, 2, Student With Student .no = 0003 .name = 勇气 .mark = 88 End With Put #1, 3, Student With Student .no = 0004 .name = 光明 .mark = 55 End With Put #1, 4, Student With Student .no = 0005 .name = 加油 .mark = 77 End With Put #1, 5, Student Close #1 End SubPrivate Sub Command2_Click() Open C:UsersAdministratorDesktop第八章H4score.dat For Random As #1 Len = Len(Student) For i = 1 To 5 Get #1, i, Student Print Student.no, Student.name, Student.mark Stud(i) = Student Next i Close #1 For i = 1 To 5 For j = i + 1 To 5 If Stud(i).mark Stud(j).mark Then t = Stud(i): Stud(i

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号