精心打造
Option Explicit Private Sub Command1_Click() Dim S As Single,T As Single,K As Integer,x As Single Const eps As Single=0.000001 x=Val(Textl) k=1 S=1 T=1 Do T= t*(-1)*x^2/((2*k-1)*(2*k)) S=S+T K=K+1 Loop While Abs(t)>eps Text2=S End Sub
11. 用以下公式求Sin(x)的值.当级数通项的绝对值小与eps=10^-7时停止计算,x的值由键盘输入。
(2n?1)x3x5nx?...?(?1)?... x?? n=0,1,2…. sin(x)?x??3!5!(2n?1)!请完善如下程序
Option Explicit Private Sub Form Click() Dim x As Double,t As Double,n As Integer,sum As Double Const eps=0.0000001 X =Val(InputBox(“请输入x:”)) t=1 sum =x n =1 Do Until t 12.下面程序的功能是求序列:1/2、2/3、3/5、5/8、8/13、……前若干项的和,项数由文本框输入。试完善程序使其达到功能要求。 Option Explicit Private Sub command1_click() Dim n As Integer, a1 As Integer, a2 As Integer 6 未来 精心打造 Dim i As Integer, t As Single, s As Single n = Val(Text1.Text) a1 = 1 a2 = 2 For i = 1 To n t=a1/a2 s = s + t a2 = a1 + a2 a1=a2-a1 Next i Text2 = s End Sub 13.下面程序的功能是计算s=1- 111 n≤9的值。请完善如下程序: ????22333nn? nOption Explicit Private Sub Command1_Click() Dim n As Integer, st As String, i As Integer Dim k As Single, s As Single, j As Integer n=Text1 s=1 For i =2 To n __ st=””______________ For j =1 To i st=st & CStr(i) next j k=____1/st __________ s=s + k Next i Text2=s End Sub 14.本程序的功能是将某一位的16进制数转换为一个四位的2进制数, 请完善本程序,如A->1010,3->0011。 Private sub command1_click() Dim s as String,st As String, i As Integer,p as String s=″A″ st=″0123456789ABCDEF″ For i=1 To Len(st) If ____ mid(st,I,1)=s __________ Then Exit For Next i i=i-1 Do p=__ (I mod 2) & p ___________ 7 未来 精心打造 i=i\\2 Loop Until i<=0 Print s & ″的二进制为″& P End Sub 15.本程序的功能是找出一组连续的整数,它们的和数等于给定的数。通常给定一个数,可能会找出多组符合要求的连续整数。查找的方法是穷举法。请完善本程序。程序界面见图。 图2-11 求等于给定和数的连续整数序列 Option Explicit Private Sub CmdRun_Click() Dim m As Integer, sum As Integer, st As String Dim i As Integer, j As Integer, k As Integer m = Text1 For i = 1 To m - 1 _____ sum=0___________ For j = i To m sum = ____ sum+j ____________ If sum = m Then st = \赋以空值 For k = i To j st = st & k & “+” Next k List1.AddItem Left(st, Len(st) - 1) End If Next j Next i End Sub 16.下面是一个加密/解密程序。加密的算法是:将原文中每个字符的ASCII代码加上该字符在原文中的位置数(设字符在原文中是第n个字符,则其位置数是n被10除的余数,当余数为0时,其位置数为10。例如,原文字符串为\,第一个字符\,其位置数为1,而 \为第12个字符,其位置数是被10除的余数2,其余依此类推)作为密文字符的代码,再将密码字符逆序排列(例如ABCD,逆序排列为DCBA)即为密文。解密算法 8 未来 精心打造 是加密算法的逆运算。 Option Explicit Option Base 1 Private Sub Command1_Click() '加密 Dim enc As String, dec As String, i As Integer Dim s As String * 1, n As Integer dec = Text1 For i = 1 To Len(dec) s = Mid(dec, i, 1) _ n=I mod 10_________________ If n = 0 Then n = 10 enc = Chr(Asc(s) + n) & enc Next i Text2 = enc End Sub Private Sub Command2_Click() '解密 Dim enc As String, dec As String, i As Integer Dim n As Integer, s As String * 1, p As Integer enc = Text2 n=len(enc) For i = 1 To n s = Mid(enc, i, 1) p = n Mod 10 If p = 0 Then p = 10 dec =chr(asc(s)-p) & dec n = n - 1 Next i Text1 = dec End Sub 17.下面程序的功能是随机生成小于100的诸素数组成的数组。程序界面参见下图。 图2-12 生成素数数组 Private Sub Command1_Click() Dim p() As Integer, i As Integer, n As Integer,st As String, k As Integer Randomize k = InputBox(\元素个数:\ ReDim p(k) 9 未来 精心打造 Do n = Int(Rnd * 90) + 10 If ____ prime(n)______________ Then i = i + 1 p(i) = n End If Loop Until _____ i=k _____________ For i = 1 To UBound(p) st = st & Str(p(i)) If i Mod 10 = 0 Then st = st & vbCrLf Next i Text1 = st End Sub Private Function prime(n As Integer) As Boolean Dim i As Integer : prime = True For i = 2 To Sqr(n) If n Mod i = 0 Then ______ prime = False Exit For End If Next i End Function 18.在计算机中用一个字节(8位)来存储一个字符的ASCII码,其中低7位二进制数对应字符的编码,每个字节的最高位一般保持为\,在数据传输时可用作奇偶校验位。传输时ASCII代码转换成传输码的方法是,若7位ASCII码有偶数个1,则最高位为1。有奇数个1,则最高位为0。例如字母\的ASCII码十进制表示为65,二进制表示为01000001。\的传输码则为\。本程序的功能就是把ASCII代码转换成这种传输码。程序界面见图。 未来 10
金陵科技学院2012年VB期末考试题目(2)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)
![](/skin/haowen/images/icon_star.png)