2010-01-22 10 views
0

我想要一個函數來計算命理。例如,如果我輸入「XYZ」,那麼我的輸出應該是3。我想要VB腳本中的函數來計算命理

這裏是如何成爲3:

X = 24 
Y = 25 
Z = 26 

上添加變得75,它再加到12(7 + 5),其再次加起來3(1 + 2)。同樣,無論我應該通過哪個名字,我的輸出都應該是單個數字的分數。

+0

這有什麼好做的函數式編程。請刪除該標籤。 – 2010-01-22 07:33:12

+0

@Max現在就完成了。 – 2010-01-22 07:38:01

回答

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 
+2

但是這是vb.net,而不是vb腳本 – 2010-01-22 11:30:06

+0

是啊我沒有注意到vb腳本部分 – rerun 2010-01-22 18:36:34

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

僅供參考:* // while結果大於10 *部分實際上是數字根計算(http://en.wikipedia .org/wiki/Digital_root),可以用一個簡單的公式來完成:'1 +(result - 1)Mod 9'。 ;) – Helen 2010-01-22 11:23:02

+0

@海倫,很高興知道!我看到你的回答補充說,所以我會保留這個版本,好嗎? – 2010-01-22 11:28:12

+0

嗨魯本斯法里亞斯 非常感謝你張貼我使用這個答案的答案。該計劃非常有效。我剛剛添加了literal = Ucase(文字)行以進行正確的計算。而已。感謝您的幫助 – Deepa 2010-01-25 05:15:54

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 
+0

非常感謝Helen的回答。這是一個很好的答案。謝謝 – Deepa 2010-01-25 05:23:35