用vb做一个斐波那契数列程序
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/17 21:16:31
用vb做一个斐波那契数列程序
用vb做一个斐波那契数列程序
用vb做一个斐波那契数列程序
Option Explicit
Dim FileName As String '文件名变量
Private Sub Form_Load()
Dim FilePath As String
FilePath = App.Path
If Right(FilePath, 1) "\" Then
FilePath = FilePath & "\"
End If
FileName = FilePath & "fb.txt"
End Sub
Private Sub Command1_Click() '建立文件
Dim i As Integer
Dim ifilenum As Integer
ifilenum = FreeFile '获得可用文件号
Open FileName For Output As #ifilenum '以输出方式打开文件
For i = 1 To 15
Write #ifilenum, fb(i) '写入文件
Next
Close #ifilenum
End Sub
Private Function fb(ByVal n As Integer) As Long '求斐波那契数列函数
If n = 1 Or n = 2 Then
fb = 1
Else
fb = fb(n - 1) + fb(n - 2)
End If
End Function
Private Sub Command2_Click() '读出文件到列表框
Dim n As Long, n1 As Long
Dim i As Integer
Dim ifilenum As Integer
ifilenum = FreeFile
Open FileName For Input As #ifilenum '以输入方式打开文件
Do While Not EOF(1)
Input #ifilenum, n1 '从文件读取
n = n + n1
i = i + 1
Loop
Close #ifilenum
'添加到List1
List1.AddItem "合计:" & n
List1.AddItem "平均数:" & n / i
End Sub