《编程练习一到四及答案.doc》由会员分享,可在线阅读,更多相关《编程练习一到四及答案.doc(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流编程练习一到四及答案.精品文档.编程练习一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
2、 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 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按以上公式计
3、算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 = 9
4、0Print 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.0
5、00)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_Cl
6、ick()Dim x As Double, F As Double, i As Integerx = Val(Text1.Text)F = 1: i = -1While F 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中,单击第二个
7、按钮“求最小数”(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
8、的初始值为数组中的第一个元素值 通过循环依次比较,求最小值 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()
9、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) 0 Then Label3.Caption = 所找的数在第 & Str(k) & 个位置 Else Label3.Caption = 没找到 End If Text2.SetFocus Text2.S
10、elStart = 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
11、.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 Function
12、Private 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
13、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区间的随机整数,显示在第一个
14、文本框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