五笔打字通主页
VB程序题:将斐波那契数列的前10项写入文件Fb .dat,然后从该文件将数据读取出来并计算合计和平均数,最后送入列表框。
要求:文件数据格式如下所示,列表框中项目格式如图H.3所示。
"Fib(0)",0
"Fib(1)",1
"Fib(2)",1
"Fib(3)",2
"Fib(4)",3
"Fib(5)",5
"Fib(6)",8
"Fib(7)",13
"Fib(8)",21
"Fib(9)",34
图H.3:
解题,画2个按钮,1个列表框控件,代码如下:
Private Sub Command1_Click()
Dim fib%(0 To 9), i%
Open "c:\fb.dat" For Output 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:\fb.dat", vbNormalNoFocus)
End Sub
Private Sub Command2_Click()
Dim st$, n%, sum%
Open "c:\fb.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 Sub
Visual Basic程序设计教程(第3版) (龚沛曾等编)课后实验源码
来源:济亨网
本文链接:http://wb86.com/post/108.html