我想在VB SCRIPT中使用函数来计算数字命理

时间:2010-01-22 07:30:08

标签: algorithm function vbscript

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

以下是3:

X = 24
Y = 25
Z = 26

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

3 个答案:

答案 0 :(得分:5)

你在这里:

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

答案 1 :(得分: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

答案 2 :(得分:1)

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

 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
相关问题