VB程序设计教程(第四版)龚沛曾-实验8-2

实验8-2

将斐波那契数列的前10项写入文件Fb .dat,然后从该文件将数据读取出来并计算合计和平均数,最后送入列表框。

要求:文件数据格式如2.8.2所示,列表框中项目格式如图2.8.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

你可能感兴趣的:(学习,课后习题答案)