- 182
- 0
- 约6.97千字
- 约 9页
- 2016-05-23 发布于重庆
- 举报
vb教程答案第七章
1.略
2.略
3.略
4.(1) BASIC
(2) 6 26
2 62
-2 98
(3) 23
47
(4)10 8
12 -10
5 2
12 10
5. 编写一个摄氏温度与华氏温度转换的通用过程。摄氏温度(C)与华氏温度(F)转换的公式如下:F=C×9÷5+32
Private Sub Command1_Click()
Dim c As Single, f As Single
c = Text1.Text
Call zhh(c, f)
Text2.Text = f
End Sub
Private Sub zhh(cs As Single, fs As Single)
fs = cs * 9 / 5 + 32
End Sub
6. 编写一个验证一个数是否是素数的通用过程。
Private Sub Form_Click()
Dim n As Integer, flag As Boolean
n = InputBox(输入一个整数:, 判断素数)
Call sushu(n, flag)
If flag Then
MsgBox Str(n) + 是素数
Else
MsgBox Str(n) + 不是素数
End If
End Sub
Private Sub sushu(m As Integer, f As Boolean)
Dim i As Integer
For i = 2 To Int(m / 2)
If m Mod i = 0 Then Exit For
Next i
If i = Int(m / 2) + 1 Then
f = True
Else
f = False
End If
End Sub
7. 编写程序找出1-100之间的所有孪生素数。
Option Base 1
Option Explicit
Private Sub Form_Click()
Dim prime(50) As Integer, i As Integer, lprime() As Integer, lj As Integer
Dim k As Integer, m As Integer, j As Integer
prime(1) = 2
m = 1
For i = 3 To 99 Step 2
For k = 2 To Sqr(i)
If i Mod k = 0 Then Exit For
Next k
If k Sqr(i) Then
m = m + 1
prime(m) = i
End If
Next i
Call pd(prime, m, lprime, lj)
For i = 1 To lj
Print lprime(i);
If i Mod 2 = 0 Then Print
Next i
End Sub
Private Sub pd(p() As Integer, n As Integer, lp() As Integer, j As Integer)
Dim i As Integer
For i = 1 To n - 1
If p(i + 1) - p(i) = 2 Then
ReDim Preserve lp(j + 2)
lp(j + 1) = p(i)
lp(j + 2) = p(i + 1)
j = j + 2
End If
Next I
End Sub
8.利用随机函数Rnd()生成20个在1~100之间的各不相同的正整数,并在窗体上显示出来。
Private Sub Command1_Click()
Dim a(19) As Integer
Call s(a(), 19)
End Sub
Private Sub s(b() As Integer, n As Integer)
Dim i As Integer, j As Integer
For i = 0 To 19
b(i) = Int(Rnd * 100 + 1)
For j = 0 To i - 1
If b(i) = b(j) Then i = i - 1: Exit For
Next j
Print b(i);
Next i
End Sub
9.将上面生成的20个首尾相连,找出每相邻四个数之和最大的四个数,并按下面格式打印出来:n1+n2+n3+n4=sum,其中n1、n2、n3、n4是所求的四个数,sum是这四个数的和。
Private Sub Command1_Click()
Dim a(19) As Integer, i As Integer, j As Integer, s
原创力文档

文档评论(0)