VB总结1、阶乘和Private Function fact(n As Integer) As Long Dim i As Integerfact = 1For i = 1 To nfact = fact * iNext iEnd FunctionPrivate Sub Command1_Click()Dim a As Integer, sum As LongDim i As Integern = Val(Text1.Text)For i = 1 To nsum = sum + fact(i)Next iText2.Text = sumEnd Sub2、逆序Private Sub reverse(s As String, s1 As String) Dim i As IntegerFor i = 1 To Len(s)s1 = Mid(s, i, 1) & s1Next iEnd SubPrivate Sub Command1_Click()Dim m As String, m1 As Stringm = Text1.TextCall reverse(m, m1)Text2.Text = m1End Sub3、最小公倍数Private Sub Command1_Click()Dim s1 As Integer, s2 As Integers1 = Val(Text1.Text)s2 = Val(Text2.Text)Text3.Text = s1 * s2 / gcd(s1, s2)End SubPrivate Function gcd(ByVal m As Integer, ByVal n As Integer) As Integer Dim r As IntegerDor = m Mod nm = nn = rLoop Until r = 0gcd = mEnd Function4、找出100-200之间的升序数Private Function ascnum(n As Integer) As BooleanDim i As IntegerFor i = 2 To Len(CStr(n))If Mid(n, i, 1) <=Mid(n, i - 1, 1) ThenExit FunctionEnd IfNext iascnum = TrueEnd FunctionPrivate Sub Command1_Click()Dim i As IntegerFor i = 100 To 200If ascnum(i) Then List1.AddItem iNext iEnd Sub5、找出100-300素数及求各个位数之和也是素数Private Function nsum(n As Integer) As IntegerDim i As IntegerFor i = 1 To Len(CStr(n))nsum = nsum + Mid(n, i, 1)Next iEnd FunctionPrivate Function prime(n As Integer) As BooleanDim i As Integerprime = FalseFor i = 2 To Sqr(n)If n Mod i = 0 Then Exit FunctionNext iprime = TrueEnd FunctionPrivate Sub Command1_Click()Dim i As IntegerFor i = 100 To 300If prime(i) And prime(nsum(i)) ThenList1.AddItem iEnd IfNext iEnd Sub6、质因子Private Sub Command1_Click()Dim n As Integern = Val(Text1.Text)Text2.Text = n & "=" & zyz(n)End SubPrivate Function zyz(ByVal n As Integer) As String Dim k As Integerzyz = " "k = 2Do Until n = 1If n Mod k = o Thenzyz = zyz & k & "*"n = n / kElsek = k + 1End IfLoopzyz = Left(zyz, Len(zyz) - 1)End Function因子和Private Sub Command1_Click()Dim n As Integern = Val(Text1.Text)Text2.Text = yz(n)End SubPrivate Function yz(ByVal n As Integer) As Integer Dim i As IntegerFor i = 1 To nIf n Mod i = o Then yz = yz + iNext iEnd Function7、生成由个位数组成的四行五列的数组Option Base 1 Private Sub Command1_Click()Dim a(4, 5) As Integer, i As Integer, j As IntegerFor i = 1 To 4For j = 1 To 5a(i, j) = Int(Rnd * 10)Print a(i, j);Next jPrintNext iEnd Sub8、由个位数组成的五行五列的矩阵,并显示下面一个三角形Option Base 1Dim a(5, 5) As Integer Private Sub Command1_Click()Dim i As Integer, j As IntegerFor i = 1 To 5For j = 1 To 5a(i, j) = Int(Rnd * 10)Picture1.Print a(i, j);Next jPicture1.PrintNext iEnd Sub Private Sub Command2_Click()Dim i As Integer, j As IntegerFor i = 1 To 5For j = 1 To iPicture2.Print a(i, j);Next jPicture2.PrintNext iEnd Sub9、由个位数组成的五行五列的矩阵,并沿主对角线对折Option Base 1Dim a(5, 5) As Integer Private Sub Command1_Click()Dim i As Integer, j As IntegerFor i = 1 To 5For j = 1 To 5a(i, j) = Int(Rnd * 10)Picture1.Print a(i, j);Next jPicture1.PrintNext iEnd Sub Private Sub Command2_Click()Dim i As Integer, j As Integer, tmp As IntegerFor i = 1 To 5For j = 1 To itmp = a(i, j)a(i, j) = a(j, i)a(j, i) = tmpNext jNext iFor i = 1 To 5For j = 1 To 5Picture2.Print a(i, j);Next jPicture2.PrintNext iEnd Sub10、生成12个二位数,并打印出最小元素和最大元素Option Base 1 Private Sub Command1_Click()Dim com(12) As Integer, i As IntegerDim max As Integer, min As Integermin = 100For i = 1 To 12com(i) = 10+Int(Rnd * 90)Print com(i);If com(i) > max Then max = com(i)If com(i) < min Then min = com(i)Next iPrintPrint "最大元素:" & maxPrint "最小元素:" & minEnd Sub11、实验教程P56Option ExplicitOption Base 1Dim a(5, 5) As IntegerPrivate Sub Command1_Click()Dim i As Integer, j As IntegerFor i = 1 To 5For j = 1 To 5a(i, j) = Int(Rnd * 90) + 10Picture1.Print a(i, j);Next jPicture1.PrintNext iEnd SubPrivate Sub Command2_Click()Dim i As Integer, j As Integer, sum As IntegerFor i = 1 To 5sum = 0For j = 1 To 5sum = sum + a(i, j)Next jText1.Text = Text1.Text & sum & vbCrLf Next iEnd SubPrivate Sub Command3_Click()Dim i As Integer, j As Integer, sum As IntegerFor j = 1 To 5sum = 0For i = 1 To 5sum = sum + a(i, j)Next iText2.Text = Text2.Text & sum & " "Next jEnd SubPrivate Sub Command4_Click()Dim i As Integer, j As Integer, sum As IntegerFor i = 1 To 5For j = 1 To 5If i = j Or i + j = 6 Then sum = sum + a(i, j) Next jNext iText3.Text = sumEnd Sub12、求任意一维数组元素之和Private Function tim(a() As Integer)Dim t As Double, i As Integert = 0For i = LBound(a) To UBound(a)t = t + a(i)Next itim = tEnd Function13、排序(升序)Option Base 1Private Sub Command1_Click()Dim i As Integer, b(10) As IntegerFor i = 1 To 10b(i) = 10 + Int(Rnd * 90)Print b(i)Next iPrintCall ascsort(b)For i = 1 To 10Print b(i)Next iEnd Sub冒泡法Private Sub ascsort(a() As Integer)Dim i As Integer, j As Integer, temp As IntegerFor i = 1 To UBound(a) - 1For j = 1 To UBound(a) - iIf a(j) > a(j + 1) Thentemp = a(j)a(j) = a(j + 1)a(j + 1) = tempEnd IfNext jNext iEnd Sub选择法Private Sub ascsort(a() As Integer)Dim i As Integer, j As Integer, temp As IntegerFor i = 1 To UBound(a)For j = i + 1 To UBound(a)If a(i) > a(j) Thentemp = a(i)a(i) = a(j)a(j) = tempEnd IfNext jNext iEnd Sub直接排序Private Sub ascsort(a() As Integer)Dim i As Integer, j As Integer, temp As Integer, pointer As Integer For i = 1 To UBound(a) - 1pointer = iFor j = i + 1 To UBound(a)If a(pointer) > a(j) Then pointer = jNext jIf i <> pointer Thentemp = a(i)a(i) = a(pointer)a(pointer) = tempEnd IfNext iEnd Sub1、I递归法阶乘:Private Function Fact(ByVal n As Integer) As LongDim i As IntegerFact = 1For i = 1 To nFact = Fact * iNext iEnd FunctionPrivate Sub Command1_Click()Dim a As Integera = Text1.TextText2.Text = Fact(a)End SubII非递归法阶乘Private Function Fact(ByVal n As Integer) As Long If n = 0 Or n = 1 ThenFact = 1ElseFact = n * Fact(n - 1)End IfEnd FunctionPrivate Sub Command1_Click()Dim a As Integera = Text1.TextText2.Text = Fact(a)End Sub2、最大公约数:Private Sub Command1_Click()Dim x As Integer, y As Integer, z As Integerx = Val(Text1.Text)y = Val(Text2.Text)z = gcd(x, y)Text3.Text = zEnd SubPrivate Function gcd(ByVal m As Integer, ByVal n As Integer) As IntegerDim r As IntegerDor = m Mod nm = nn = rLoop Until r = 0gcd = mEnd Function3、最小公倍数:Private Sub Command1_Click()Dim x As Integer, y As Integer, z As Integerx = Val(Text1.Text)y = Val(Text2.Text)z = gcd(x, y)Text3.Text = zEnd SubPrivate Function gcd(ByVal m As Integer, ByVal n As Integer) As IntegerDim r As Integer, s As Integers = m * nDor = m Mod nm = nn = rLoop Until r = 0gcd = s / mEnd Function4、逆序:Private Sub Command1_Click()Dim i As Integer, st As Stringi = Val(Text1.Text)Call prime(i, st)Text2.Text = stEnd SubPrivate Sub prime(ByVal n As Integer, st As String) Dim i As Integer, s As Strings = CStr(n)For i = 1 To Len(s)st = Mid(s, i, 1) & stNext iEnd Sub5、素数(判断素数):Private Sub Command1_Click()Dim n As Integern = Val(Text1.Text)If ptm(n) ThenText2.Text = "该数是素数"ElseText2.Text = "该数不是素数"End IfEnd SubPrivate Function ptm(n As Integer) As Boolean Dim i As IntegerFor i = 2 To Sqr(n)If n Mod i = 0 Then Exit ForNext iIf i > Sqr(n) Thenptm = TrueEnd IfEnd Function6、素数(求素数):Private Sub Command1_Click()Dim i As IntegerFor i = 100 To 999If Prime(i) = True Then List1.AddItem iNext iEnd SubPrivate Function Prime(n As Integer) As Boolean Dim i As IntegerFor i = 2 To n - 1If n Mod i = 0 Then Exit FunctionNext iPrime = TrueEnd Function7、数组求和Option ExplicitOption Base 1Dim a () As IntegerPrivate Sub Command1_Click()Dim i As Integer ,n As IntegerN=Val (inputBox(“请输入n的值:”))ReDim a(n)For i=1 To NA(i) =Int (Rnd*90)+10Picture 1.Print a(i)Next iEnd SubPrivate Sub total (a() As Integer,sum As Integer) Dim i As IntegerFor i =1 To UBound(a)sum=sum+a(i)Next iEnd SubPrivate Sub Command 2_Click()Dim k As Integercall total (a,k)Text1.Text=CStr(k)End Sub8、升(降)序Private Sub Command1_Click()Dim n As Integern = Val(Text1.Text)If ascnum(n) ThenText2.Text = "该数是升(降)序数"ElseText2.Text = "该数不是升(降)序数"End IfEnd SubPrivate Function ascnum(n As Integer) As Boolean Dim s As Integer, i As Integers = CStr(n)For i = 1 To Len(s) - 1If Mid(s, i, 1) >(<)= Mid(s, i + 1, 1) Then Exit For Next iIf i > Len(s) - 1 Then ascnum = TrueEnd Function9、因子和:Option ExplicitPrivate Sub Command1_Click()Dim n As Integern = Val (Text1)Text2 = cstr (yzh(n))End SubPrivate Function yzh (ByVal n As Integer) As Integer Dim i As IntegerFor i = 1 To nIf n mod i = 0 Then yzh=yzh+iNext iEnd Function。