文档库 最新最全的文档下载
当前位置:文档库 › vb第二版习题册 部分答案

vb第二版习题册 部分答案

Option Explicit
6-1
Dim i As Integer
Dim x As Integer


Private Sub CmdExit_Click()
End
End Sub

Private Sub CmdJudge_Click()
x = Val(txtInput.Text)
For i = 2 To x / 2
If x Mod i = 0 Then Exit For
Next
If x Mod i <> 0 Then
MsgBox Str(x) & "是素数!"
Else
MsgBox Str(x) & "不是素数!"
End If
End Sub


6-3
Option Explicit

Private Sub CmdCalculate_Click()
Dim x As Integer, n As Integer
Dim s As Single, t As Single
s = 1
x = Val(txtX.Text)
n = 1
Do
n = n + 1
t = n * x / x ^ n
s = s + t
If t < 10 ^ -5 Then Exit Do
Loop
txtS = Str(s)
End Sub


Private Sub CmdExit_Click()
End
End Sub




6-3改
Option Explicit

Private Sub CmdCalculate_Click()
Dim x As Integer, n As Integer
Dim s As Single, t As Single
s = 1
x = Val(txtX.Text)
n = 1
Do
n = n + 1
t = (n * x / x ^ n) * (-1) ^ (n - 1)
s = s + t
If Abs(t) < 10 ^ -5 Then Exit Do
Loop
txtS = Str(s)
txtS = Format(txtS, "0.000000000") '0到1之间的小数,例如0.5,运行时小数点前的0本不能显示,用Format函数后能了
End Sub


Private Sub CmdExit_Click()
End
End Sub




6-4
Dim n As Integer



Private Sub CmdClear_Click()
txtS.Text = ""
txtV.Text = ""
txtL.Text = ""
txtS.SetFocus
End Sub

Private Sub CmdExit_Click()
Unload Me
End Sub

Private Sub CmdInvert_Click()
Dim i As Integer
Dim s As String
n = Len(txtS.Text)
For i = n To 1 Step -1
s = s + Mid(txtS.Text, i, 1)
Next i
txtV.Text = s
End Sub

Private Sub CmdLen_Click()
n = Len(txtS.Text)
txtL.Text = n
End Sub



7-10
Option Explicit

Dim A(20) As Integer


Private Sub CmdClear_Click()
Text1 = ""
List2.Clear

End Sub

Private Sub CmdExit_Click()
Unload Me

End Sub

Private Sub CmdForm_Click()

Dim i As Integer, s As String
For i = 1 To 20
A(i) = Int(21 * Rnd) + 20
s = s & A(i) & " "
If i = 10 Then
s = s & Chr(13) & Chr(10)
End If
Next i

Text1 = s


End Sub



Private Sub CmdTong_Click()
Dim t As Integer
Dim B(20 To 40) As Integer, i As Integer
For i = 1 To 20
B(A(i)) = B(A(i)) + 1
Next i

For i = 20 To 40
If B(i) <> 0 Then List2.AddItem i & "出现:" & B(i) & "次"
Next i
End Sub





8-1
Option Explicit

Dim sPhrase As String, sEncrypted As String
Dim iLen As Integer


Private Sub CmdOperate_Click(Index As Integer)
Dim sCurrent As String, sNew As String
Dim sDecrypted As String
Dim x As Integer
If Index = 0 Then
sPhrase = InputBox("请输入短语", "短语将被加密")
iLen = Len(sPhrase)
ElseIf Index = 1 Then
sEncrypted = ""

For x = 1 To iLen
sCurrent = Mid(sPhrase, x, 1)
sNew = Chr(Asc(sCurrent) + 2)
sEncrypted = sEncrypted & sNew
Next x
MsgBox sEncrypted, vbExclamation, "加密的短语"
ElseIf Index = 2 Then
For x = 1 To iLen
sCurrent = Mid(sEncrypted, x, 1)
sNew = Chr(Asc(sCurrent) - 2)
sDecrypted = sDecrypted & sNew
Next x
MsgBox sDecrypted, vbExclamation, "解密的短语"

Else
Unload Me
End If
End Sub





9-4
Option Explicit

Private Sub CmdClear_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub

Private Sub CmdExit_Click()
Unload Me
End Sub


Private Sub Option1_Click(Index As Integer)
Dim a As Integer, b As Integer
Dim m As Integer, d As Integer
a = Text1
b = Text2
m = common(a, b)
If Index = 0 Then
Text3 = m
Else
d = a * b / m
Text3 = d
End If
End Sub
Private Function common(ByVal a As Integer, ByVal b As Integer) As Integer
Dim r As Integer
Do
r = b Mod a
If r = 0 Then Exit Do
b = a
a = r
Loop While r <> 0
common = a
End Function




9-6
Option Explicit
Private Sub CmdSearch_Click()
Dim i As Integer
For i = 3 To 100 Step 2
If prime(i) And prime(i + 2) Then
Print Str(i) & "," & Str(i + 2)
End If
Next i
End Sub
Private Function prime(n As Integer) As Boolean
Dim i As Integer
For i = 2 To n - 1
If n Mod i = 0 Then
prime = False
Exit Function
End If
Next i
prime = True

End Function



9-7
Option Explicit

Private Sub CmdClear_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""

End Sub

Private Sub CmdExit_Click()
End
End Sub

Private Sub CmdTran_Click()
Dim s As Long
Dim bin() As String
Dim i As Integer, k As Integer
For i = 1 To Len(Text2)
k = k + 1
ReDim Preserve bin(k)
bin(k) = Mid(Text2, i, 1)
Next i
Call tran(bin, s)
Text3 = s
End Sub

Private Sub Text1_Change()
Dim n As Integer
n = Val(Text1.Text)
Label3.Caption = Str$(n) + "进制数"
End Sub
Private Sub tran(vary() As String, p As Long)
Dim i As Integer, n As Integer
n = Val(Text1.Text)
For i = 1 To UBound(vary)
If Val(vary(i)) >= 0 And Val(vary(i)) <= 9 Then
p = p + Val(vary(i)) * n ^ (UBound(vary) - i)
End If
If Asc(UCase(vary(i))) >= 65 And Asc(UCase(Val(i))) <= 70 Then
p = p + (Asc(UCase(vary(i))) - 65 + 10) * n ^ (UBound(vary) - i)
End If
Next i

End Sub




10-4
Option Explicit

Dim den As Integer, num As Integer



Private Sub Command1_Click()
lowterm num, den
Text3.Text = CStr(den)
Text4.Text = CStr(num)
End Sub

Private S

ub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End Sub

Private Sub Command3_Click()
End
End Sub

Private Sub Text1_Change()
den = Val(Text1.Text)
End Sub

Private Sub Text2_Change()
num = Val(Text2.Text)
End Sub
Sub lowterm(x As Integer, y As Integer)
Dim gcdvalue As Integer
gcdvalue = gcd(x, y)
If gcdvalue > 1 Then
x = x / gcdvalue
y = y / gcdvalue
End If
End Sub
Private Function gcd(ByVal a1 As Integer, ByVal b1 As Integer)
Dim remainder As Integer
remainder = a1 Mod b1
If remainder = 0 Then
gcd = b1
Else
a1 = b1
b1 = remainder
gcd = gcd(a1, b1)
End If
End Function






10-8
Option Explicit

Private Sub Command1_Click()
Dim n As Integer, m As Integer
For n = 10 To 9999
Call Armstrong(n)
Next n
End Sub

Private Sub Command2_Click()
Form1.Cls
End Sub

Private Sub Command3_Click()
Unload Me
End Sub
Private Sub Armstrong(a As Integer)
Dim p As String, q As Integer
Dim k As Integer, b As String
Dim r As Integer
p = CStr(a)
q = Len(p)
For k = 1 To q
b = Mid(p, k, 1)
r = r + (Val(b)) ^ q
Next k
If r = a Then
Print a
End If
End Sub



相关文档