《VB二级期末编程题.docx》由会员分享,可在线阅读,更多相关《VB二级期末编程题.docx(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、一、一维数组求和:Private Sub Commandl Click ()Dim a(10) As Integer, i As Integer, s As Integer RandomizeFor i = 1 To 10a(i) = Int (Rnd * 100 + 1)Textl.Text = Textl.Text & a(i) & Next iCall sum(a, s)Text2.Text = CStr(s)End SubPublic Sub sum(a() As Integer, s As Integer)Dim i As IntegerFor i = 1 To UBound(a)
2、s = s + a(i)Next iEnd Sub二、素数.找出100以内的挛生素数Private Sub Command1 Click()Dim i As IntegerFor i = 2 To 97If prime(i)= True And prime(i + 2)= True Then Listl. Additem i & 和 & i + 2End IfNext iEnd SubPublic Function prime(n As Integer) As BooleanDim i As IntegerFor i = 2 To sqr (n)If n Mod i = 0 Then Exit
3、 ForNext iIf i n - 1 Then(If i Len(CStr (i) Then Listl. Addltem i End IfNext iEnd SubPublic Function prime(n As Integer) As BooleanDim i As IntegerFor i = 2 To sqr (n)If n Mod i = 0 Then Exit ForNext iIf i nIf i nIf i n-1 Then=Trueprime End If End Function 三、逆序输出L求逆序Private Sub Commandl_Click()Dim a
4、 As Stringa = Textl. TextText2.Text 二 reserve(a)End SubPublic Function reserve(n As String) As String Dim i As Integer, b As StringFor i = 1 To Len (CStr(n) b = Mid(n, i, 1) & bNext i reserve 二 bEnd Function1 .找出所有四位回文数Private Sub Command1 Click ()Dim num As Integer, renum As IntegerFor num = 1000 T
5、o 9999Call reserve(num, renum)If num = renum ThenListl. Addltem CStr(num)End IfNext numEnd SubPublic Sub reserve(ByVai n As Integer, ren As Integer) Dim st As String, i As IntegerFor i = 1 To Len(CStr (n)st = Mid (CStr(n), i, 1) & stNext iren = Vai(st)End Sub四、求阶乘之和L非递归Private Sub Commandl Click()Di
6、m i As Integer, n As Integer, s As Long n = Vai(Textl. Text)For i = 1 To ns = s + fact(i)Next iText2.Text = CStr (s)End SubPublic Function fact(ByVai k As Integer) As Dim i As Integer fact = 1 For i = 1 To k fact = fact * i Next iEnd Function2 .递归Private Sub Commandl Click ()Dim i As Integer, n As I
7、nteger, s As n = Vai (Textl.Text)For i = 1 To ns = s + fact (i)Next iText2.Text = CStr (s)End SubPublic Function fact(ByVai k As Integer) As Long If k = 0 Or k = 1 Then fact = 1Elsefact = k * fact (k - 1)End IfEnd Function五、求最小公倍数(先求最大公约数) Private Sub Commandl_Click ()Dim m As Integer, n As Integer
8、m = Vai(Textl. Text) n = Vai(Text2. Text)Text3. Text = CStr (m * n / gcd(m, n) End SubPublic Function gcd(ByVai m As Integer, ByVai n As Integer) As Integer Dim r As Integer Do r = m Mod n m = n n = rLoop Until r = 0 gcd 二 mEnd Function六、升、降序数判断L升序数Private Sub Command1_Click()Dim i As IntegerFor i =
9、 100 To 999If sx (i) Then Listl. Additem iNext iEnd SubPublic Function sx(n As Integer) As Boolean Dim i As Integer, s As String s = CStr (n) sx = False可省略For i = 1 To Len(s) - 1If Mid(s, i, 1) = Mid(s, i + 1, 1) Then Exit Function Next i sx = TrueEnd Function2)降序数Private Sub Command1 Click()Dim i A
10、s IntegerFor i = 100 To 999If jx(i)= True Then Listl. Additem iNext iEnd SubPublic Function jx(n As Integer) As Boolean Dim i As Integer, s As String s = CStr (n) jx = False 可以省略 For i = 1 To Len (s) - 1If Mid(s, i, 1) = Mid(s, i + 1, 1) Then Exit Function Next i jx 二 TrueEnd Function七、求因子和Private S
11、ub Commandl Click ()Dim n As Integer n = Vai(Textl. Text) Text2.Text = CStr(sum(n)End SubPublic Function sum(n As Integer) As Integer Dim i As IntegerFor i = 1 To nIf n Mod i = 0 Then sum = sum + i Next iEnd Function八、求因子个数Private Sub Command1 Click()Dim n As Integer n 二 Vai (Textl.Text)Text2.Text = CStr(num (n)End SubPublic Function num(n As Integer) As Integer Dim i As Integer, k As Integer For i = 1 To nIf n Mod i = 0 Then k = k + 1Next i num = k End Function