内容发布更新时间 : 2025/1/7 16:06:12星期一 下面是文章的全部内容请认真阅读。
1.程序参考界面如图2所示,编程时不得增加或减少界面对象或改变对象的种类,窗体及界面元素大小适中,且均可见;
2.运行程序,在文本框1中输入测试数据,单击“运行”按纽,求出其所有因子及所有因子的倒数和,按图示格式输出到多行文本框2中(结果要进行约分);
3.单击“清除”按纽,则将两个文本框清空,焦点置于文本框1上; 4.程序中必须包含一个求其正整数所有因子倒数之和的通用过程。
答:源程序:
Private Sub Command1_Click() Dim x As Long
x = Int(Trim(Text1.Text)) Text2.Text = x & \的所有因子为:\Call yzsum(x) End Sub
Private Sub yzsum(ByVal x As Long)
Dim a() As Integer, idx As Integer, fz As Long, s1 As Long, fm As Long, s2 As Long Dim i As Integer For i = 1 To x
If x Mod i = 0 Then idx = idx + 1
13
ReDim Preserve a(idx) a(idx) = i
Text2 = Text2 & i & \ If fm = 0 Then fm = fm + i fz = 1 Else
s1 = gcd(fm, i) s2 = fm * i / s1
fz = fz * s2 / fm + s2 / i fm = s2 End If End If Next i
s1 = gcd(fm, fz) fz = fz / s1 fm = fm / s1 If fm = 1 Then
Text2 = Text2 & vbCrLf & \所求因子倒数之和为: \Else
Text2 = Text2 & vbCrLf & \所求因子倒数之和为: \End If End Sub
Private Function gcd(ByVal x As Long, ByVal y As Long) As Long Dim r As Long Do
r = x Mod y
x = y y = r
Loop Until (y = 0) gcd = x End Function
Private Sub Command2_Click() Text1.Text = \Text2.Text = \Text1.SetFocus End Sub
15