gpt4 book ai didi

excel - vba excel 2003中的素数

转载 作者:行者123 更新时间:2023-12-04 21:08:57 26 4
gpt4 key购买 nike

我正在分析网站上的代码,我也尝试过,但似乎不起作用。你能告诉我为什么吗?非常感谢您的帮助。

谢谢

Private Sub CommandButton1_Click()
Dim N, D As Single
Dim tag As String

N = Cells(2, 2)
Select Case N
Case Is < 2
MsgBox "It is not a prime number"
Case Is = 2
MsgBox "It is a prime number"
Case Is > 2
D = 2
Do
If N / D = Int(N / D) Then
MsgBox "It is not a prime number"
tag = "Not Prime"
Exit Do
End If
D = D + 1
Loop While D <= N - 1
If tag <> "Not Prime" Then
MsgBox "It is a prime number"
End If
End Select
End Sub

最佳答案

我看到的最大问题是使用 Single而不是 IntegerLong .素数是正整数,不考虑十进制值(据我所知)。因此,通过使用单数并将它们与划分的整数进行比较,您将面临令人讨厌的边缘情况路由错误。

If N / D = Int(N / D) Then正在使用一种糟糕的方法来查看数字是否为素数。假设每次将浮点数(在本例中为单数)除以除数时,如果它有小数余数,则该余数的整数转换将不相等。但是,在尝试比较答案时,我有时会遇到浮点数的舍入错误,一般来说,我学会了避免使用浮点到整数的转换作为比较数字的一种方式。

这是您可以尝试的一些代码。需要注意的一些事项:

  • 我改变了 N 和 D 的类型,使它们是 Longs 而不是 Singles。这意味着它们不是浮点数并且可能存在舍入误差。
  • 我还明确地将单元格值转换为 long。这样您就可以在代码中知道您没有使用浮点类型。
  • 为了比较,我使用了 Mod ,它返回 N 的剩余部分除以 D .如果余数为 0,则返回 true,并且我们知道我们没有素数。 (注:余数常与 \ 一起使用,它只返回除法结果的整数值。Mod\ 常用于整数类型的精确除法,在这种情况下非常合适。
  • 最后,我更改了您的消息框以显示正在比较的实际数字。由于单元格中的数字是转换的,如果用户输入一个浮点值,他们可以看到它被转换为什么。

  • 您可能还会注意到,当您处理数亿的高数字时,此代码的运行速度比您的代码快得多。
    '
    Sub GetPrime()
    Dim N As Long
    Dim D As Long
    Dim tag As String

    N = CLng(Cells(2, 2))

    Select Case N
    Case Is < 2
    MsgBox N & " is not a prime number"
    Case Is = 2
    MsgBox N & " is a prime number"
    Case Is > 2
    D = 2
    Do
    If N Mod D = 0 Then
    MsgBox N & " is not a prime number"
    tag = "Not Prime"
    Exit Do
    End If
    D = D + 1
    Loop While D <= N - 1
    If tag <> "Not Prime" Then
    MsgBox N & " is a prime number"
    End If
    End Select
    End Sub

    注意:我将程序的名称更改为 GetPrime .在您的代码中,您有:
    Private Sub CommandButton1_Click()

    在上面的行中,您正在定义一个过程(也称为方法或有时仅称为子)。字 Sub表示您在代码中定义一个不返回值的过程。 (有时您可能会看到 Function 而不是 Sub 。这意味着该过程返回一个值,例如 Private Function ReturnANumber() As Long 。)过程( Sub )是在调用时将执行的代码体。另外值得注意的是,一个 excel 宏在 VBA 中存储为 Sub程序。

    在您的代码行中, CommandButton1_Click()是过程的名称。这很可能是通过在 Excel 电子表格中添加一个按钮自动创建的。如果按钮绑定(bind)到 Excel 电子表格, CommandButton1_Click()每次按下按钮时都会执行。

    在您的代码中, Private表示程序的范围。 Private通常意味着不能在它所在的模块或类之外调用该过程。在我的代码中,我省略了 Private因为您可能想调用 GetPrime来自不同的代码模块。

    您在评论中提到您必须将我的程序名称从 GetPrime() 更改为至 CommandButton1_Click() .这当然有效。但是,您也可以简单地调用 GetPrime来自 CommandButton1_Click() ,如下所示:
    Private Sub CommandButton1_Click()
    'The following line of code will execute GetPrime() '
    'Since GetPrime does not have parameters and does not return a value, '
    'all you need to do is put the name of the procedure without the () '
    GetPrime
    End Sub

    'Below is the entire code for the Sub GetPrime() '
    Sub GetPrime()
    'The body of the code goes below: '
    ' ... '

    End Sub

    希望这有助于解释一些关于 VBA 的知识,以加深您的理解!

    关于excel - vba excel 2003中的素数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1763182/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com