高分寻求VB高手帮忙解决一下,答案及各语句的意思,谢谢!

2024-11-19 01:37:32
推荐回答(1个)
回答(1):

四、 程序填空题
------------------
1、找整数a和b……

Option Explicit
Dim a As Integer, b As Integer, n As Integer, c As Integer

Private Function Gcd(ByVal a As Integer, ByVal b As Integer) As Integer
Do
c = a Mod b: a = b: b = c
Loop While c Mod 3 > 0 '( )
Gcd = a
End Function

Private Sub form_click()
n = 0
For a = 1 To 49
b = 99 - a '( )
c = Gcd(a, b)
If c Mod 3 = 0 Then
n = n + 1 '( )
Print a, b, c
End If
Next a
Print n
End Sub

-------------

2、十六进制数转换为十进制数
Private Sub form_click()
Dim st As String, dem As Long
st = InputBox("输入一个十六进制数")
dem = convert(st)
Print st; "=>"; dem
End Sub

Private Function convert(s As String) As Long
Dim n As Integer, I As Integer, substring As String * 1
Dim p As Long, k As Long, c1 As Integer

s = UCase(s)
k = 0
n = Len(s) '( )

For I = 1 To n
p = 16 ^ (n - I)
substring = Mid(s, I, 1) '( )

Select Case substring
Case "0" To "9"
k = k + p * Val(substring)
Case "A"
k = k + p * (Asc(substring) - Asc("A") + 10)
Case "B" to "F"
k = k + p * (Asc(substring) - Asc("A") + 10)

End Select
Next
convert = k
End Function

''''''''
这提代码实在有些凌乱且有点残缺,我按大概的意思重写了部分,已测试通过
这只不过是道考试题目,你在实际应用中碰到千万不要用这么傻的方法,下面一句代码即可搞定
dem = Val("&H" & st)

--------------------------

3、找出所有分母………

Private Sub command1_click()
Dim I As Integer, J As Integer
Dim n As Integer, flag As Boolean
n = Val(InputBox("输入分母N:"))

For I = 1 To n - 1
flag = False '另加否则不能实现功能
Call sub1(I, n, flag)
If flag Then '( )
Print CStr(I); "/"; CStr(n)
End If
Next
End Sub

Private Sub sub1(K As Integer, n As Integer, f As Boolean)
Dim I As Integer
If K / n > 1 / 4 And K / n < 1 / 3 Then '( )
For I = 2 To K
If (K Mod I = 0) And (n Mod I = 0) Then '( )
Exit Sub
End If
Next I
f = True
End If
End Sub