我想要一个函数来计算命理。例如,如果我输入“XYZ”,那么我的输出应该是3。我想要VB脚本中的函数来计算命理
这里是如何成为3:
X = 24
Y = 25
Z = 26
上添加变得75,它再加到12(7 + 5),其再次加起来3(1 + 2)。同样,无论我应该通过哪个名字,我的输出都应该是单个数字的分数。
我想要一个函数来计算命理。例如,如果我输入“XYZ”,那么我的输出应该是3。我想要VB脚本中的函数来计算命理
这里是如何成为3:
X = 24
Y = 25
Z = 26
上添加变得75,它再加到12(7 + 5),其再次加起来3(1 + 2)。同样,无论我应该通过哪个名字,我的输出都应该是单个数字的分数。
我不知道这可能用于什么,但反正写起来很有趣。
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脚本 – 2010-01-22 11:30:06
是啊我没有注意到vb脚本部分 – rerun 2010-01-22 18:36:34
在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结果大于10 *部分实际上是数字根计算(http://en.wikipedia .org/wiki/Digital_root),可以用一个简单的公式来完成:'1 +(result - 1)Mod 9'。 ;) – Helen 2010-01-22 11:23:02
@海伦,很高兴知道!我看到你的回答补充说,所以我会保留这个版本,好吗? – 2010-01-22 11:28:12
嗨鲁本斯法里亚斯 非常感谢你张贴我使用这个答案的答案。该计划非常有效。我刚刚添加了literal = Ucase(文字)行以进行正确的计算。而已。感谢您的帮助 – Deepa 2010-01-25 05:15:54
给你:
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
非常感谢Helen的回答。这是一个很好的答案。谢谢 – Deepa 2010-01-25 05:23:35
这有什么好做的函数式编程。请删除该标签。 – 2010-01-22 07:33:12
@Max现在就完成了。 – 2010-01-22 07:38:01