VB上机试题及答案 .doc
《VB上机试题及答案 .doc》由会员分享,可在线阅读,更多相关《VB上机试题及答案 .doc(19页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、上机操作练习题1某数组有20个元素,其值为随机产生的两位整数。现要求将前10个元素与后10个元素对换。即第1个元素与第20个元素互换,第2个数与第19个元素互换,第10个元素与第11个元素互换。在窗体上输出数组原来各元素的值与对换后各元素的值。Option explicitDim a(1 to 20) as integer, I as integer, j as integer, t as integerPrivate Sub Form_Load()ShowPrint 原始数组是:RandomizeFor i = 1 To 20 a(i) = Int(Rnd * 90) + 10 Print
2、a(i); If i Mod 10 = 0 Then PrintNext iPrintPrint 现在数组是:For i = 1 To 10 j = 21 - i t = a(i): a(i) = a(j): a(j) = tNext i For i = 1 To 20 Print a(i); If i Mod 10 = 0 Then Print Next iEnd Sub2从数组中删除一个数。先定义一个一维数组,大小、数组的值自定。从键盘上输入一个数,若这个数在数组当中,就把它删除;若这个数不在数组中,提示“该数不在数组中”注意:1)如果数组中存在多个与该数相同的元素,每一个都能删除;2)能
3、实现多次删除Private Sub Form_Load()ShowReDim a(9) As Integerm = 1Print 原来数组为:For i = 0 To 9 a(i) = InputBox(请输入第 & m & 个数:) Print a(i); m = m + 1Next iPrintn = InputBox(查找的数是:)i = 0: flag = FalseDo If a(i) = n Then j = i: flag = True For k = j To UBound(a) - 1 a(k) = a(k + 1) Next k ReDim Preserve a(UBoun
4、d(a) - 1) i = i - 1End If i = i + 1Loop While (i = UBound(a)If flag = False Then MsgBox n & 不在该数组中ElsePrint 删除 & n & 后的数组是: For i = 0 To UBound(a) Print a(i); Next iEnd IfEnd Sub3数组a中存放着升序排列的数据,使用array赋值,将inputbox输入的新数插入到数组中,插入后数组仍有序。注意:要求利用动态数组,能够实现多次插入Private Sub Form_Load()Shown = 8: ReDim a(n)a
5、= Array(5, 15, 23, 52, 68, 88, 90, 99)n = n + 1: ReDim Preserve a(n)m = InputBox(请输入要插入的数据:)For i = 1 To n - 1 If m singer(i).fenshu(k) Then t = singer(i).fenshu(j) singer(i).fenshu(j) = singer(i).fenshu(k) singer(i).fenshu(k) = t End If Next NextNextFor i = 1 To 4 For j = 2 To 5 singer(i).score = s
6、inger(i).score + singer(i).fenshu(j) Next singer(i).pingjun = singer(i).score / 4Next For i = 1 To 3 For j = i To 4 If singer(i).score max Then max = a(i, j) If a(i, j) min Then min = a(i, j) sum = sum + a(i, j) Next j ave = (sum - max - min) / (n - 2) b(i) = aveNext iFor i = 1 To n - 1 For j = i +
7、1 To n If b(i) b(j) Then t = b(i): b(i) = b(j): b(j) = t End If Next jNext iPrint 选手的得分由高到低为:For i = 1 To 10 Print b(i);Next iEnd Sub5编写函数求一元二次方程ax2+bx+c=0的解。注意:要求考虑方程有两相等实根、两不等实根和两个虚根的情况。Dim a As Single, b As Single, c As SingleDim s As Singlea = InputBox(请输入a)b = InputBox(请输入b)c = InputBox(请输入c)s
8、= b 2 - 4 * a * cIf s 47 And Asc(Mid(a, i, 1) 96 And Asc(Mid(a, i, 1) 64 And Asc(Mid(a, i, 1) 91 Then p = p + 1 End IfNextEnd Sub7随机产生一个5*5的矩阵,将主对角线和次对角线元素都置为1,并求出矩阵的四周元素之和,显示在列表框中。Option Base 1Private Sub Form_Load()Dim a(5, 5) As IntegerDim s As IntegerFor i = 1 To 5 For j = 1 To 5 If i = j Or i =
9、 6 - j Then a(i, j) = 1 Else a(i, j) = Int(Rnd * 1000) End If = & & a(i, j) s = s + a(i, j) Next = & vbCrLf & vbCrLfNextem (s)End Sub8编写一个函数,能将字母按下列规律译成密码:AZ azBY by即第1个字母与第26个字母互换,第i个字母与第(26-i+1)个字母互换。在文本框中任意输入一个字符串,调用该函数将密码显示在另一个文本框中。Private Sub Command1_Click()Dim s1 As Strings1 = = convert(s1)En
10、d SubPrivate Function convert(s1 As String) As StringFor i = 1 To Len(s1) If Asc(Mid(s1, i, 1) 64 Then convert = convert & Chr(91 - Asc(Mid(s1, i, 1) + 64) ElseIf Asc(Mid(s1, i, 1) 96 Then convert = convert & Chr(123 - Asc(Mid(s1, i, 1) + 96) End IfNextEnd Function9编写2个函数,分别求两个正整数的最大公约数和最小公倍数,用户任意输入
11、两个数,采用函数嵌套调用,该函数将它们在窗体显示。Private Sub Form_Load()Text1 = : Text2 = End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then cusEnd SubPrivate Sub Text2_KeyPress(KeyAscii As Integer)Dim m As Integer, n As Integer, r As Integer, t As Integer, xClsIf KeyAscii = 13 Then m = n = If m n
12、 Then t = m: m = n: n = t End If r = m Mod n While r 0 m = n: n = r: r = m Mod n Wend Print If m 0 Or n 0 Then x = MsgBox(请输入正数, vbCritical + vbOKCancel) If x = vbCancel Then End Else Text1 = : Text2 = cus End If Else Print & 和 & & 的最大公约数是:; n End IfEnd IfEnd Sub10. 编写计算阶乘的Function过程,利用ex的下列公式计算e(直到
13、最后一项小于10-6为止)的近似值。ex1+x/1!+x2/2!+x3/3!+xn/n! Option ExplicitPrivate Sub Form_Load()ShowDim x As Integer, n As Integer, s As Doublex = InputBox(请输入一个数:)n = 1: s = 1Do s = s + x n / (jiecheng(n) n = n + 1Loop Until x n / (jiecheng(n) i Then a(i, j) = 0 Else If i = j Then a(i, j) = 1 Else a(i, j) = a(i
14、 - 1, j) + a(i - 1, j - 1) End If End If If a(i, j) = 0 Then Exit For Else Tab(g); a(i, j); g = g + 10 End If Next j Next iEnd SubPrivate Sub Picture1_Click()End Sub12编写一个过程,输出以下菱形图形: A BBB CCCCC DDDDDDD CCCCC BBB A主程序输入一个奇数(3),调用过程在窗体上输入图形。Private Sub Command1_Click()Dim n As Integern = InputBox()I
15、f n Mod 2 = 0 Or n = 3 Then MsgBox (错误)Else Call draw(n)End IfEnd SubSub draw(n As Integer)For i = 1 To (n + 1) / 2 s = Chr(64 + i) Print Tab(41 - i + 1); String(2 * (i - 1) + 1, s)Nextc = 1For j = (n - 1) / 2 To 1 Step (-1) c = c + 1 s = Chr(64 + i - c) Print Tab(41 - i + c + 1); String(2 * (j - 1)
16、 + 1, s)NextEnd Sub13编写一个将十进制转换为二进制字符串输出的函数。调用函数计算用户输入一个十进制数,在文本框中输出二进制值。注意:输入数据为负数,应该将符号位输出如 -13 结果 -1101Option Base 1Dim a()Private Function convert(y) As StringDim i As Integeri = 1ReDim a(i)x = Abs(y)Do While x 0ReDim Preserve a(i) a(i) = x Mod 2 If a(i) = 1 Then x = (x - 1) / 2 Else x = x / 2 E
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VB上机试题及答案 VB 上机 试题 答案
限制150内