文档库

最新最全的文档下载
当前位置:文档库 > VB代码

VB代码

一、判断素数

Option Explicit

Private Sub Command1_Click()

Dim i As Integer

For i = 100 To 300

If prime(i) Then List1.AddItem i

Next i

End Sub

Private Function prime(ByVal n As Integer) As Boolean Dim i As Integer

prime = False

For i = 2 To n - 1

If n Mod i = 0 Then Exit For

Next i

If i > n - 1 Then prime = True

End Function

二、求因子和

Option Explicit

Private Sub Command1_Click()

Dim n As Integer

n = Val(Text1.Text)

Text2.Text = CStr(yzh(n))

End Sub

Private Function yzh(ByVal n As Integer) As Integer Dim i As Integer

For i = 2 To n - 1

If n Mod i = 0 Then yzh = yzh + i

Next i

End Function

三、阶乘

Private Sub Command1_Click()

Dim i As Integer, n As Integer, s As Long

n = Val(Text1.Text)

For i = 2 To 2 * n Step 2

s = s + fact(i)

Next i

Text2.Text = CStr(s)

End Sub

Private Function fact(ByVal k As Integer) As Long

Dim i As Integer

fact = 1

Do

fact = fact * k

k = k - 1

Loop Until k = 1

End Function

四、升/降序数

升序数

Option Explicit

Private Sub Command1_Click()

Dim n As Integer, i As Integer

Randomize

For i = 1 To 20

n = Int(Rnd * 900) + 100

Text1.Text = Text1.Text & CStr(n) & " "

If i Mod 5 = 0 Then Text1.Text = Text1.Text & vbCrLf

If judge(n) Then List1.AddItem n

Next i

End Sub

Private Function judge(ByVal n As Integer) As Boolean Dim i As Integer, s As String

s = CStr(n)

judge = False

For i = 1 To Len(s) - 1

If Mid(s, i, 1) >= Mid(s, i + 1, 1) Then Exit Function Next i

judge = True

End Function

降序数

Option Explicit

Private Sub Command1_Click()

Dim n As Integer, i As Integer

Randomize

For i = 1 To 20

n = Int(Rnd * 900) + 100

Text1.Text = Text1.Text & CStr(n) & " "

If i Mod 5 = 0 Then Text1.Text = Text1.Text & vbCrLf

If judge(n) Then List1.AddItem n

Next i

End Sub

Private Function judge(ByVal n As Integer) As Boolean

Dim i As Integer, s As String

s = CStr(n)

judge = False

For i = 1 To Len(s) - 1

If Mid(s, i, 1) <= Mid(s, i + 1, 1) Then Exit Function Next i

judge = True

End Function

五、求数组元素之和

Option Explicit

Option Base 1

Private Sub Command1_Click()

Dim a(10) As Integer, i As Integer, k As Integer

For i = 1 To 10

a(i) = Int(Rnd * 90) + 10

Picture1.Print a(i);

Next i

Call total(a, k)

Text1.Text = CStr(k)

End Sub

Private Sub total(a() As Integer, sum As Integer)

Dim i As Integer

For i = 1 To UBound(a)

sum = sum + a(i)

Next i

End Sub

六、最大公约数

Option Explicit

Private Sub Command1_Click()

Dim m As Integer, n As Integer

m = Val(Text1.Text)

n = Val(Text2.Text)

Text3.Text = CStr(gcd(m, n))

End Sub

Private Function gcd(ByVal m As Integer, ByVal n As Integer) As Integer Dim r As Integer

Do

r = m Mod n

m = n

n = r

Loop Until r = 0

gcd = m

End Function

七、最小公倍数

法一

Option Explicit

Private Sub Command1_Click()

Dim m As Integer, n As Integer

m = Val(Text1.Text)

n = Val(Text2.Text)

Text3.Text = CStr(m * n / gcd(m, n))

End Sub

Private Function gcd(ByVal m As Integer, ByVal n As Integer) As Integer Dim r As Integer

Do

r = m Mod n

m = n

n = r

Loop Until r = 0

gcd = m

End Function

法二

Option Explicit

Private Sub Command1_Click()

Dim m As Integer, n As Integer

m = Val(Text1.Text)

n = Val(Text2.Text)

Text3.Text = CStr(lcd(m, n))

End Sub

Private Function lcd(ByVal m As Integer, ByVal n As Integer) As Integer Dim r As Integer, a As Integer

a = m * n

Do

r = m Mod n

m = n

n = r

Loop Until r = 0

lcd = a / m

End Function

八、逆序

法一、书P137

法二

Private Sub Command1_Click()

Dim i As Integer

For i = 1000 To 9999

If i = nx(i) Then List1.AddItem CStr(i) Next i

End Sub

Private Function nx(ByVal n As Integer) As Integer Dim st As String, i As Integer

Dim s As String

s = CStr(n)

For i = 1 To Len(s)

st = Mid(s, i, 1) & st

Next i

nx = Val(st)

End Function

法三

Option Explicit

Private Sub Command1_Click()

Dim i As Integer

For i = 1000 To 9999

If judge(i) Then List1.AddItem CStr(i) Next i

End Sub

Private Function judge(ByVal n As Integer) As Boolean Dim st As String, i As Integer

Dim s As String

judge = False

s = CStr(n)

For i = 1 To Len(s)

st = Mid(s, i, 1) & st

Next i

If s = st Then judge = True

End Function