(
在一些朋友的帮助下我做了这个:它采用标准骰子符号表达并给出最小的结果。
Public Function Rollmin(r As Range) As Variant
Application.Volatile
Dim v As String, NewForm As String, deemode As Boolean
Dim dee As String
dice = "d"
glute = False
dicemode = False
v = r.Value
NewForm = "="
For I = 1 To Len(v)
ch = Mid(v, I, 1)
If ch = dice Then
NewForm = NewForm & "*"
dicemode = True
glute = True
Else
If Not IsNumeric(ch) And dicemode Then
dicemode = False
NewForm = NewForm & ""
End If
If glute Then
ch = "1"
glute = False
End If
NewForm = NewForm & ch
End If
Next I
Rollmin = Evaluate(NewForm)
结束功能
当我用一个1位数的骰子(如d4,d6,d8)喂它时,一切正常。
如果我给它1d4 + 4,我得到1 * 1 + 4 = 5
这是我的问题,当我给它一个d10,d12,d20或d100(或任何超过1位数的骰子)时,它只会取第一个数字。给定1d12 + 2,它将给出1 * 12 + 2。给定1d14 + 2,它将给出1 * 14 + 2。
我需要它来考虑" d"之间的所有数字。和下一个操作标志+, - ,/。
我尝试在很多方面修改它但是我不能让它取d和下一个运算符之间的所有数字,它总是取第一个而没有别的,然后在它之后添加其他数字。
确切地说,我需要让我的函数来计算任何骰子表达式的最小值。给定XdY + Z,它会做X * 1 + Z,给定XdY + 2 + XdZ它会做X * 1 + 2 + X * 1.
提前致谢。
编辑1:
通过这样做:
Public Function Rollmin(r As Range) As Variant
Application.Volatile
Dim v As String, NewForm As String, deemode As Boolean
Dim dee As String
dice = "d"
glute = False
dicemode = False
v = r.Value
NewForm = "="
For I = 1 To Len(v)
ch = Mid(v, I, 1)
If ch = dice Then
NewForm = NewForm & "*"
dicemode = True
glute = True
Else
If Not IsNumeric(ch) And dicemode Then
dicemode = False
NewForm = NewForm & "1+"
End If
If glute Then
ch = "1"
glute = False
End If
If IsNumeric(ch) And Not dicemode Then
NewForm = NewForm & "+" & ch
End If
End If
Next I
Rollmin = (NewForm)
End Function
它有效,但表达式中有几个++。这样做会阻止它运作良好,但我想知道如何避免这种情况......
编辑2:不,它不起作用。
编辑3:拆分功能可以有多个分隔符吗?
编辑4:此帖子已经过审核,此评论已添加到我的注意中:"评论:删除了rpg标记,因为此问题与RPG语言无关" 骰子符号仅用于RPG(RolePlaying Games)我没有使用IBM / RPG语言。
答案 0 :(得分:1)
这应该做你想要的:
Public Function Rollmin(byval diceString As String) As Long
dim splitByPlus as Variant
'Example 1d14+2+5d13
'Split string in all addition parts
splitByPlus = Split(diceString, "+")
'splitByPlus is now Array(1d14,2,5d13)
dim i as long
'Loop through each element of the array and adds the current number
for i = LBound(splitByPlus) To Ubound(splitByPlus)
Rollmin = Rollmin + Split(splitByPlus(i), "d")(0)
'First 1d14 gets split to (1,14) and the first element (1) gets added
'Second 2 gets split to (2) and the first element (2) gets added
'Third 5d13 gets split to (5,13) and the first element (5) gets added
next
'Rollmin is now 1+2+5
End Function
编辑:
好的,我的解决方案不容易修改。请改用:
Public Function Rollmin(byval diceString As String) As Long
dim i as long
dim convertedFormula as String
dim currentCharacter as String
for i = 1 to Len(diceString)
currentCharacter = Mid(diceString,i,1)
if currentCharacter = "d" then
'Skip d and all following numbers
while i<Len(diceString) and IsNumeric(Mid(diceString,i+1,1))
i=i+1
wend
else
convertedFormula = convertedFormula & currentCharacter
end if
next
Rollmin = Evaluate("=" & convertedFormula)
End Function