《2022年大学程序设计基础VisualBasic教材课后习题答案》由会员分享,可在线阅读,更多相关《2022年大学程序设计基础VisualBasic教材课后习题答案(8页珍藏版)》请在金锄头文库上搜索。
1、学习必备欢迎下载 教材课后习题答案第 1 章一、选择题1 2 3 4 5 6 7 B D B C B B D 第 2 章一、选择题1 2 3 4 5 6 7 8 9 10 B C B C C B C A A B 11 12 13 14 15 16 A B B C A A 二、填空题1.rem 或 _ :2.“” # # 3.5x2-3x-2sinA/3 4.321456 5.300 三、操作题1、程序段:Private Sub Command1_Click() Dim x As Integer, y As Integer Dim s As Long, c As Long x = Text1.T
2、ext y = Text2.Text s = x * y c = (x + y) * 2 Label3.Caption = 长方形的面积为 + Str(s) Label4.Caption = 长方形的周长为 + Str(c) End Sub 3、程序段:Private Sub Command1_Click() Text1.Text = 第一 End Sub Private Sub Command2_Click() Text1.Text = 第二 End Sub 4、程序代码:Private Sub Text1_Change() Text1.MaxLength = 10 End Sub 5、程序
3、代码:Private Sub Text1_Click() 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 1 页,共 8 页学习必备欢迎下载Text1.SelStart = 0 Text1.SelLength = Len(Text1.Text) End Sub 第 3 章一、选择题1 2 3 4 5 6 7 D A D B A D B 二、填空题1. False 2.B2-4*a*c else 三、操作题1从键盘输入三个值,判断它们能否构成三角形的三个边。如果能构成一个三角形,则计算三角形的面积。Dim a As Integer, b As Lon
4、g, c As Long, s As Single, p As Single a = Val(InputBox(请输入一个值:) b = Val(InputBox(请输入一个值:) c = Val(InputBox(请输入一个值:) If (a + b c And a + c b And b + c a) Then p = (a + b + c) / 2 s = Sqr(p * (p - a) * (p - b) * (p - c) Print a & , & b & , & c & 能构成一个三角形, 它的面积为 : & s End If 2编写程序,任意输入一个整数,判定该整数奇偶性。Di
5、m n As Integer n = Val(InputBox(请输入一个整数:) If n Mod 2 = 0 Then Print n & 是偶数 Else Print n & 是奇数 End If 3求一元二次方程的a*x2+b*x+c=0的根。Dim a As Integer, b As Integer, c As Integer, delta As Single, x1 As Single, x2 As Single a = Val(InputBox(请输入一个整数:) b = Val(InputBox(请输入一个整数:) c = Val(InputBox(请输入一个整数:) del
6、ta = b 2 - 4 * a * c If delta = 0 Then x1 = -b / (2 * a) Print 一元二次方程有两个相等的实根x1=x2= & x1 Else If delta 0 Then x1 = (-b + Sqr(delta) / (2 * a) x2 = (-b - Sqr(delta) / (2 * a) Print 一元二次方程有两个不等的实根x1= & x1 & ,x2= & x2 Else 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 2 页,共 8 页学习必备欢迎下载 x1 = (-b) / (2
7、* a) x2 = Sqr(-delta) / (2 * a) Print 一元二次方程有两个不等的虚根x1= & x1 & + & x2 & i,x2= & x1 & - & x2 & i End If End If 4. 任意输入三个数,输出最大数。Dim a As Integer, b As Integer, c As Integer, max As Integer a = Val(InputBox(请输入一个整数:) b = Val(InputBox(请输入一个整数:) c = Val(InputBox(请输入一个整数:) max = a If max b Then max = b E
8、nd If If max c Then max = c End If Print 最大数是 & max 5任意输入三个数,由大到小排序输出。Dim a As Integer, b As Integer, c As Integer, temp As Integer a = Val(InputBox(请输入一个整数:) b = Val(InputBox(请输入一个整数:) c = Val(InputBox(请输入一个整数:) If a c Then Print a, b, c Else If a c Then Print a, c, b Else Print c, a, b End If End
9、If 第 4 章一、选择题1 2 3 4 5 6 7 8 9 10 A B A C D B A D B B 11 12 13 14 15 16 D C D C B B 二、填空题1. 9 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 3 页,共 8 页学习必备欢迎下载2. 1 2 3 3. 7 4. 100+100 ;x mod 5=0 5. 6 6. 5 7. * 2 5 15 30 75 16 32 80 三、编程题1键盘输入几个不同的数,将它们从大到小排序。Dim i%, j%, k%, s(10) As Integer For i = 1
10、 To 10 s(i) = Int(Rnd() * 100) Next For i = 1 To 9 For j = i + 1 To 10 If s(i) s(j) Then k = s(i) s(i) = s(j) s(j) = k End If Next Next For i = 1 To 10 Print s(i); Next 2设计程序,求出S=11+(1+2)+(1+2+3)+ +(1+2+3+4+N)的值。Dim i As Integer, sn As Long, s As Long, n As Integer sn = 0 s = 0 n = InputBox() For i
11、= 1 To n sn = sn + i s = s + sn Next Print s 运行结果:若n 为 10,S为 220。3输入X 的值,求S=x-x3/3!+x5/5!- 的前 20 项的和值。Dim x As Single, i As Integer, s As Double x = InputBox(输入 X值: ) s = x For i = 1 To 19 p = 1 For j = 1 To 2 * i + 1 p = p * j Next s = s + (-1) (i) * x (2 * i + 1) / p Next 精选学习资料 - - - - - - - - -
12、名师归纳总结 - - - - - - -第 4 页,共 8 页学习必备欢迎下载Print s 运行结果:当X为 2 时, S为 0.91( 精确到小数点后两位) 4勾股定理中3 个数的关系是:a2+b2=c2。编写程序,输出30 以内满足上述关系的整数组合,例如3、4、 5 就是一个整数组合。Dim a As Integer, b As Integer, c As Integer, k As Integer For a = 1 To 30 For b = 1 To 30 For c = 1 To 30 If a 2 + b 2 = c 2 Then k = k + 1: Print a, b,
13、 c Next Next Next Print k 运行结果: 22 5输出 1000 以内的完全数。完全数就是它的各个约数之和等于该数本身(如:6=1+2+3,6是完全数)。Dim i As Integer, j As Integer, a As Integer For i = 2 To 1000 a = 0 For j = 1 To i - 1 If i Mod j = 0 Then a = a + j 判断 j 是否能整除i ,如果能,则a 增加 j Next j If a = i Then Print i 判断 i 是否是完全数,如果是,则输出i Next i 运行结果: 6 28 4
14、96 6编程求出所有的水仙花数。水仙花数就是一个三位数,其各位数字立方和等于该数。(如153=13+53+33) 。Dim i As Integer, s As Integer Dim a As Integer, b As Integer, c As Integer Print 100到 999 所有水仙花数:; For i = 100 To 999 a = i 100 取百位数 b = i 10 Mod 10 或 b = i Mod 100 10 取十位数 c = i Mod 10 取个位数 s = a 3 + b 3 + c 3 水仙花数的判断依据 If s = i Then Print
15、i; Next i 运行结果: 100 到 999所有水仙花数:153 370 371 407 7编写程序,输出100300 间的所有素数。Dim i As Integer, j As Integer, c As Integer, k As Integer For i = 100 To 300 For j = 2 To i - 1 If i Mod j = 0 Then Exit For Next 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 5 页,共 8 页学习必备欢迎下载 If i = j Then Print i; k = k + 1 s
16、 = s + i If k Mod 10 = 0 Then Print End If Next Print Print 100300 间总共有 + Str(k) + 个素数, + 和值为 + Str(s) 运行结果:8我国现有人口为13 亿。设年增长率为1,编写程序,计算多少年后增加到20亿。Dim i As Long, s As Single Dim n As Single, m As Single n = InputBox(请输入增长率:) i = 0 s = 13 m = s Do While m 0.000001 i = i + 2 flag = -flag n = 1 / i pi
17、= pi + flag * n Loop Print pi * 4 运行结果: 3.14159465358569 第 5 章一、选择题精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 6 页,共 8 页学习必备欢迎下载1 2 3 4 5 6 7 8 9 10 D C D D B A A B C B 11 12 13 14 15 D B B C B 二、填空题1. 24 2. 7 3. a=30 b=70 4. “123456789 ”三、编程题1 编写一个求3 个数中最大值Max和最小值Min 的过程,然后用这个过程分别求3 个数、5 个数、 7个数
18、中的最大值和最小值。Dim a() Sub maxmin(b() 过程Dim max, min, k max = b(1) min = b(1) For k = 1 To UBound(b) If max b(k) Then min = b(k) End If End If Next Print 最大值 =; max, 最小值 =; min End Sub Private Sub Command1_Click() 主程序n = InputBox(n=) ReDim a(n) For i = 1 To n a(i) = Val(InputBox(a=) Print a(i), Next Prin
19、t Call maxmin(a() End Sub 2编写程序,求SA!+B!+C! ,阶乘的计算分别用Sub 过程和 Function过程两种方法来实现。Public Sub fact2(m, ss) Dim p, k p = 1 For k = 1 To m p = p * k Next ss = ss + p End Sub 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 7 页,共 8 页学习必备欢迎下载Public Function fact1(n) Dim p, k p = 1 For k = 1 To n p = p * k Next
20、 fact1 = p End Function Private Sub Command1_Click() Dim s, a, b, c a = Val(InputBox(a=) b = Val(InputBox(b=) c = Val(InputBox(c=) s = fact1(a) + fact1(b) + fact1(c) 函数Print s ss = 0 Call fact2(a, ss) 过程Call fact2(b, ss) Call fact2(c, ss) Print ss End Sub 3编写一个过程,以整型数作为形参,当该参数为奇数时输出False ,而当该参数为偶数时输出 True。4设 a 为一整数,如果数a 与其平方数的尾部相等,则称 a 为“同构数”。例如 5225,252625,则 5 和 25 都是同构数。 试编写一个Function过程 Automorphic ,其形参为一正整数,判断其是否为同构数,然后用该过程查找11000 内的所有同构数。第 6 章一、选择题1 2 3 4 5 D A C B D 第 7 章一、选择题1 2 3 4 5 6 7 8 D B A A D D B A 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 8 页,共 8 页