VB上机考试最全调试题及答案_第1页
VB上机考试最全调试题及答案_第2页
VB上机考试最全调试题及答案_第3页
VB上机考试最全调试题及答案_第4页
VB上机考试最全调试题及答案_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第二部分 浙江省vb二级试题解a程序调试题一、选择结构程序调试01option explicitpublic sub getanswer() 该过程是用于计算一元二次方程的根,。 dim dalt!, a#, b#, c# a = inputbox(输入系数a) b = inputbox(输入系数b) c = inputbox(输入系数c) dalt = b * b - 4 * a * c if -1- then dalt = sqr(dalt) msgbox format(-b + dalt) / 2 / a), 0.00 + chr(13) + chr(10) + format(-b -

2、dalt) / 2 / a, 0.00) elseif -2- then msgbox format(-b / 2 / a, 0.00) + chr(13) + chr(10) + format(-b / 2 / a, 0.00) else dalt= -3- msgbox format(-b / 2 / a, 0.00) + +i + format(dalt / 2 / a, 0.00) + chr(13) + chr(10) + format(-b / 2 / a, 0.00) + -i + format(dalt / 2 / a, 0.00) end ifend sub二、单重循环程序调

3、试02option explicit 这条语句要求在该模块中,使用变量前必须先声明。public sub uppersen() 该过程是将一个英文句子的每个单词的首字母都变成大写。 从键盘上任意输入一条英文句子, 将句子中的每个单词的首字母都变成大写 例如:输入i am a good student. 要求输出i am a good student. dim oldsen as string, newsen as string dim char as string, lastchar as string dim k as integer, i as integer oldsen = input

4、box(请输入英文句子:) k = -1- 以空格作为单词的界定,空格后的字母转换为大写字母 lastchar = -2- for i = 1 to k char = -3- if lastchar = then char = ucase(char) end if newsen = newsen & char lastchar = char next i form1.print input:; oldsen form1.print output:; newsenend sub程序调试03public sub jisuan() 该过程用于计算1-(1/2)+(1/3)-.+(1/99)-(1/1

5、00)的值并打印出来。 dim i as integer * 错误1 * dim k as integer dim s as single s = 0 * 错误2 * for i = 1 to 100 k = 1 / i s = s + k * 错误3 * k = 1 / i + 1 s = s + k next i form1.print s=; send sub程序调试04option explicitpublic sub same() 该过程找出1-1000之间所有的同构数 所谓同构数是指一个数出现在它的平方数的右端 如25在25平方625的右端,则25为同构数 利用数字转字符再取出右端

6、字符的方法进行判断 dim i as long dim x1 as string, x2 as string for i = 1 to 1000 x1 = -1- 将i转字符型 x2 = -2- 将i2转字符型 if -3- then form1.print i; 是同构数 end if next iend sub程序调试05public sub armstrongnumber() 该过程是用于求出1-999之间所有的armstrong数,并输出。 所谓armstrong数是指一个数等于它每位上数字的立方和。例如:371=33+73+13,那么371就是一个armstrong数。 dim ar

7、mstrong as integer dim i as integer dim hundred as integer 百位上的数字 dim ten as integer 十位上的数字 dim one as integer 个位上的数字 for i = 1 to 999 hundred = -1- ten = int(i mod 100) 10) one = -2- if -3- then form1.print i; is armstrong number end if next iend sub程序调试06public sub sum() 该过程是用于计算并输出f=1-1/(2*3)+1/(

8、3*4)-1/(4*5)+1/(19*20)。 dim f as single dim i as integer dim sign as integer -1- f = 1 -2- f = f + sign / (i * (i + 1) -3- next i form1.print f=; fend sub程序调试07option explicitpublic sub eat() 该过程是用于计算猴子共摘了多少个桃子。 (小猴在一天摘了若干个桃子,当天吃掉了一半多一个; 第二天吃了剩下的一半多一个; 以后每天都吃尚存的一半零一个,到第7天早上要吃时只剩下一个了), 并将结果输出来。 dim n

9、%, i%, x% x = 1 for i=6 to -1- step -2- -3- next i form1.print 共有; x; 个桃子end sub程序调试08public sub fabonia() 该过程是对以下数列进行运算: 有一个数列,它的前三个数是0,1,2, 从第四个数起,每个数都是它前面的两个数之和 求出该数列的第17个数是多少 求出该数列的第几个数起每个数都超过1e+8 dim last_one as long dim last_two as long dim this_one as long dim i as integer last_one = 1 数列的第二个

10、数 last_two = 2 数列的第三个数 i = 4 从数列的第四个数求起 do this_one = last_one + last_two last_one = -1- last_two = -2- if i = 17 then form1.print no:17=; this_one end if -3- loop while this_one 1e+8end sub程序调试09public sub findat() 该过程是用于在一个字符串变量中查找”at”,并用消息框给出查找结果的报告:没有找到或找到的个数。 dim str1 as string dim length as in

11、teger 字符串长度 dim sum as integer 查到的个数 dim i as integer str1 = inputbox(请输入一个字符串) length = -1- i = 1 sum = 0 do while i -2- if -3- = at then sum = sum + 1 end if i = i + 1 loop if -4- then msgbox 没有找到! else msgbox 找到了 & str(sum) & 个 end ifend sub程序调试10注:此题题目和第41题相同,但算法不一样。option explicitpublic sub com

12、div() 该过程是求任意两个正整数的最大公约数 dim m as integer, n as integer 任意两个正整数 dim i as integer dim div as integer 最大公约数 输入两个正整数,要求m与n都必须大于零 do m = val(inputbox(m=) n = val(inputbox(n=) loop while -1- 先将两个整数中的较小数假设为最大公约数,再依次往下 寻找能同时除尽m和n的数即为最大公约数 -2- if n m then div = n end if do while m mod div 0 or n mod div 0 -

13、3- loop form1.print m; 和; n; 的最大公约数是:; divend sub程序调试11public sub wrap()该过程用于判断一个字符串是否“回文”。所谓“回文”是指字符串顺读与倒读都是一样的,如“潮起潮落,落潮起潮”。 dim length as integer dim str1 as string dim strleft as string dim strright as string dim k as integer str1 = inputbox(请输入任意的字符串) 输入任意字符串 *错误1* length = val(str1) k = 1 do *

14、错误2* strleft = left(str1, k) 从左边起逐个取出一个字符 *错误3* strright = right(str1, k) 从右边起逐个取出一个字符 *错误4* if strleft = strright then exit do end if k = k + 1 loop while k length / 2 then form1.print str1 & 是回文 else form1.print str1 & 不是回文 end ifend sub程序调试12option explicitpublic sub dtob() 该过程是将一个十进制正整数转换成为一个二进制

15、数。 采用连除2取余数的方法,将一个十进制数转换为二进制数。 dim dec as integer dim bin as string dim res as integer dim i as integer dec = val(inputbox(x=) 输入一个十进制数 form1.print 十进制数:; dec do res = -1- 求出除以2的余数 bin = trim(str(res) & -2- dec = -3- loop while -4- form1.print 转换为二进制数为:; binend sub程序调试13public sub guess() 该过程是猜数游戏,由

16、计算机产生一个1,100的任意整数, 输入猜数后计算机给出提示,如果5次后还没有猜中就结束游戏并公布正确答案。 dim r as integer dim x as integer dim times as integer randomize r = int(rnd * 100) + 1 产生一个在区间1,100 的任意整数 times = 1 do x = val(inputbox(输入猜数x) select case x case r form1.print 猜中了 exit do * 错误1 * case is 5 if times 5 then form1.print 猜数失败,游戏结束

17、! * 错误3 * form1.print 正确答案为 & str(x) end ifend sub程序调试14option explicitpublic sub summary()该过程是用于计算s=1+2+22+23+,直至s超过1e+16 dim s as single dim i as integer -1- i = 1 -2- s = s + 2 i -3- loop form1.print s=; send sub程序调试15option explicitpublic sub find() 该过程是用于找出被3、5、7除,余数为1的最小的5个整数,并将结果输出来。 dim coun

18、tn%, n% countn = 0 n = 1 do n=-1- if -2- then form1.print n countn = countn + 1 end if loop -3-end sub程序调试16option explicitpublic sub e() 该过程是用于计算e的值并将结果输出,要求精确到0.000000000000001, e的计算公式为:e=1+1/1!+1/2!+1/n!。 dim n, term, t n = 0: term = 1: t = 1 do n = n + 1 t= -1- term= -2- loop while t 0.000000000

19、000001 form1.print e=& -3-end sub程序调试17option explicitpublic sub qiun() 该过程是用于求出满足不等式1+2x+3x2+4x3+(n+1)xn1000的最大n值, 其中x是大于等于1的实数,其值由键盘输入。 dim x as single, s as single, n as integer, s1 as single, p as single x = val(inputbox(x=) s = 1: n = 1 p = x while -1- s1 = s s = s + (n + 1) * p p=p* -2- n= -3-

20、 wend n= -4- form1.print the maxism of n; n, s=; s1end sub三、多重循环程序调试18public sub prt() 该过程是输出由数字组成的如下所示金字塔图案 9 888 77777 6666666 555555555 44444444444 3333333333333 222222222222222 11111111111111111 dim i as integer, j as integer * 错误1 * for i = 9 to 1 form1.print space(i); for j = 1 to 19 - 2 * i *

21、错误2 * form1.print i next j form1.print next i *错误3 *end function程序调试19option explicitpublic sub pyramid() 该过程用于打印数字金字塔 1 222 33333 4444444 555555555 6666666 77777 888 9 dim i as integer dim j as integer dim start as integer 每行起始空格数 dim num as integer 每行数字个数 for i = 1 to 9 if i = -1- then start = 20

22、- i num = 2 * i - 1 else start = 10 + i num = 19 - 2 * i end if for j = 1 to -2- form1.print tab(start+j);-3- next j form1.print next iend sub程序调试20public sub prime() 该过程是输出100到200之间所有的素数,并统计素数的个数 dim i as integer dim j as integer dim k as integer dim t as integer 统计素数的个数 dim b as boolean for i = 10

23、0 to 200 b = true k= - 1 - j = int(sqr(i) do while k ave then -3- form1.print x(i); space(2); if -4- then form1.print end if next i form1.print form1.print 共有; count; 个在平均值之上.end sub程序调试22option explicitpublic sub del() 该过程从一个含有10个数组元素的数组中删除一个指定的数据,若该数据不存在,则给出提示。 先产生10个1,100的随机整数 从键盘上输入一个数,将该数从数组中删除

24、 randomize dim x as integer dim a() as integer dim i as integer, pos as integer redim a(10) as integer for i = 1 to 10 a(i) = int(rnd * 100) + 1 next i form1.print 原始数据: for i = 1 to 10 form1.print a(i); ; next i form1.print x = val(inputbox(输入删除的数x=) pos = 0 for i = 1 to 10 if x = a(i) then -1- exi

25、t for end if next i if pos 0 then for i = -2- to 9 -3- next i redim preserve a(9) 保留原数组中的9个数 form1.print 删除后数据: for i = 1 to 9 form1.print a(i); ; next i form1.print else msgbox 数组中未找到 & str(x) & ,删除不成功 end ifend sub程序调试23option explicitpublic sub counteven() 该过程是用于统计在随机产生的10个两位整数中的偶数的个数并用消息框输出。 dim

26、 a(10) as integer 存放10个随机数第一个为a(0) dim count as integer dim i as integer, n as integer randomize *错误1* for i = 1 to 10 *错误2* n = int(rnd * 90) a(i) = n form1.print a(i) if a(i) mod 2 = 0 then count = count + 1 end if next i *错误3* msgbox(偶数个数:,count)end sub程序调试24option explicitpublic sub sortchar() 该

27、过程是对字符串进行整理, 首先从键盘上输入一个任意的字符串,将该字符串的所有组成字符拆分开, 再按照字符ascii码从小到大的顺序将这些字符重新组成新的字符串。 例如输入a4fkze5,重新组合的字符串为45aefkz。 dim x as string 原始字符串 dim y as string 重新组合的字符串 dim c() as string 拆分出的字符 dim k as integer 字符串长度 dim i as integer, j as integer dim temp as string x = inputbox(输入一个字符串) k = -1- redim c(k) as

28、string 字符串拆分 for i = 1 to k c(i) = -2- next i 字符排序 for i = 1 to k - 1 for j = -3- if c(i) c(j) then temp = c(i) c(i) = c(j) c(j) = temp end if next j next i 排序后的字符组成新字符串 y = for i = 1 to k y = y & c(i) next i form1.print 原始字符串; x form1.print 重新组合的字符串; yend sub程序调试25option explicitpublic sub composit

29、or() 该过程是用于产生20个不大于1000的自然数, 并使用选择分类法按从大到小的顺序输出,相同数不输出。 const n = 20 dim a(n), i, p, j, t for i = 1 to n a(i) = -1- next i for i = 1 to n - 1 p = i for j = i to n if a(p) a(j) then -2- next j if -3- then t = a(i): a(i) = a(p): a(p) = t next i form1.print a(1) for i = 2 to n if a(i) a(i - 1) then for

30、m1.print -4- next i form1.printend sub程序调试26option explicitpublic sub prt() 该过程是输出由数字组成的如下所示杨辉三角形图案 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 const n = 8 dim a(n + 1) as byte, i as integer, j as integer dim s as string 给数组a赋初值 for i = 1 to n + 1 a(i) = 0 next i 计算并打印第一个数 a(1) = 1

31、form1.print space(2 * n + 2); 计算数字前的空格数 form1.print a(1) 计算其余行的数并打印 for i = 2 to -1- a(i) = a(i - 1) for j = -2- to 2 step -1 -3- next j form1.print space(n - i + 1) * 2); for j = 1 to i s = trim(str(-4-) form1.print space(int(4 - len(s) / 2 + 0.5) + s + space(int(4 - len(s) / 2 + 0.5); next j form1

32、.print next iend sub五、多维数组程序调试27public sub transpose() 该过程用于产生一个66的转置矩阵,将二维数组中所有行和对应列的元素进行交换。 dim a(1 to 6, 1 to 6) as integer dim i as integer, j as integer form1.print 原始数据 for i = 1 to 6 for j = 1 to 6 a(i, j) = int(rnd * 10) form1.print a(i, j); next j form1.print next i for i = 2 to 6 for j = 1

33、 to - 1 - - 2 - next j next i form1.print 转置后数据 for i = 1 to 6 for j = 1 to 6 - 3 - next j form1.print next iend subpublic sub swap(a as integer, b as integer) dim temp as integer temp = a a = b b = tempend sub程序调试28public sub printarray() 下面程序用于显示5行5列的数字方阵 使两对角线上元素均为1,其余均为2 要求显示的数字方阵两列数字之间空3格,两行之间空

34、一行 只要修改标出出错位置的下面那一条语句 dim a(5, 5) as integer dim i as integer, j as integer for i = 1 to 5 for j = 1 to 5 * 错误1 * if i j or i 6 - j then a(i, j) = 2 else a(i, j) = 1 end if * 错误2 * form1.print a(i, j) + space(3); next j * 错误3 * form1.print: form1.cls next iend sub程序调试029printarray过程是输出一个右上三角元素(含对角线)

35、为1,其余元素为0 的55矩阵public sub printarray()dim a(1 to 5, 1 to 5) as integerdim i as integer, j as integerfor i = 1 to 5 for j = 1 to 5 if - 1 - then - 2 - end if next jnext ifor i = 1 to 5 for j = 1 to 5 form1.print ; a(i, j); next j - 3 -next iend sub六、sub过程程序调试30private const n = 10private a(1 to n) as

36、 integertj过程是将一批数据中小于零的数及它们的积打印出来数据由scsj过程产生,数据的取值范围为-10 10 之间的随机整数数public sub tj() dim i as integer dim t as single t= - 1 - for i = 1 to 10 if - 2 - then t = t * a(i) end if next i form1.print t=; tend subpublic sub scsj() randomize form1.print 原始数据 dim i as integer dim j as integer for i = 1 to n

37、 随机产生0或1,为0时取负,为1时取正 j = int(rnd * 2) if - 3 - then j = -1 a(i) = j * int(rnd * (n + 1) form1.print a(i); next i form1.printend sub程序调试31dim a(30) as integerpublic sub findmax() 该过程是用于查找一批数据中的最大数,并输出最大数的值及最大数在数组中的下标值。 dim position as integer 最大数在数组中的下标 dim max as integer 最大数 dim i as integer generat

38、edata max = -1- position = -2- for i = 2 to 30 if a(i) max then max = -3- position = -4- end if next i form1.print 最大值= & str(max) form1.print 最大值的下标= & str(position)end subpublic sub generatedata() dim i as integer for i = 1 to 30 a(i) = int(500 * rnd() + 1) form1.print a(i), if i mod 5 = 0 then form1.print next iend sub程序调试32dim a(1 to 5, 1 to 4) as integerdim ave(1 to 5) as integerpublic sub maxline() 该过程用于查找一个5行4列的二维数组中行平均值最大的行,并将该行所有数据调整到第一行的位置。 dim i as integer, j as intege

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论