梦想的边缘

总有梦想,总在追寻,却总在梦想的边缘徘徊,直到有一天,明白了,原来自己的梦想就在这不停追寻之中......

导航

实验11-5答案

Posted on 2005-06-17 19:11  梦想的边缘  阅读(458)  评论(0编辑  收藏  举报

程序界面:



代码:

Private Sub Command1_Click()
   
    '判断用户输入是否合法(大于2的偶数)
    If IsNumeric(Text1.Text) = False Or Val(Text1.Text) <= 2 Or Val(Text1.Text) Mod 2 <> 0 Then
        MsgBox "请输入一个大于2的偶数"
        Text1.Text = ""
        Text1.SetFocus
        Exit Sub
    End If
   
    Dim j As Integer
    Dim t As Integer
    t = Val(Text1.Text)
   
    '若j和t-j均为素数,则说明t可以分解为两个素数之和
    For j = 2 To t - 2
        If SuShu(j) = True And SuShu(t - j) = True Then
            Text2.Text = CStr(j)
            Text3.Text = CStr(t - j)
            Exit For
        End If
    Next j

End Sub

'判断参数a是否为素数,若是素数,返回True;否则返回False
Private Function SuShu(a As Integer) As Boolean
    Dim i As Integer
    For i = 2 To a - 1
        If a Mod i = 0 Then Exit For
    Next i
    If i = a Then
        SuShu = True
    Else
        SuShu = False
    End If
End Function