使用顺序查找法,在一组数中查找某给定的数x。VB程序 编写个实例代码

使用顺序查找法,在一组数中查找某给定的数x。VB程序 编写个实例代码,第1张

Option Base 1

Private Function find(a() As Single, x As Single) As Integer

Dim n%, p%

n = UBound(a) '数组元素个数

For p = 1 To n '循环每个元素

If x = a(p) Then Exit For '如果找到相同 则退出循环 此时的P值既是结果

Next p

'如果没找到 P值将会是 N+1

If p >n Then p = 0

find = p

End Function

Private Sub Form_click()

Dim test(10) As Single

Dim x As Single

Randomize

For i = 1 To 10

test(i) = Int(Rnd * 10 + 1)

Next

x = 2 '

MsgBox find(test, x)

End Sub

Private Sub Form_Activate()

'1年期2.25%,2年期2.43%,3年期为2.70%,5年期为2.88%(不记复利)。今有X元,5年以后使用,共有如下6中存法:

Const x1 As Double = 0.0225, x2 As Double = 0.0243, x3 As Double = 0.027, x5 As Double = 0.0288

Dim x As Double

x = Val(InputBox(""))

Print "①存一次5年期,5年后到期的本息合计"ss(x, 5, x5)

Print "②存一次3年期,一次2年期,5年后到期的本息合计"ss(ss(x, 3, x3), 2, x2)

Print "③存一次3年期,两次1年期,5年后到期的本息合计"ss(ss(ss(x, 3, x3), 1, x1), 1, x1)

Print "④存两次2年期,一次1年期,5年后到期的本息合计"ss(ss(ss(x, 2, x2), 2, x2), 1, x1)

Print "⑤存一次2年期,三次1年期,5年后到期的本息合计"ss(ss(ss(ss(x, 2, x2), 1, x1), 1, x1), 1, x1)

Print "⑥存五次1年期,5年后到期的本息合计"ss(ss(ss(ss(ss(x, 1, x1), 1, x1), 1, x1), 1, x1), 1, x1)

End Sub

Private Function ss(ByVal a As Double, b As Integer, c As Double) As Double

ss = a + a * c * b

End Function

Private Sub Form_Load()

AutoRedraw = True

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/8081056.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存