编程练习一到四及答案.doc
【精品文档】如有侵权,请联系网站删除,仅供学习与交流编程练习一到四及答案.精品文档.编程练习一1.按指定方法编程序实现如下分段函数。 lnx x5 y= ex 1x5 x1方法一:用单行If语句实现运行时,从文本框Text1输入x值,单击“计算”按钮Command1按以上公式计算y值,计算结果保留三位小数,显示于标签Label2上。Private Sub Command1_Click()Private Sub Command1_Click()Dim x As Single, y As Singlex = Val(Text1.Text)'If x > 5 Then y = Log(x) Else If x >= 1 Then y = Exp(x) Else y = Sqr(Abs(x 3 = 2 * x + 1)If x > 5 Then y = Log(x)If x >= 1 And x <= 5 Then y = Exp(x)If x < 1 Then y = Sqr(Abs(x 3 + 2 * x + 1)Label2.Caption = Format(y, "0.000")End Sub方法二:用块If语句实现运行时,用输入框(InputBox)输入x值,单击“计算”按钮Command1按以上公式计算y值,计算结果保留三位小数,显示于文本框Text2中。Private Sub Command1_Click()Private Sub Command2_Click()Dim x As Single, y As Singlex = Val(InputBox("请输入x的值")If x > 5 Theny = Log(x)ElseIf x >= 1 Theny = Exp(x)Elsey = Sqr(Abs(x 3 + 2 * x + 1)End IfText2.Text = Format(y, "0.000")End SubEnd Sub方法三:用多分支选择语句实现运行时,用输入框(InputBox)输入x值,单击“计算”按钮Command1按以上公式计算y值,计算结果保留三位小数,显示于窗体上。Private Sub Command1_Click()Private Sub Command3_Click()Dim x As Single, y As Singlex = Val(InputBox("请输入x的值")Select Case xCase Is > 5y = Log(x)Case Is >= 1y = Exp(x)Case Elsey = Sqr(Abs(x 3 + 2 * x + 1)End SelectForm1.ScaleHeight = 100Form1.ScaleWidth = 100CurrentX = 35CurrentY = 90Print Format(y, "0.000")End Sub编程练习二1编程序实现:运行时单击窗体,用输入框(InputBox)输入n的值,求的值,计算结果打印在当前窗体上。Private Sub Form_Click()1Private Sub Form_Click()Dim n As Integer, s As Single, i As Integern = Val(InputBox("请输入n的值:")For i = 1 To n s = s + 1 / (2 * i - 1)Next iCurrentX = 200CurrentY = 2200Print "s=" Format(s, "0.000")End Sub2编程序实现:运行时单击窗体,用文本框Text1输入x的值,求小于x的最大值,计算结果显示于标签Label3上。Private Sub Form_Click()2Private Sub Command1_Click()Dim x As Double, F As Double, i As Integerx = Val(Text1.Text)F = 1While F < x i = i + 1 F = F * (2 * i - 1)WendF = F / (2 * i - 1)Label3.Caption = Str(F)End SubPrivate Sub Command2_Click()Dim x As Double, F As Double, i As Integerx = Val(Text1.Text)F = 1: i = -1While F < x i = i + 2 F = F * i3、编写程序在窗体上输出如下图所示的图形。要求:通过输入框函数InputBox输入打印图形的行数和构成图形的字符,通过循环实现图形输出。Private Sub Command1_Click()Dim N As Integer, S As String, i As IntegerN = Val(InputBox("请输入行数:")S = InputBox("请输入构成图形的字符")For i = 1 To N Print Tab(i); For j = 1 To 2 * N - 2 * i + 1 Print S; Next j PrintNext iEnd SubPrivate Sub Command2_Click()Dim N As Integer, S As String, i As IntegerN = Val(InputBox("请输入行数:")S = InputBox("请输入构成图形的字符")For i = N To 1 Step -1 Print Tab(N - i + 1); For j = 1 To 2 * i - 1 Print S; Next j PrintNext iEnd SubPrivate Sub Command3_Click()Dim N As Integer, S As String, i As IntegerN = Val(InputBox("请输入行数:")S = InputBox("请输入构成图形的字符")For i = N To 1 Step -1 Print Tab(N - i + 1); String(2 * i - 1, S)Next iEnd Sub编程练习三1编程序实现:运行时,单击命令按钮Command1从键盘输入N个学生的成绩并显示于带水平滚动条的文本框Text1中,统计其中大于平均分的学生人数并显示于标签Label2中。1Dim A() As Single, N As IntegerPrivate Sub command1_Click()Text1.Text = "" Dim num1 As Integer Dim total As Single, average As Single num1 = 0 N = Val(InputBox("请输入学生的人数:") ReDim A(1 To N) For i = 1 To N A(i) = Val(InputBox("请输入第" & Str(i) & "个学生的成绩", "请输入成绩", "") Text1.Text = Text1.Text & Str(A(i) & " " total = total + A(i) Next i average = total / N For i = 1 To N Select Case A(i) Case Is > average num1 = num1 + 1 End Select Next i Label2.Caption = "大于平均分的学生人数为" & Str(num1)End SubPrivate Sub Command2_Click()EndEnd Sub2. 编写程序实现:运行时,单击第一个按钮“生成随机数”(Command1),生成100个100,199之间的随机整数,存于一维数组B中显示于文本框Text1中,单击第二个按钮“求最小数”(Command2),求出这100个随机整数的最小数,并将结果显示在文本框Text2中。2Dim B(1 To 100) As IntegerPrivate Sub Command1_Click() Text1.Text = "" Randomize For i = 1 To 100 B(i) = Int(Rnd * 100 + 100) Text1.Text = Text1.Text & Str(B(i) & Space(2) Next iEnd SubPrivate Sub Command2_Click() Dim Min As Integer Min = B(1) ' 设变量Min的初始值为数组中的第一个元素值 ' 通过循环依次比较,求最小值 For i = 1 To 100 If B(i) < Min Then Min = B(i) Next i Text2.Text = Str(Min) ' 显示最小值End Sub3编程序实现:运行时,单击命令按钮Command1生成100个-10,10之间的随机整数,保存到数组A中,同时显示于带水平滚动条的文本框Text1中,单击命令按钮Command2对这100个数按从大到小的顺序排序,结果显示于带水平滚动条的文本框Text2中。Dim C(1 To 100) As IntegerPrivate Sub Command1_Click() Text1.Text = "" Randomize For i = 1 To 100 C(i) = Int(Rnd * 21 - 10) Text1.Text = Text1.Text & Str(C(i) & Space(3) Next iEnd SubPrivate Sub Command2_Click() For i = 1 To 99 For j = i + 1 To 100 If C(i) < C(j) Then t = C(i) C(i) = C(j) C(j) = t End If Next j Next i Text2.Text = "" For i = 1 To 100 Text2.Text = Text2.Text & Str(C(i) & Space(3) Next iEnd Sub4下面程序实现:运行时,单击命令按钮Command11生成50个1,100之间的随机整数,保存到一维数组X中,同时显示于一个带水平滚动条的文本框Text1中;单击命令按钮Command2从文本框Text2输入某数,查找该数:如果该数在数组中,则在标签Label3中显示该数在数组中第1次出现的位置,否则在标签Label3中显示“没找到”。4Dim D(1 To 50) As Integer ' 在窗体的通用声明段声明数组APrivate Sub Command1_Click() Randomize For I = 1 To 50 D(I) = Int(Rnd * 100 + 1) Text1.Text = Text1.Text & Str(D(I) Next IEnd SubPrivate Sub Command2_Click()Dim nmber As Integer, k As Integer Number = Val(Text2.Text) k = 0 ' 假设用变量k保存查找位置 ' 顺序查找 For I = 1 To 50 If D(I) = Number Then ' 如果找到 k = I ' 保存找到的位置 Exit For ' 退出循环 End If Next I ' 根据k的值判断查找结果 If k > 0 Then Label3.Caption = "所找的数在第" & Str(k) & "个位置" Else Label3.Caption = "没找到" End If Text2.SetFocus Text2.SelStart = 0 Text2.SelLength = Len(Text2.Text)End Sub编程练习四1(1)编写一个计算表达式 值的函数过程,过程名称为F1。(2)在命令按钮Command1的Click事件过程中编写代码,分别从文本框Text1、Text2、Text3输入a、b、c的值,调用以上函数过程求以下表达式的值,用标签Label5显示计算结果。1Function F1(x, y) F1 = Sqr(Abs(x 3 + y 3)End FunctionPrivate Sub Command1_Click() a = Val(Text1.Text) b = Val(Text2.Text) c = Val(Text3.Text) y = (F1(a, b) + F1(a, c) / F1(b, c) Label5.Caption = Format(y, "0.000")End Sub2.(1)编写一函数过程,过程名称为F2,用于计算 1+2+3+n 的值。(2)在窗体的Click事件过程中,用输入框(InputBox)输入a、b、c的值,调用以上函数过程F2计算以下y值,用消息框(MsgBox函数)显示计算结果。2Function F2(n) Sum = 0 For i = 1 To n Sum = Sum + i Next i F2 = SumEnd FunctionPrivate Sub Form_Click() a = Val(InputBox("请输入a") b = Val(InputBox("请输入b") c = Val(InputBox("请输入c") y = F2(a) * F2(b) * F2(c) MsgBox "y=" & Str(y)End Sub3(1)编写一个Sub过程,过程名称为S1,用于计算n!。(2) 编写命令按钮Command1的Click事件过程实现:运行时,单击Command1,分别从文本框Text1、Text2输入m和n的值,调用以上Sub过程S1计算以下y值,计算结果显示在文本框Text3中。3Sub S1(n, f) f = 1 For i = 1 To n f = f * i Next iEnd SubPrivate Sub Command1_Click() m = Val(Text1.Text) n = Val(Text2.Text) Call S1(m, f1) Call S1(n, f2) y = (f1 - f2) / (f1 + f2) Text3.Text = Format(y, "0.0000")End Sub4(1)编写一个Sub过程,过程名称为S2,用于求任意一维数组元素的最大值和最小值。(2)在“生成随机数”按钮Command1的Click事件过程中生成100个-50,50区间的随机整数,显示在第一个文本框Text1中。(3)在命令按钮Command2的Click事件过程中调用以上Sub过程求这些随机整数的最大值和最小值,显示在第二个文本框Text2中。4Dim a(1 To 100) As IntegerSub S2(x() As Integer, Max, Min) k1 = LBound(x) k2 = UBound(x) Max = x(k1) Min = x(k1) For i = k1 To k2 If x(i) > Max Then Max = x(i) If x(i) < Min Then Min = x(i) Next iEnd SubPrivate Sub Command1_Click() Text1.Text = "" Randomize For i = 1 To 100 a(i) = Int(Rnd * 101 - 50) Text1.Text = Text1.Text & Str(a(i) & " " Next iEnd SubPrivate Sub Command2_Click() Call S2(a(), Max, Min) Text2.Text = "最大数为:" & Str(Max) & " 最小数为:" & Str(Min)End Sub