知ing

VB语言程序设计(第三版)

林卓然 编 / 电子工业出版社

边治霏 上传

查看本书

第7章习题


一、单选题

  1. B     2. C     3.B          

4.B      5. C     6. D

7. C      8. B   


二、填空题

  1. EF        

2. 7; 1; 4        

3. (1)10 (2)28 (3)6 (4) 6        

4. Call Comd1_Click

5. (1) n>=16   (2) n\16   (3) Mid(ch, r+1, 1)

编程及上机调试

1. 在窗体上建立四个文本框Text1和一个命令按钮Command1(“检验”)。文本框Text1~Text3用于输入,Text4用于输出结果。

Private Sub Command1_Click()

      Dim a As String, b As String, c As String

      a = Trim(Text1.Text)

      b = Trim(Text2.Text)

      c = Trim(Text3.Text)

    If che(a) And che(b) And che(c) Then

          Text4.Text = Val(a) + Val(b) + Val(c)

      Else

          Text4.Text = "存在非数字字符"

      End If

End Sub

Function che(x As String) As Boolean      '若存在非数字字符,函数值为假

      che = True

      For k = 1 To Len(x)

          s = Mid(x, k, 1)

          If s < "0" Or s > "9" Then

              che = False

              Exit For

          End If

      Next k

End Function

2.在窗体上建立七个文本框Text1~Text7和一个命令按钮Command1(“比较”)。Text1~Text6用于输入,Text7用于输出结果。

Private Sub Command1_Click()

      a = Val(Text1.Text):b = Val(Text2.Text):c = Val(Text3.Text)

      d = Val(Text4.Text):e = Val(Text5.Text):f = Val(Text6.Text)

      Call max(a, b, c, m)

      Call max(m, d, e, m)

      Call max(m, f, f, m)

      Text7.Text = m

End Sub

Sub max(x, y, z, m)             ‘标准模块文件

      If x > y Then

          m = x

      Else

          m = y

      End If

      If m < z Then

          m = z

      End If

End Sub

3. (1) s<>""          (2) Mid(s, p+1)           (3) n

4. 程序代码如下:

Private Sub Form_Load()

      Dim x As Single, n As Integer, y As Double

      x = InputBox("x=", "求X的n次方")

      n = InputBox("n=", "求X的n次方")

      y = Power(x, n)

      Show

      Print x; "的"; n; "次方="; Format(y, "###,###.##")

End Sub

Function Power(x As Single, n As Integer) As Double

      If n > 1 Then

          Power = Power(x, n - 1) * x

      Else

          Power = x

      End If

End Function

5. 在窗体上设置三个窗体Form1~Form3。Form1中建立了两个文本框Text1(“用户名”)和Text2(“密码”),以及命令按钮Command1(“判断”)。Form2中建立了一个文本框Text1(其Text属性值为“欢迎你使用本系统”)和命令按钮Command1(“返回”)。Form3中建立了一个文本框Text1(其Text属性值为“对不起,请向管理员查询”)和命令按钮Command1(“退出”)。

Dim n As Integer

Private Sub Command1_Click()                   ‘窗体Form1的“判断”

      usern = Trim(Text1.Text):passw = Trim(Text2.Text)

      If usern = "username" And passw = "password" Then

         Form1.Hide

         Form2.Show

      Else

         n = n + 1:Text1.SetFocus

         If n = 3 Then

             Form1.Hide

             Form3.Show

         End If

      End If

      Text1.Text = "":Text2.Text = ""

End Sub

Private Sub Form_Activate()

     Text1.Text = "":Text2.Text = ""

     Text1.SetFocus

End Sub

Private Sub Command1_Click()     ‘窗体Form2的“返回”

     Form2.Hide  

Form1.Show

End Sub

Private Sub Command1_Click()     ‘窗体Form3的“退出”

     Unload Form1

     Unload Form2

Unload Form3

     End

End Sub

     6. (1) Form1窗体模块程序代码:

Private Sub Form_Load()

     Randomize

End Sub

Private Sub Command1_Click()      '"生成随机数"

     Dim s As String

     s = ""

     For i = 1 To 10

        a(i) = Int(11 + 90 * Rnd)

        s = s + Str(a(i)) + ","

     Next i

     Text1.Text = Left(s, Len(s) - 1)

End Sub

Private Sub Command2_Click()      '"求最大数"

     Text2.Text = Max()

End Sub

Private Sub Command3_Click()      '"打开排序窗口"

     Form1.Hide                     '隐藏主窗体

     Form2.Show                     '显示“排序”窗体

End Sub

Private Sub Command4_Click()        '"结束"

    Unload Form1                     '隐藏主窗体

    Unload Form2                     '显示“排序”窗体

    End

End Sub

(2) Form2窗体模块程序代码:

Private Sub Command1_Click()         '“排序”

      Dim t As String

      Call Sort(t)

      Text1.Text = t

End Sub

Private Sub Command2_Click()         '“返回”

      Form2.Hide                      '隐藏“排序”窗体

      Form1.Show                      '显示主窗体

End Sub

(3) 标准模块程序代码:

Public a(1 To 10) As Single

Function Max() As Single             '求最大值

      Dim m As Single

      m = a(1)

      For i = 2 To 10

          If m < a(i) Then

              m = a(i)

          End If

      Next i

      Max = m

End Function

Sub Sort(s As String)                '排序

      s = ""

      For i = 1 To 9

          For j = i + 1 To 10

              If a(i) > a(j) Then

                  t = a(i): a(i) = a(j): a(j) = t

              End If

          Next j

          s = s + Str(a(i)) + ","

      Next i

      s = s + Str(a(10))

End Sub


查看更多