当前位置:文档之家› VB教材课后习题答案

VB教材课后习题答案

<大学程序设计基础-Visual Basic>教材课后习题答案第1章一、选择题1 2 3 4 5 6 7B D BC B B D第2章一、选择题1 2 3 4 5 6 7 8 9 10B C B C B C A A A A11 12 13 14A B A A二、填空题1.rem或’ _ :2.“” # #3.5x2-3x-2sinA/34.3214565.300三、操作题1、程序段:Private Sub Command1_Click()Dim x As Integer, y As IntegerDim s As Long, c As Longx = Text1.Texty = Text2.Texts = x * yc = (x + y) * 2Label3.Caption = "长方形的面积为" + Str(s)Label4.Caption = "长方形的周长为" + Str(c)End Sub3、程序段:Private Sub Command1_Click()Text1.Text = "第一"End SubPrivate Sub Command2_Click()Text1.Text = "第二"End Sub4、程序代码:Private Sub Text1_Change()Text1.MaxLength = 10End Sub5、程序代码:Private Sub Text1_Click()Text1.SelStart = 0Text1.SelLength = Len(Text1.Text)End Sub第3章一、选择题1 2 3 4 5 6 7D A D B A D B二、填空题1. False2.B^2-4*a*c else三、操作题1.从键盘输入三个值,判断它们能否构成三角形的三个边。

如果能构成一个三角形,则计算三角形的面积。

Dim a As Integer, b As Long, c As Long, s As Single, p As Singlea = Val(InputBox("请输入一个值:"))b = Val(InputBox("请输入一个值:"))c = Val(InputBox("请输入一个值:"))If (a + b > c And a + c > b And b + c > a) Thenp = (a + b + c) / 2s = Sqr(p * (p - a) * (p - b) * (p - c))Print a & "," & b & "," & c & "能构成一个三角形,它的面积为:" & sEnd If2.编写程序,任意输入一个整数,判定该整数奇偶性。

Dim n As Integern = Val(InputBox("请输入一个整数:"))If n Mod 2 = 0 ThenPrint n & "是偶数"ElsePrint n & "是奇数"End If3.求一元二次方程的a*x^2+b*x+c=0的根。

Dim a As Integer, b As Integer, c As Integer, delta As Single, x1 As Single, x2 As Singlea = Val(InputBox("请输入一个整数:"))b = Val(InputBox("请输入一个整数:"))c = Val(InputBox("请输入一个整数:"))delta = b ^ 2 - 4 * a * cIf delta = 0 Thenx1 = -b / (2 * a)Print "一元二次方程有两个相等的实根x1=x2=" & x1ElseIf delta > 0 Thenx1 = (-b + Sqr(delta)) / (2 * a)x2 = (-b - Sqr(delta)) / (2 * a)Print "一元二次方程有两个不等的实根x1=" & x1 & ",x2=" & x2Elsex1 = (-b) / (2 * a)x2 = Sqr(-delta) / (2 * a)Print "一元二次方程有两个不等的虚根x1=" & x1 & "+" & x2 & "i,x2=" & x1 & "-" & x2 & "i"End IfEnd If4. 任意输入三个数,输出最大数。

Dim a As Integer, b As Integer, c As Integer, max As Integera = Val(InputBox("请输入一个整数:"))b = Val(InputBox("请输入一个整数:"))c = Val(InputBox("请输入一个整数:"))max = aIf max < b Thenmax = bEnd IfIf max < c Thenmax = cEnd IfPrint "最大数是" & max5.任意输入三个数,由大到小排序输出。

Dim a As Integer, b As Integer, c As Integer, temp As Integera = Val(InputBox("请输入一个整数:"))b = Val(InputBox("请输入一个整数:"))c = Val(InputBox("请输入一个整数:"))If a < b Thentemp = aa = bb = tempEnd IfIf b > c ThenPrint a, b, cElseIf a > c ThenPrint a, c, bElsePrint c, a, bEnd IfEnd If第4章一、选择题1 2 3 4 5 6 7 8 9 10A B A C D B A D B B11 12 13 14 15 16D C D C B B二、填空题1. 92. 1 2 33. 74. 100+100;x mod 5=05. 66. 57. * 2 515 30 7516 32 80三、编程题1.键盘输入几个不同的数,将它们从大到小排序。

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 Integersn = 0s = 0n = InputBox("")For i = 1 To nsn = sn + is = s + snNextPrint s运行结果:若n为10,S为220。

3.输入X的值,求S=x-x^3/3!+x^5/5!- …的前20项的和值。

Dim x As Single, i As Integer, s As Doublex = InputBox("输入X值:")s = xFor i = 1 To 19p = 1For j = 1 To 2 * i + 1p = p * jNexts = s + (-1) ^ (i) * x ^ (2 * i + 1) / pNextPrint 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 IntegerFor a = 1 To 30For b = 1 To 30For c = 1 To 30If a ^ 2 + b ^ 2 = c ^ 2 Then k = k + 1: Print a, b, cNextNextNextPrint k运行结果:225.输出1000以内的完全数。

完全数就是它的各个约数之和等于该数本身(如:6=1+2+3,6是完全数)。

Dim i As Integer, j As Integer, a As IntegerFor i = 2 To 1000a = 0For j = 1 To i - 1If i Mod j = 0 Then a = a + j '判断j是否能整除i,如果能,则a增加j Next jIf a = i Then Print i '判断i是否是完全数,如果是,则输出iNext i运行结果:6284966.编程求出所有的水仙花数。

水仙花数就是一个三位数,其各位数字立方和等于该数。

(如153=13+53+33)。

Dim i As Integer, s As IntegerDim a As Integer, b As Integer, c As IntegerPrint "100到999所有水仙花数:";For i = 100 To 999a = 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 i;Next i运行结果:100到999所有水仙花数:153 370 371 4077.编写程序,输出100~300间的所有素数。

Dim i As Integer, j As Integer, c As Integer, k As IntegerFor i = 100 To 300For j = 2 To i - 1If i Mod j = 0 Then Exit ForNextIf i = j ThenPrint i;k = k + 1s = s + iIf k Mod 10 = 0 Then PrintEnd IfNextPrintPrint "100~300间总共有" + Str(k) + "个素数," + "和值为" + Str(s)运行结果:8.我国现有人口为13亿。

相关主题