1、VB程序设计 习题及答案第7章 过程第7章 过 程71 编写程序,利用Sub过程计算下式的值:s=a!+b!+c!a,b,c的值由键盘输入【参考答案】程序代码设计Private Sub Form_Click() Dim s As Integer, n As IntegerFor i = 1 To 3 n = InputBox(n=)Call fact(n, f) s = s + f Next i Print s=; sEnd SubPrivate Sub fact(n, f)f = 1 For i = 1 To n f = f * i Next iEnd Sub运行结果:如a、b、c的值分别输
2、入为4、5、6则程序运行结果如图7-1所示:72 编写程序,打印100200之间的所有素数。要求:(1) 每行输出5个素数。(2) 利用Function过程判断每个数是否为素数,是返回1,否则返回0。【参考答案】程序代码设计Private Sub Form_Click() Print 2; k = 1 For i = 3 To 100 f = prime(i) If f = 1 Then k = k + 1 If k Mod 5 = 0 Then Print i Else Print i; End If Next iEnd SubPrivate Function prime(n) prime
3、= 1 For i = 2 To n - 1 If n Mod i = 0 Then prime = 0: Exit For Next iEnd Function程序运行结果如图7-2所示:73 编写程序,利用Function过程计算下式的值: (1+2+3+m)+(1+2+3+n) y = (1+2+3+p) 【参考答案】程序代码设计Private Sub Form_Click() m = Val(InputBox(输入m:) n = Val(InputBox(输入n:) p = Val(InputBox(输入p:) y = (sum(m) + sum(n) / sum(p) Print s
4、um=; yEnd SubPrivate Function sum(n) s = 0 For i = 1 To n s = s + i Next i sum = sEnd Function运行结果:如输入m值为:20,n值为:15,p值为:10则程序运行结果如图7-3所示:74 编写Function过程计算n!,调用该函数过程计算下式的值:S=1+1/(1+4!)+1/(1+4!+7!)+1/(1+4!+19!)【参考答案】程序代码设计Private Sub Form_Click() s = 0 t = 0 For i = 1 To 19 Step 3 t = t + fact(i) s =
5、s + 1 / t Next i Print s=; sEnd SubPrivate Function fact(n)f = 1 For i = 1 To n f = f * iNext i fact = fEnd Function程序运行结果如图7-4所示。 x x2 x3 xn 7-5计算 ex = 1 + + + + + 1! 2! 3! n! xn 设x=1,2,3,4,5,6 时,分别打印ex 的值,要求精度 0.0001 n! 用Function过程计算n!,调用该过程,计算上式的值。【参考答案】程序代码设计Private Sub Form_Click() For x = 1 To
6、 6Sum = 1 t = 1 n = 0 Do n = n + 1 t = t * xterm = t / fact(n) Sum = Sum + term Loop Until term 0.0001 Print e; x; =; Sum Next xEnd SubPrivate Function fact(n)f = 1 For i = 1 To n f = f * iNext i fact = fEnd Function程序运行结果如图7-5所示:76编写两个Function过程,分别计算40,16,34,26,52的最大公约数和最小公倍数。【参考答案】程序代码设计Private Su
7、b Form_Click() Dim a(1 To 5) As Integera(1) = 40 a(2) = 16 a(3) = 34 a(4) = 26 a(5) = 52 a1 = a(1) a2 = a(2) b = gcd(a1, a2)d = b c = a(1) * a(2) / b For i = 3 To 5a1 = c a2 = a(i) b = gcd(a1, a2) c = c * a(i) / b Next i Print 最小公倍数为:; c For i = 3 To 5 d = gcd(d, a(i) Next i Print 最大公约数为:; dEnd SubP
8、rivate Function gcd(x, y) If x y Then t = x x = yy = t End If r = x Mod yDo While r 0 x = y y = rr = x Mod y Loop gcd = yEnd Function程序运行结果如图7-6所示:77利用Function过程计算下式的值: cos2x (0x) f(x)= int(6x2-5x+1) (0x3) e3-x (3x) 调用该Function过程计算f(f(f(a)+f(f(2+f(b)的值。a,b的值由键盘输入。【参考答案】程序代码设计Private Sub Form_Click()
9、 a = Val(InputBox(输入a的值:) b = Val(InputBox(输入b的值:) t = f(f(f(a) + f(f(2 + f(b) Print 计算结果:; tEnd SubPrivate Function f(x) If x 0 Then f = Cos(2 * x) ElseIf x Max Then Max = x(i, j) m = j End If Next jEnd SubPrivate Sub ld(x() As Integer, j, n)Max = x(1, j) n = 1 For i = 2 To 5If x(i, j) Max Then Max
10、 = x(i, j) n = i End If Next iEnd Sub程序运行结果如图7-8所示:79 编写程序,要求该程序在输入某人的编号、实发工资数额后,自动确定发给多少张一百元、五十元、十元、五元、一元、五角、一角、五分、一分的钞票,并输出实发工资总额和所需各种钞票的总数目。(要求用Sub过程处理除一百元以外的钞票的数目)【参考答案】程序代码设计Private Sub Form_Click() num = Val(InputBox(输入职工编号:) s$ = InputBox(输入职工工资:) l = Len(s$) s0 = Val(Left$(s$, l - 5) Call fe
11、np(l, s$, s1, s2, s3, s4, s5, s6, s7, s8) Print 职工编号:; num, 职工工资:; s$ Print 100元; s0 Print 50元; s1 Print 10元; s2 Print 5元; s3 Print 1元; s4 Print 5角; s5 Print 1角; s6 Print 5分; s7 Print 1分; s8End SubPrivate Sub fenp(l, s$, s1, s2, s3, s4, s5, s6, s7, s8) l1 = Val(Mid$(s$, l - 4, 1) l2 = Val(Mid$(s$, l - 3, 1) l3 = Val(Mid$(s$, l - 1, 1) l4 = Val(Mid$(s$, l, 1) s1 = l1 5 s2 = l1 Mod 5 s3 = l2 5 s4 = l2 Mod 5 s5 = l3 5 s6 = l3 Mod 5 s7 = l4 5 s8 = l4 Mod 5End Sub程序运行结果如图7-9所示:(注意:输入的工资保留到分)710 编写程序,把任意一个十进制数N转换成分别用二进制、八进制和十六进制表示的数,要求用Sub过程实现数制的转换。【参考答案】程序代
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1