第六章(过程)编程题

发布时间:2021-10-29 05:01:56

地理与环境科学学院环境工程班卫生哥制作

第六章经典例题及课后*题答案

过程

-1-

第六章练*题及课后题答案

地理与环境科学学院环境工程班卫生哥制作

第六章经典例题及课后*题答案
1.三个数找最大数
Private Sub Command1_click() Dim a As Integer, b As Integer, c As Integer a = Val(InputBox("请输入第 1 个数")) b = Val(InputBox("请输入第 2 个数")) c = Val(InputBox("请输入第 3 个数")) Print "最大数为:"; zds(zds(a, b), c) End Sub Private Function zds(x As Integer, y As Integer) As Integer If x > y Then zds = x Else zds = y End If End Function

2.十个数找最大数
Private Sub Command1_click() Dim a As Integer, x As Integer For i = 1 To 10 a = Val(InputBox("请输入第" & i & "个数")) x = zds(a, x) Next Print x End Sub Private Function zds(x As Integer, y As Integer) As Integer If x > y Then zds = x Else zds = y End If End Function

3.编写一个找出任意一个正整数的因子的程 序
Private Sub Command1_Click() Dim Inta As Integer, St As String
-2第六章练*题及课后题答案

地理与环境科学学院环境工程班卫生哥制作

Inta = Text1.Text Call Factor(Inta, St) Text2.Text = St End Sub Private Sub Factor( ByVal N As Integer, S As String ) Dim I As Integer For I = 1 To N - 1 If N Mod I = 0 Then S = S & Str(I) Next I End Sub

4.编写程序计算 5!+4!+3!+2!+1!的值
Private Sub Form_Click() Dim sum As Integer, i As Integer For i = 5 To 1 Step -1 sum = sum + fact(i) Next i Print "sum="; sum End Sub Public Function fact(n As Integer) As Integer fact = 1 Do While n > 0 fact = fact * n n=n-1 Loop End Function

5.找出 5000 以内的亲密对数。
所谓“亲密对数” ,是指甲数的所有因子和等于乙数,乙数的所有因子和等于甲 数,那么甲、乙两数为亲密对数。 Private Sub Command1_Click() Dim i As Integer, sum1 As Integer, sum2 As Integer For i = 1 To 5000 Call Sum_factors(i, sum1) Call Sum_factors(sum1, sum2) If i = sum2 And i <> sum1 Then Print i, sum1 End If Next i End Sub Private Sub Sum_factors(ByVal n As Integer, sum As Integer) Dim i As Integer
-3第六章练*题及课后题答案

地理与环境科学学院环境工程班卫生哥制作

sum = 0 For i = 1 To n - 1 If n Mod i = 0 Then sum = sum + i End If Next i End Sub

6.编写一段求三角形面积的函数,并多次调 用。 (设三角形三边长为 x,y,z,则面积为 c = 1 / 2 * (x + y + z);area = Sqr(c
* (c - x) * (c - y) * (c - z))) Public Function area(x, y, z) As Single Dim c As Single c = 1 / 2 * (x + y + z) area = Sqr(c * (c - x) * (c - y) * (c - z)) End Function Private Sub Command1_Click() Dim a As Single, b As Single, c As Single, d As Single, s1 As Single, s2 As Single a = InputBox("输入 a:") b = InputBox("输入 b:") c = InputBox("输入 c:") d = InputBox("输入 d:") e = InputBox("输入 e:") f = InputBox("输入 f:") s1 = area(a, b, c): s2 = area(d, e, f) Text1.Text = Val(s1 + s2) End Sub

As Single, e As Single, f

-4-

第六章练*题及课后题答案

地理与环境科学学院环境工程班卫生哥制作

课后题 1-1 (用 sub 做)
Private Sub Command1_Click() Dim x As Long, s As Long, j As Integer For j = 1 To 11 Step 2 Call jc(j, x) s=s+x Next Print "1!+3!+5!+??+11! s :"; End Sub Private Sub jc(n As Integer, p As Long) Dim i As Integer p=1 For i = 1 To n p=p*i Next i End Sub

1-2 (用 function 做)
Private Sub Command1_Click() Dim x As Long, s As Long, i As Integer For i = 1 To 11 Step 2 s = s + jc(i) Next Print "1!+3!+5!+??+11! :";s End Sub Private Function jc(n As Integer) As Long Dim i As Integer p=1 For i = 1 To n p=p*i Next i jc = p End Function

2.(两个 command,一个用选择法,另一个是冒泡法)
Private Sub Command1_Click() '选择法调用 Dim a(1 To 10) As Integer Randomize: Cls Print "原始数据" For i = 1 To 10 a(i) = Int(100 * Rnd) + 1
-5第六章练*题及课后题答案

地理与环境科学学院环境工程班卫生哥制作

Print a(i); Next Print: Print Call xzf(a()) Print "排序结果" For i = 1 To 10 Print a(i); Next End Sub Sub xzf(b() As Integer) For i = 1 To 9 For j = i + 1 To 10 If b(i) > b(j) Then t = b(i): b(i) = b(j): b(j) = t End If Next Next End Sub Private Sub Command2_Click() '冒泡法调用 Dim d(1 To 10) As Integer Randomize: Cls Print "原始数据" For i = 1 To 10 d(i) = Int(100 * Rnd) + 1 Print d(i); Next Print: Print Call mpf(d()) Print "排序结果" For i = 1 To 10 Print d(i); Next End Sub Sub mpf(c() As Integer) For i = 9 To 1 Step -1 For j = 1 To i If c(j) > c(j + 1) Then k = c(j): c(j) = c(j + 1): c(j + 1) = k End If Next Next End Sub

-6-

第六章练*题及课后题答案

地理与环境科学学院环境工程班卫生哥制作

3.
Private Sub Command1_Click() Dim x As Integer Call wbs(x) End Sub Sub wbs(m As Integer) Dim s As Integer For m = 2 To 1000 s=0 For i = 1 To m - 1 If m Mod i = 0 Then s = s + i Next If s = m Then Print m Next End Sub

4.(四个 Command1 做返回用)
Private Sub List1_Click() Select Case List1.ListIndex Case 0 Form2.Show Case 1 Form3.Show Case 2 Form4.Show Case 3 Form5.Show End Select Form1.Hide End Sub Private Sub Command1_Click() Form1.Show Form2.Hide End Sub Private Sub Command1_Click() Form1.Show Form3.Hide End Sub Private Sub Command1_Click() Form1.Show Form4.Hide End Sub Private Sub Command1_Click()
-7第六章练*题及课后题答案

地理与环境科学学院环境工程班卫生哥制作

Form1.Show Form5.Hide End Sub Sub main() Form1.Show End Sub

5-1(用递归法)
Private Sub Command1_Click() Dim i As Integer For i = 1 To 10 n=i Print Fibonacci(i); Print "为第" & n & "项" Next End Sub Private Function Fibonacci(n As Integer) As Integer If n < 3 Then Fibonacci = 1 Else Fibonacci = Fibonacci(n - 1) + Fibonacci(n - 2) End If End Function

5-2 (用数组做)
Private Sub Form_click() For i = 1 To 30 List1.AddItem Fib(i) Next End Sub Private Function Fib(n) If n < 3 Then Fib = 1 Else Fib = Fib(n - 1) + Fib(n - 2) End If End Function Private Sub List1_Click() List1.AddItem = Fib

End Sub

5-3
-8第六章练*题及课后题答案

地理与环境科学学院环境工程班卫生哥制作

Private Sub Command1_Click() a = 1: b = 1 List1.AddItem a List1.AddItem b For n = 1 To 28 c=a+b '当 a+b 赋值为 c 时 a=b '这时 b 就变为 a 了 b=c '同样 c 就变为 b 了 List1.AddItem c Next End Sub

-9-

第六章练*题及课后题答案


相关文档

  • 第六章编程题答案
  • 第6章 程序设计语言 *题与答案
  • 第6章循环结构程序设计*题及答案
  • 第6章编程题
  • 核心编程(第二版)第六章课后*题部分答案代码
  • 数值分析 第六章编程题答案
  • 计算机二级C语言教程第6章课后编程题
  • 谭浩强第6章*题C程序设计流程
  • c语言第6章书后*题_编程题
  • 精通Linux C编程课后上机题部分答案 第六章 进程间的通信
  • 猜你喜欢

    电脑版