【问题标题】:I want a function in VB SCRIPT to calculate numerology我想要 VB SCRIPT 中的一个函数来计算命理
【发布时间】:2010-01-22 07:30:07
【问题描述】:

我想要一个函数来计算命理。例如,如果我输入“XYZ”,那么我的输出应该是 3。

它变成了 3:

X = 24
Y = 25
Z = 26

在添加时它变成 75 再次加起来 12 (7+5) 再次加起来 3(1+2) 。同样,无论我应该通过什么名字,我的输出都应该是个位数的分数。

【问题讨论】:

  • 这与函数式编程无关。请删除该标签。

标签: algorithm function vbscript


【解决方案1】:

你在这里:

Function Numerology(Str)
  Dim sum, i, char

  ' Convert the string to upper case, so that 'X' = 'x'
  Str = UCase(Str)

  sum = 0
  ' For each character, ...
  For i = 1 To Len(Str)
    ' Check if it's a letter and raise an exception otherwise
    char = Mid(Str, i , 1)
    If char < "A" Or char > "Z" Then Err.Raise 5 ' Invalid procedure call or argument

    ' Add the letter's index number to the sum
    sum = sum + Asc(char) - 64
  Next

  ' Calculate the result using the digital root formula (http://en.wikipedia.org/wiki/Digital_root)
  Numerology = 1 + (sum - 1) Mod 9
End Function

【讨论】:

  • 非常感谢海伦的回答。这是一个很好的答案。谢谢
【解决方案2】:

在 vbscript 中:

Function numerology(literal)

    result = 0
    for i = 1 to Len(literal)
        '' // for each letter, take its ASCII value and substract 64,
        '' so "A" becomes 1 and "Z" becomes 26
        result = result + Asc(Mid(literal, i, 1)) - 64
    next

    '' // while result is bigger than 10, let's sum it's digits
    while(result > 10)
        partial = 0
        for i = 1 to Len(CStr(result))
            partial = partial + CInt(Mid(CStr(result), i, 1))
        next
        result = partial
    wend

    numerology = result

End Function

【讨论】:

  • 仅供参考:// while result is large than 10 部分实际上是一个数字根计算 (en.wikipedia.org/wiki/Digital_root),可以使用一个简单的公式来完成:@987654323 @。 ;)
  • @Helen,很高兴知道这一点!我看到你的回答添加了这个,所以我会保持这个版本不变,好吗?
  • 嗨鲁本斯法里亚斯非常感谢您发布我使用这个答案的答案。该计划非常有效。我刚刚添加了 literal=Ucase(literal) 行以进行正确计算。就是这样。感谢您的帮助
【解决方案3】:

我不知道这可以用来做什么,但无论如何写起来很有趣。

 Private Function CalcStupidNumber(ByVal s As String) As Integer
    s = s.ToLower
    If (s.Length = 1) Then 'End condition
        Try
            Return Integer.Parse(s)
        Catch ex As Exception
            Return 0
        End Try
    End If
    'cover to Values 
    Dim x As Int32
    Dim tot As Int32 = 0
    For x = 0 To s.Length - 1 Step 1
        Dim Val As Integer = ConvertToVal(s(x))
        tot += Val
    Next
    Return CalcStupidNumber(tot.ToString())
End Function

Private Function ConvertToVal(ByVal c As Char) As Integer
    If (Char.IsDigit(c)) Then
        Return Integer.Parse(c)
    End If

    Return System.Convert.ToInt32(c) - 96 ' offest of a 
End Function

【讨论】:

  • 但那是 vb.net,不是 vb 脚本
  • 是的,我没有注意到 vb 脚本部分
猜你喜欢
  • 1970-01-01
  • 2014-11-12
  • 2022-06-17
  • 2015-10-02
  • 1970-01-01
  • 2022-01-12
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多