VB常用算法代码 下载本文

内容发布更新时间 : 2024/11/8 11:06:25星期一 下面是文章的全部内容请认真阅读。

Case 3: Opt = \End Select

Label1.Caption = A Label2.Caption = Opt Label3.Caption = B Label4.Visible = True Text1.Visible = True Text1.SetFocus End Sub

Private Sub Command2_Click() Dim Result As Integer Select Case Opt

Case \ Case \ Case \ Case \End Select

If Result = Val(Text1.Text) Then MsgBox \很好,你做对了!\

Picture1.Print A & Opt & B & \ V\ Correct = Correct + 1 Else

MsgBox \好可惜,你错了!\

Picture1.Print A & Opt & B & \ X\ Wrong = Wrong + 1 End If

End Sub

Private Sub Command3_Click()

MsgBox (\你做对了\题,做错了\题\End Sub

Private Sub Form_Activate() Command1.SetFocus Label4.Visible = False Text1.Visible = False

End Sub

打印ASIIC码

Private Sub Form_Click()

Dim I As Integer, N As Integer N = 1

For I = 32 To 126

Print I; \ If N = 5 Then N = 1 Print Else

N = N + 1 End If Next End Sub

递归法Feb数列前10项 Private Sub Form_Click() Dim I As Integer For I = 1 To 10 Print Feb(I) Next

End Sub

Private Function Feb(ByVal N As Integer) As Integer If N = 1 Then Feb = 0

ElseIf N = 2 Then Feb = 1 Else

Feb = Feb(N - 1) + Feb(N - 2) End If

End Function

递归法逆转输出字符串 Private Sub Form_Click() Dim S As String

S = InputBox(\Invert S End Sub

Public Sub Invert(ByVal S As String) Dim N As Integer N = Len(S) If N = 0 Then Exit Sub Else

Print Mid(S, N, 1); S = Mid(S, 1, N - 1) Invert S End If End Sub

递归法求最大公约数 Private Sub Form_Click()

Dim M As Integer, N As Integer M = InputBox(\N = InputBox(\

Print M; N; \的最大公约数为:\

End Sub

Public Function Gcd(ByVal M As Integer, ByVal N As Integer) As Integer Dim R As Integer R = M Mod N If R = 0 Then Gcd = N Else

M = N N = R

Gcd = Gcd(M, N) End If

End Function

计算两个矩阵的积 Option Base 1

Private Sub Form_Click()

Dim A(3, 4) As Integer, B(4, 3) As Integer, C(3, 3) As Integer Dim I As Integer, J As Integer, K As Integer Print \矩阵A为:\For I = 1 To 3

For J = 1 To 4

A(I, J) = 5 * Rnd Print A(I, J), Next Print Next

Print \矩阵B为:\For I = 1 To 4

For J = 1 To 3

B(I, J) = 5 * Rnd Print B(I, J), Next Print Next

Print \矩阵C为:\For I = 1 To 3

For J = 1 To 3 C(I, J) = 0 For K = 1 To 4

C(I, J) = C(I, J) + A(I, K) * B(K, J) Next

Print C(I, J), Next Print Next End Sub

冒泡法排序 Option Base 1

Private Sub Form_Click()

Dim A(10) As Integer, I As Integer, J As Integer, Temp As Integer For I = 1 To 10

A(I) = Int(90 * Rnd) + 10 Print A(I); Next Print

For I = 9 To 1 Step -1 For J = 1 To I

If A(J) > A(J + 1) Then Temp = A(J) A(J) = A(J + 1) A(J + 1) = Temp End If

Next Next

For I = 1 To 10 Print A(I); Next End Sub

逆序输出字符串

Private Sub Form_Click() Dim C As String, I As Integer

C = InputBox(\请输入一个字符串\Print C

For I = 1 To Len(C)

Print Mid(C, Len(C) - I + 1, 1); Next Print End Sub

牛顿迭代法

Private Sub Command1_Click()

Dim x As Single, x1 As Single, Eps As Single x = InputBox(\输入X的初始值\牛顿迭代法\Eps = InputBox(\输入允许的误差\牛顿迭代法\Do

x1 = x

x = x1 - (x1 * Exp(x1) - 1) / (Exp(x1) * (x1 + 1)) Loop Until Abs(x - x1) <= Eps Text1.Text = Str(x)

End Sub

求定积分

Private Sub Form_Click()

Dim A As Single, B As Single, N As Long Dim S As Double, H As Double, I As Long N = InputBox(\A = 0 B = 1

H = (B - A) / N S = 0

For I = 0 To N

S = S + (A + I * H) * H Next