vb 斐波那契数列问题……急.
来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/11/07 17:56:59
vb 斐波那契数列问题……急.
将斐波那契数列的前15项写入文件fb.txt,然后从该文件中将数据读取出来并计算合计和平均数,最后送入列表框.
就直接全都显示在一个框里就行了,先是前十五个数,再是和,最后是 均值在 您只给我代码就可以了,框框我自己做…… 哈哈 遇见高手 了!太高兴了~
将斐波那契数列的前15项写入文件fb.txt,然后从该文件中将数据读取出来并计算合计和平均数,最后送入列表框.
就直接全都显示在一个框里就行了,先是前十五个数,再是和,最后是 均值在 您只给我代码就可以了,框框我自己做…… 哈哈 遇见高手 了!太高兴了~
Option Explicit
'产生斐波那契数列并保存到D:\fb.txt
Private Sub Command1_Click()
Dim FB(1 To 15) As Integer
Dim i As Integer
'产生斐波那契数列前15项
FB(1) = 1
FB(2) = 1
For i = 3 To 15
FB(i) = FB(i - 1) + FB(i - 2)
Next
'保存到D盘fb.txt
Open "d:\fb.txt" For Output As #1
For i = 1 To 15
Print #1,FB(i)
Next
Close #1
MsgBox "斐波那契数列产生完毕并保存" '不想要这句可以删掉
End Sub
'从D:\fb.txt读取斐波那契数列并计算输出
Private Sub Command2_Click()
Dim FB(1 To 15)
Dim i As Integer
Dim Sum As Double
Dim PingJun As Double
'读取D盘fb.txt
If Dir("d:\fb.txt") = "" Then MsgBox "你还没有生成d:\fb.txt文件":Exit Sub
Open "d:\fb.txt" For Input As #1
For i = 1 To 15
Line Input #1,FB(i)
Next
Close #1
'计算总和
For i = 1 To 15
Sum = Sum + FB(i)
Next
'计算平均值
PingJun = Sum / 15
'将读出的数据输出到列表框
List1.Clear
For i = 1 To 15
List1.AddItem FB(i)
Next
List1.AddItem "和为" & Str(Sum)
List1.AddItem "平均值为" & Str(PingJun)
End Su
'产生斐波那契数列并保存到D:\fb.txt
Private Sub Command1_Click()
Dim FB(1 To 15) As Integer
Dim i As Integer
'产生斐波那契数列前15项
FB(1) = 1
FB(2) = 1
For i = 3 To 15
FB(i) = FB(i - 1) + FB(i - 2)
Next
'保存到D盘fb.txt
Open "d:\fb.txt" For Output As #1
For i = 1 To 15
Print #1,FB(i)
Next
Close #1
MsgBox "斐波那契数列产生完毕并保存" '不想要这句可以删掉
End Sub
'从D:\fb.txt读取斐波那契数列并计算输出
Private Sub Command2_Click()
Dim FB(1 To 15)
Dim i As Integer
Dim Sum As Double
Dim PingJun As Double
'读取D盘fb.txt
If Dir("d:\fb.txt") = "" Then MsgBox "你还没有生成d:\fb.txt文件":Exit Sub
Open "d:\fb.txt" For Input As #1
For i = 1 To 15
Line Input #1,FB(i)
Next
Close #1
'计算总和
For i = 1 To 15
Sum = Sum + FB(i)
Next
'计算平均值
PingJun = Sum / 15
'将读出的数据输出到列表框
List1.Clear
For i = 1 To 15
List1.AddItem FB(i)
Next
List1.AddItem "和为" & Str(Sum)
List1.AddItem "平均值为" & Str(PingJun)
End Su