《期末考试VB实验考题.docx》由会员分享,可在线阅读,更多相关《期末考试VB实验考题.docx(3页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、期末考试VB实验考题找出100以内的挛生素数(包含判断一个数是否是素数的函数prime)Private Sub command l_click()Dim i As IntegerFor i = 2 To 97If prime (i) =True And prime (i+2) thenPrint i & “和” & i+2End IfNext iEnd SubPrivate Function prime (n As Integer) As BooleanDim i As IntegerFor i = 2 To n-1If n Mod i= o Then Exit ForNext iIfin-l
2、ThenPrime = TrueEnd IfEnd Function判断一个数是否是超级素数(包含判断一个数是否是素数的函数prime)Private Sub Command l_click()Dim i As IntegerFor i = 100 To 9999If prime (i) Thenfor j =1 To Len(Cstr (i)If prime (mid (ij,l)=False Then ExitNext jIf jLen (Cstr(i) Then listl.additemEnd ifEnd SubPrivate Function Prime (n As Integer)
3、 As BooleaDim i As IntegerIf n n-1 ThenPrime = TrueEnd IfEnd Function逆序输出(包含将字符串逆序的过程reverse)Private Sub Commandl_click()Dim n As Integer, i As Integer, sum As Longn=Val (Text 1)For i=l To nSum = sum + fact (i)Next iText 2 =sumEnd SubPrivate Function fact (n As Integer) As LongDim i As Integer, Fact
4、 =1For i = 1 To nFact =fact X iNext iEnd Function(4)求1! +2! +n!的值(包含求n!的值的函数fact)Private Sub Command l_click()Text 2 = reverse (Text 1)End SubPrivate Function reverse (s as string) As StringDim i As IntegerFor i = 1 To Len(s)reverse = mid(s,i,l)& reverseNext iEnd Function求最小公倍数(包含求两个数最大公约数的函数gcd) Pr
5、ivate Sub Commandl_click()Dim m As Integer, n As Integer 9a As Integerm= val (Text l.text)n = val(Text 2.text) a =mXnText 3 = a/gcd(m,n)End SubGcd =mEnd FunctionGcd =mEnd FunctionLoop until r=oPrivate Function gcd(m As Integer, n As Integer) As Integer Dim r As Integer Do r=m Mod n m=n n=r(6)求一维数组元素
6、之和(包含一维数组元素之和过程totalOption BasePrivate Sub commandl_click()Dim a(10) As Integer, i As integer, sum As IntegerFor i= 1 To 10a(i)=Int(Rnd*90) +10Text 1 =Text 1 & a(l)Next iCall total (a,sum)Text 2 = CStr(sum)End SubPrivate Sub total a() As Integer, sum As IntegerDim i As IntegerFor i = 1 To UBound(a)S
7、um = sum = a(i)Next iEnd Sub找出100200之间的升序数(包含判断一个数是否是升序数的函数sx)Private Sub Command l_click ()Dim i AS Integer ,K As BodeanFor i = 100 To 200If sx(i) ThenList 1 additem iEnd ifNext iEnd SubPrivate Function sx (n As Integer) As BooleanDim i As Integer, s As StringS= CStr(n)sx=FalseFor i = 1 To Len(s )-
8、1If mid (s,i=l J)=mid (s,i+l J) ThenExit FunctionEnd ifNext isx = trueEnd Function(8)找出100200之间的降序数(包含判断一个数是否是升序数的函数sxQx)Private Sub Command l_dick ()Dim i AS Integer ,K As BodeanFor i = 100 To 200If sx(i) Then list 1. additem iEnd ifNext iEnd SubPrivate Function sx (n As Integer) As Boolean Dim i A
9、s Integer, s As String S= CStr(n) sx=FalseFor i = 1 To Len(s )-1If Mid (s,i=l,l) =midThenExit FunctionEnd ifNext isx = trueEnd Function求一个数的因子和(包含求一个数的因子和的函数yzsum)Private Sub Command l_click ()Dim n As Integern= val (Text l.text)Text 2 = CStr(yzsum(n)End SubPrivate Function yzsum (n As Integer) As I
10、ntegerDim i As IntegerFor i = 1 To nIf n mod i =0 Then yzsum =yzsum + iNext iEnd Function(10)求一个数的因子的个数(包含求一个数的因子和的函数yzsum)Private Sub Commandl_click ()Dim n As Integern= val (Text Ltext)Text 2 = CStr(yzsum(n)End SubPrivate Function yzsum (n As Integer) As IntegerDim i As IntegerFor i = 1 To nIf n M
11、od i =0 Thenyzsum =yzsum + 1End ifNext iEnd Function(11)普通函数过程求一个数的阶乘Private Function jc(n As Integer) As LongDim i As Integerjc=lFor i = 1 To nJC=JC 可Next iEnd FunctionPrivate Sub Command l_click ()Dim n As Integern = val (Text 1)text2=CStr (jc(n)End Sub(12)通过递归来求阶乘Private Function fac(n As Integer ) As LongIf n=l Thenfac=lElseFfac=n* fac(n-l)End IfEnd FunctionPrivate Sub Command_click()Printfac(a)w ;fac(4)End Sub