vb课后习题答案李淑琴主编西安电子科技大学出版社_第1页
vb课后习题答案李淑琴主编西安电子科技大学出版社_第2页
vb课后习题答案李淑琴主编西安电子科技大学出版社_第3页
vb课后习题答案李淑琴主编西安电子科技大学出版社_第4页
vb课后习题答案李淑琴主编西安电子科技大学出版社_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

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

文档简介

1、第1章1选择题 (1)b(2)c(3)c(4)a(5)a(6)d or ab(7)d(8)a(9)c(10)d(11)a第2章1选择题 (1)a b以数字开头;c是vb关键字;d中包含了“-”(减)号 (2)b a是一个字符串变量名;c什么也不是;d是一个变量名 (3)a b是整型;c是字符型;d是双精度型 (4)b(5)d(6)a (7)c 这是一个典型的四舍五入算法,根据这个可以自己写出一个四舍五入函数。 (8)b(9)c(10)c(11)d(12)b(13)a、b(14)d(15)c(16)a(17)b(18)c(19)c(20)c(21)b(22)c (23)a c尽管也是3456,但

2、它是一个数值,而不是字符串,所以c是错误的 (24)d这里要注意的是表达式分为两个部分,后一部分的最小值为-1,前一部分的最大值为5,所以整个表达式的最小值就为-5,而后一部分的最大值为3,所以整个表达式的最大值为15 (25)b(26)c(27)c(28)a(29)a(30)c(31)c(32)a(33)a2判断题 (1)错;(2)错;(3)错;(4)对;(5)错;(6)错;(7)错;(8)对;(9)对;(10)(错, str函数转换正数时前面会有一个符号位表现为空格,正确的应该是7。)(11)错。3按要求写表达式 (1)转换表达式a、(3*a*a+4*b*b*b)/(a-b) (这里的乘方

3、也可以用乘方号)b、6*sin(x+y*y)/(140/(3+a)(这里的3+a可以写成分子的一部分)c、exp(x)+log(10)/sqr(x+y+1)(2)写布尔表达式a、x>y and f<2b、x<z and y>=z or y<z and x>=z 也可写成 (x-z)*(y-z)<=0 and x<>yc、a*b>0d、a*b=0 and a<>b(3)利用函数写表达式a、chr(int(rnd * (asc("l") - asc("c") + 1) + asc(&qu

4、ot;c")也可以写成chr(int(rnd*10+67)b、int(rnd*(200-100+1)+100)c、(x mod 5) *(x mod 7)=0 d、right(x,1) & left(x,1) 也可以写成 (x mod 10)*10+x10 e、round(x,2) or format(x,”#.00”) f、mid(s,5,6) 这道题很多人容易写成mid(“s”,5,6),这就严重错误了。第03章1选择题 (1)c(2)c(3)d(4)a(5)d(6)b(7)d(8)b(9)b(10)a(11)b(12)d(13)a(14)d(15)b(16)d(17)a

5、(18)d(19)b、a(20)b(21)c(22)d(23)a(24)c(25)c(26)a(27)c(28)c(29)d(30)b(31)c(32)b(33)c(34)a(35)c(36)b(37)c (38)c (39)d2编程题(1) 鸡兔同笼private sub command1_click() dim h as integer, f as integer dim x as integer, y as integer h = val(text1.text) f = val(text2.text) x = (4 * h - f) / 2 y = (f - 2 * h) / 2 lab

6、el3.caption = "计算结果:鸡有" & x & "只,兔有" & y & "只"end sub(2)计算纸币张数private sub command1_click() dim x%, y%, z%, a100%, a50%, a20%, a10%, a5%, a1% x = val(inputbox("请输入支付金额") y = x a100 = x 100 x = x mod 100 a50 = x 50 x = x mod 50 a20 = x 20 x = x m

7、od 20 a10 = x 10 x = x mod 10 a5 = x 5 a1 = x mod 5 z = a100 + a50 + a20 + a10 + a5 + a1 print y & "元需要支付的钱币总张数为:" & a100 & "+" & a50 & "+" & a20 & "+" _& a10 & "+" & a5 & "+" & a1 & "

8、;=" & z & "张"end sub另解,利用数组:option base 1private sub command1_click() dim x%, z%, a(), b%(5), result$ a = array(100, 50, 20, 10, 5) x = val(inputbox("请输入支付金额") result = x & "元需要支付的钱币总张数为:" for i = 1 to 5 b(i) = x a(i) x = x mod a(i) z = z + b(i) result

9、= result & b(i) & "+" next i result = result & x & "=" & z & "张" print resultend sub(3)电话号码升位。private sub command1_click() dim x$ x = inputbox("请输入一个带区号的原电话号码,格式为:*-*") msgbox left(x, 4) & "8" & right(x, 7)end sub第04章1

10、选择题 (1)c(2)a(3)c(4)b(5)a(6)a(7)d(8)c(9)c(10)b(11)c(12)元答案 (13)c(14)c (15)c(16)a(17)c(18)a (19)c (20)b(21)b (22)a(23)d(24)c(25)d (26)b(27)d(28)a(29)d2判断题(1)对;(2)对;(3)对;(4)对;(5)错(应该是整型);(6)错;(7)对3根据程序写运行结果(略)4编程题(1) private sub command1_click()dim x%x = inputbox("请输入数值")print tab(2); format$

11、(x * x, "#.000"); tab(12); format$(sqr(x), "#.000"); tab(22); format$(x * x * x, "#.000"); tab(32); format$(x 1 / 3, "#.000")'如果format函数中小数位用"#"的时候当不够三位时不足的位不补0。'如果用round()函数,如果计算结果为整数时,则没有小数位。end sub(2)输入3个数,输出最大最小数private sub command1_click

12、() dim a%, b%, c% form1.fontsize = 20 a = inputbox("请输入第一个数") b = inputbox("请输入第二个数") c = inputbox("请输入第三个数") print a, b, c '先按照升序或降序排序然后再进行取最大最小数 if a > b then t = a a = b b = t end if if a > c then t = a a = c c = t end if if b > c then t = b b = c c = t

13、end if print "最小数是" a, "最大数是" cend subprivate sub command2_click() '定义变量max用于保存最大数,min保存最小数 dim max as single, min as single dim a as single, b as single, c as single a = val(text1.text) b = val(text2.text) c = val(text2.text) '假设a是最大数,因此将a的值赋给max max = a 用max与b进行比较,若b大于m

14、ax,则将b赋给max,让max保存前两个数的最大数 if max < b then max = b end if '用max与c比较,若c大于max则将c赋给max,让max保存三个数中的最大数 if max < c then max = c end if '假设a是最小数,因此将a的值赋给min max = a '用min与b进行比较,若b小于min,则将b赋给min,让min保存前两个数的最小数 if min > b then min = b end if '用min与c比较,若c小于min则将c赋给min,让min保存三个数中的最小数

15、if min < c then min = c end if '显示结果 label2.caption = "最大数为:" & max & ",最小数为:" & minend sub(3)判断闰年private sub command1_click() dim x%, y% x = val(inputbox("请输入年份") if (x / 4 = x 4 and x / 100 <> x 100) or x / 400 = x 400 then print x & "

16、;是闰年" else print x & "非闰年" end ifend sub(4)判断能否被5整除private sub command1_click() dim a%, b%, c% form1.fontsize = 20 picture1.cls x = text1.text if x mod 5 = 0 then picture1.print "您输入的数能被5整除!" else picture1.print "您输入的数不能被5整除!" end ifend sub(5)部门征税private sub co

17、mmand1_click() dim tax as single, income as single income = val(text1.text) if income <= 0 then '若输入的值小于0则提示错误,并退出该过程,不进行税额的计算 label2.caption = "输入有误." exit sub end if select case income case is <= 300 tax = 0 case is <= 500 tax = (income - 300) * 0.02 case is < 5000 tax =

18、200 * 0.02 + (income - 500) * 0.03 case else tax = 200 * 0.02 + 4500 * 0.03 + (income - 5000) * 0.04 end select label2.caption = "你应缴纳的数款为:" & tax & "元"end sub(6)输出100-200之间能被3整除的数private sub command1_click() dim x%, n% for x = 100 to 200 if x / 3 = x 3 then print tab(n m

19、od 10) * 6 + 1); x; n = n + 1 end if next xend sub(7)输出1000到1100之间的质数private sub form_click() dim flag as boolean for i = 1000 to 1100 m = i: flag = true for j = 2 to m - 1 if m mod j = 0 then flag = false exit for end if next j if flag = true then n = n + 1 print tab(2 + 7 * (n - 1) mod 6); m; end

20、if next iend sub(8)猜数字private sub command1_click() randomize timer '给一个随机种子,让每次产生的随机数都不同 x = int(101 * rnd) label1.caption = "你还有8次机会" for i = 8 to 1 step -1 y = val(inputbox("请猜一个100以内的整数", "猜数字") if y > x then label1.caption = "大了点,你还有" & i &

21、"次机会" elseif y < x then label1.caption = "小了点,你还有" & i & "次机会" else label1.caption = "恭喜你猜对了!" exit for end if next if i = 0 then label1.caption = "很遗憾,你用了8次机会也没猜正确!" & vbcrlf & "正确值为:" & x end ifend sub(9)100到999之间回文

22、数private sub command1_click() dim n as integer '记录第几个回文数 for x = 100 to 999 g = x mod 10 '获取x的各位数 b = x 100 '获取x的百位数 '若个位数和百位数相等则x就是回文数 if g = b then picture1.print tab(6 * (n mod 8) + 2); x; n = n + 1 end if nextend sub(10)e的近似值'方法一private sub command1_click() dim e as single, f

23、 as long, n as integer e = 1 f = 1 n = 1 do while f < 10 4 f = f * n n = n + 1 e = e + 1 / f loop label2.caption = "e的近似值为:" & eend sub'方法二private sub command2_click() dim e as single, f as long, n as integer e = 1 n = 1 do while f < 10 4 f = 1 for i = 1 to n f = f * i next n

24、 = n + 1 e = e + 1 / f loop label2.caption = "e的近似值为:" & eend sub(11)1+23+33+n3<m的最大nprivate sub command1_click() m = val(text1.text) if m < 200 then label1.caption = "m的值小于等于200,重新输入" text1.text = "" text1.setfocus else s = 0 n = 0 do while s < m n = n + 1

25、 s = s + n 3 loop label1.caption = "满足不等式的最大n值是:" & n - 1 end ifend sub方法二'n的取值范围1m(1/3),按要求我们从m(1/3)(最大的数)开始找符合条件的第一个数就是了private sub command1_click() m = val(text1.text) for n = int(m (1 / 3) to 1 step -1 s = 0 for i = 1 to n s = s + i 3 next if s < m then label2.caption = &quo

26、t;最大的n为:" & n exit for end if nextend sub(12)'方法一private sub command1_click() picture1.cls '清除以前输出的内容 for i = 1 to 6 picture1.print spc(15 - i); '在左侧打印n个空格来填充或将spc换为tab for j = 1 to 2 * i picture1.print "0" next picture1.print nextend sub'方法二private sub command2_cl

27、ick() picture1.cls '清除以前输出的内容 for i = 1 to 6 picture1.print tab(15 - i); string(2 * i, "0") '用string函数产生n个0 nextend sub(13)十进制转二进制 转十六进制private sub command1_click() n = val(inputbox("输入要转换的十进制整数") m = n x = "" do while n <> 0 a = n mod 2 n = n 2 x = a &

28、; x loop msgbox m & " 转换二进制数是:" & xend subprivate sub command2_click() n = val(inputbox("输入要转换的十进制整数") m = n x = "" do while n <> 0 a = n mod 16 select case a case 10 b = "a" case 11 b = "b" case 12 b = "c" case 13 b = "d

29、" case 14 b = "e" case 15 b = "f" case else b = a end select n = n 16 x = b & x loop msgbox m & " 转换十六进制数是:" & xend sub(14)解灯谜private sub command1_click() dim a%, b%, c%, d% dim x%, y%, z% for a = 0 to 9 for b = 0 to 9 for c = 0 to 9 for d = 0 to 9 x =

30、a * 1000 + b * 100 + c * 10 + d y = c * 100 + d * 10 + c z = a * 100 + b * 10 + c if x - y = z then print "a=" a; "b=" b; "c=" c; "d=" d end if next d, c, b, aend sub方法二private sub command2_click() '由题意可知abcd的取值范围是10009999之间,因此通过循环来找出符合条件的abcd for abcd = 1

31、000 to 9999 abc = abcd 10 '获取abc的值 c = abc mod 10 d = abcd mod 10 '分别获取abcd的十位数c和个位数d cdc = c * 100 + d * 10 + c '计算cdc的值'如果符合abcd-cdc=abc则输出 if abcd - cdc = abc then label1.caption = "abcd的值为:" & abcd exit for end if nextend sub(15)猴子吃桃private sub command1_click() '

32、;验证 m = 1534 s = m for i = 1 to 10 print s s = s - (s / 2) - 1 next i if i = 11 and s = 1 then print send subprivate sub command2_click() '穷举发 for m = 1000000 to 1 step -1 s = m for i = 1 to 9 s = s - (s / 2) - 1 next i if i = 10 and s = 1 then print m next mend subprivate sub command3_click() &

33、#39;思路:假设第n天的桃子为x,前一天(第n-1天)的桃子为y '则x=y-(y/2+1)=>x=y/2-1;y=2*(x+1) '现在第十天的桃子是已知的,我们反推到第一天即可 'x = 1' for i = 9 to 1 step -1' y = 2 * (x + 1)' x = y' next' print y dim x as integer, i as integer x = 1 for i = 10 to 2 step -1 x = (x + 1) * 2 next i print xend sub第05章1

34、选择题 (1)b(2)a(3)b(4)c(5)b(6)d(7)a(8)b(9)b(10)b(11)a(12)c(13)c(14)d(15)b (16)c (17)b (18)c2编程题(1)private sub command1_click() dim c(0 to 7) a = array(1, 3, 5, 2, 4, 18, 50, 25) b = array(5, 27, 30, 35, 60, 41, 87, 33) for i = 0 to 7 c(i) = a(i) + b(i) next print "a()", "b()", "

35、;c()" for i = 0 to 7 print a(i), b(i), c(i) nextend sub(2)矩阵找最大元素的行列private sub command1_click() '先声明一个动态数组a,因为其大小不能确定 dim a() as integer n = val(text1.text) m = val(text2.text) '指定数组的大小 redim a(1 to n, 1 to m) as integer picture1.cls '用随机数给数组赋值,并打印到窗体 for i = 1 to n for j = 1 to m

36、a(i, j) = int(rnd * 901) picture1.print tab(5 * (j - 1) + 2); a(i, j); next next '假设第一个元素的值最大,用r和c分别存放最大数所在的行和列 r = 1: c = 1 for i = 1 to n for j = 1 to m if a(r, c) < a(i, j) then r = i c = j end if next next label3.caption = "矩阵中的最大值为:" & a(r, c) & vbcrlf label3.caption =

37、label3.caption & "位置:" & r & "行," & c & "列"end sub(3)交换数组元素值private sub command1_click() dim a$(), x, i%, n% a = split(inputbox("请输入n个数据,数据之间用逗号分隔!"), ",") '注意这里的inpubox函数的用法,输出数据时,数据之间应该用英文状态的逗号分隔 print "对换前数据序列为:"

38、for each x in a print x; " " next x print n = ubound(a) for i = 0 to n 2 x = a(i): a(i) = a(n - i): a(n - i) = x next i print "对换后数据序列为:" for each x in a print x; " " next xend sub(4)private sub command1_click() dim x$(), a%(4 to 9), b(), i%, n%, y, k% b = array("无

39、效数据", "小于60分", "6069", "7079", "8089", "90100") x = split(text1, ",") n = ubound(x) for each y in x if y < 0 or y > 100 then y = 40 elseif y < 60 then y = 50 elseif y = 100 then y = 90 end if k = y 10 a(k) = a(k) + 1 next y fo

40、r i = 4 to 9 picture1.print b(i - 4), a(i) next iend sub(5)根据身份证前17位算第18位option base 0private sub command1_click() dim a%(16), b$(), w(), i%, x$, sum% b = split("1,0,x,9,8,7,6,5,4,3,2", ",") w = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2) x = inputbox("请输入您的身份

41、证前17位") for i = 0 to 16 a(i) = mid(x, i + 1, 1) sum = sum + a(i) * w(i) next i print "您的身份证号证为:" & x & b(sum mod 11)end sub(6)输出斐波拉契数列option base 1 private sub command1_click() dim a&(30), i% a(1) = 1: a(2) = 1 for i = 3 to 30 a(i) = a(i - 1) + a(i - 2) next i for i = 1 to

42、 30 print tab(i - 1) mod 5) * 10 + 1); a(i); next iend sub(7)随进产生15个不同大写字母option base 1private sub command1_click() dim a$(15), i%, j% for i = 1 to 15 a(i) = chr(int(rnd * 26 + 65) for j = 1 to i - 1 if a(j) = a(i) then i = i - 1 exit for end if next j next i for i = 1 to 15 print a(i); " "

43、; next iend sub(8)将一个数插入数组option base 1dim a() '声明窗体及变量是为了能够多次插入数据sub parray(x() '定义打印数组的子程序 dim i%, y for each y in x print y; next y printend subprivate sub command1_click() dim i%, k%, n%, x n = ubound(a) print "插入前:": call parray(a) do '保证插入位置在有效范围内 k = inputbox("请输入待插

44、入的位置(" & 1 & "" & n + 1 & ")") loop until k >= 1 and k <= n + 1 redim preserve a(n + 1) '扩大数组 x = inputbox("请输入待插入的数据") for i = n to k step -1 a(i + 1) = a(i) next i a(k) = x print "插入后:": call parray(a)end subprivate sub form_lo

45、ad() a = array(1, 2, 3, 4, 5, 6, 7, 8, 9) '给数组赋初值end sub(9)option base 1dim a() '声明窗体及变量是为了能够多次插入数据sub parray(x() '定义打印数组的子程序 dim i%, y for each y in x print y; next y printend subprivate sub command1_click() dim x%, i%, n%, k%, f as boolean n = ubound(a) print "删除前:": call par

46、ray(a) x = inputbox("请输入待删除的数") for i = 1 to n if a(i) = x then f = true for k = i to n - 1 a(k) = a(k + 1) next k end if if f = true then redim preserve a(n - 1) exit for end if next i if f = true then print "删除后:": call parray(a) else print "查无此数!" end ifend subprivat

47、e sub form_load() a = array(1, 2, 3, 4, 5, 6, 7, 8, 9) '给数组赋初值end sub(10)求矩阵四周元素之和option base 1sub parray(x%() '定义打印数组的子程序 dim i%, j% for i = 1 to ubound(x, 1) for j = 1 to ubound(x, 2) print tab(j * 5); x(i, j); next j next i printend subprivate sub command1_click() dim i%, j%, m%, n%, s%,

48、a%() m = inputbox("m=") n = inputbox("n=") redim a(m, n) for i = 1 to m for j = 1 to n a(i, j) = int(rnd() * 10) next j next i call parray(a) for i = 1 to m s = s + a(i, 1) + a(i, n) next i for i = 2 to n - 1 s = s + a(1, i) + a(m, i) next i print "四周元素之和为:" & send

49、sub(11)运动会private type cj bh as string * 3 cj as singleend typeprivate sub command1_click() dim a(0 to 9) as cj, x$(), y$(), t as cj dim i%, j%, k% x = split("011,095,041,070,008,009,021,061,006,004", ",") y = split("12.4,11.1,13.4,12.1,12.4,10.4,14.4,15.1,15.4,11.4", &

50、quot;,") for i = 0 to 9 a(i).bh = x(i) a(i).cj = y(i) next i print "编号", "成绩" print "=" for i = 0 to 9 print a(i).bh, a(i).cj next i for i = 0 to 8 k = i for j = i + 1 to 9 if a(k).cj > a(j).cj then k = j next j t = a(k): a(k) = a(i): a(i) = t next i print "

51、;=" for i = 0 to 9 print a(i).bh, a(i).cj next iend sub补充一:打印魔方阵option base 1private sub command1_click() dim a%(), i%, j%, k%, n%, x%, y%, p%, q% do n = inputbox("n=") loop while n 2 = n / 2 redim a(n, n) x = 1: y = n 2 + 1 a(x, y) = 1 '先放1 for i = 2 to n * n '再放其它数 p = x: q = y '保留前一个数的位置 x = x - 1 '找下一个数的行位置 if x < 1 then x = n '如果行小于1,让行为n y = y + 1 '找下一个数的列位置 if y > n then y =

温馨提示

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

评论

0/150

提交评论