D
dreamfly1024
Unregistered / Unconfirmed
GUEST, unregistred user!
上午的没写完, 现在补上, 不知道还有没有漏洞
<.
// 测试用例
var fs = [
['(1.8+2)*(3*4)', '(1.8+2)*3*4'],
['3*(((4+5)))', '3*(4+5)'],
['1+1+1', '1+1+1'],
['2/(2/2)', '2/(2/2)'],
['1/((1+1))', '1/(1+1)'],
['1/((5+6))-7', '1/(5+6)-7'],
['(1+2)+3-(4-5)*6*(7*8)/9/(10/11)', '1+2+3-(4-5)*6*7*8/9/(10/11)'],
['(1+2)+3+(3.1+3.2*(4-5)*6*(7*8)/9/(10/11))-(4-5)*6*(7*8)/9/(10/11)', '1+2+3+(3.1+3.2*(4-5)*6*7*8/9/(10/11))-(4-5)*6*7*8/9/(10/11)'],
['(1+2)+3-(((((((((4-5)))*6))*(7*8)))/9))/(10/11)', '1+2+3-(4-5)*6*7*8/9/(10/11)']
]
var Need = false
foreach f = fsdo
var ret = '+' ~ f[0]
// 反复迭代
loop
Need = false
ret = FormatIt(ret)
if not Need then
{exit loop}
end loop
ret = ret.Delete(0, 1)
?? ret ~ /t/t ~ (ret = f[1])
end foreach
function FormatIt(f)
// 嵌套括号
Result = PutOff(f)
function PutOff(f)
Result = ''
var reg = System.Text.Regex('/( ( (?>[^()]+) | (?R) )* /)', 'x')
var mat = reg.Match(f)
while mat.Successdo
Result ~= mat.UnmatchedValue
var newValue = ReplaceIt(mat.Value, '^/( (/(.*/)) /)$', '$1')
//?? newValue
newValue = '(%s)'.Format(PutOff(newValue.SubString(1, newValue.Length-2)))
Result ~= newValue
mat.NextMatch()
end while
Result ~= mat.UnmatchedValue
end function
// 规则定义
// 规则条目
// 前导运算符, 括号内运算符, 后继运算符
var ps = [
['+/-*', '*/'],
['+', '+/-', '+/-']
]
foreach p = psdo
Result = ReplaceIt(Result, GenPattern(p[0], p[1], p[2]), '${A}${B}${C}')
end foreach
end function
function GenPattern(p0, p1, p2)
//var fact = '(?P<F>[/./d]+|/((?P>F)/))'
var fact = '(?P<F> [/./d]+ | /( (?P>F) [+/-*/] (?P>F) /) )'
Result = '(?P<A>[%s]) /( (?P<B>%s ([%s]%s)+)+ /)'
.Format(p0, fact, p1, fact.Replace('F', 'G'))
if p2 <> nil then
Result ~= '(?P<C>[%s]|$)'.Format(p2)
else
Result ~= '(?P<C>)'
end if
end function
function ReplaceIt(f, p, r)
Result =f
while Result.RegexIsMatch(p, 'x')do
Result = Result.RegexReplace(p, r, 'x')
Need = true
end while
end function
.>
<.
// 测试用例
var fs = [
['(1.8+2)*(3*4)', '(1.8+2)*3*4'],
['3*(((4+5)))', '3*(4+5)'],
['1+1+1', '1+1+1'],
['2/(2/2)', '2/(2/2)'],
['1/((1+1))', '1/(1+1)'],
['1/((5+6))-7', '1/(5+6)-7'],
['(1+2)+3-(4-5)*6*(7*8)/9/(10/11)', '1+2+3-(4-5)*6*7*8/9/(10/11)'],
['(1+2)+3+(3.1+3.2*(4-5)*6*(7*8)/9/(10/11))-(4-5)*6*(7*8)/9/(10/11)', '1+2+3+(3.1+3.2*(4-5)*6*7*8/9/(10/11))-(4-5)*6*7*8/9/(10/11)'],
['(1+2)+3-(((((((((4-5)))*6))*(7*8)))/9))/(10/11)', '1+2+3-(4-5)*6*7*8/9/(10/11)']
]
var Need = false
foreach f = fsdo
var ret = '+' ~ f[0]
// 反复迭代
loop
Need = false
ret = FormatIt(ret)
if not Need then
{exit loop}
end loop
ret = ret.Delete(0, 1)
?? ret ~ /t/t ~ (ret = f[1])
end foreach
function FormatIt(f)
// 嵌套括号
Result = PutOff(f)
function PutOff(f)
Result = ''
var reg = System.Text.Regex('/( ( (?>[^()]+) | (?R) )* /)', 'x')
var mat = reg.Match(f)
while mat.Successdo
Result ~= mat.UnmatchedValue
var newValue = ReplaceIt(mat.Value, '^/( (/(.*/)) /)$', '$1')
//?? newValue
newValue = '(%s)'.Format(PutOff(newValue.SubString(1, newValue.Length-2)))
Result ~= newValue
mat.NextMatch()
end while
Result ~= mat.UnmatchedValue
end function
// 规则定义
// 规则条目
// 前导运算符, 括号内运算符, 后继运算符
var ps = [
['+/-*', '*/'],
['+', '+/-', '+/-']
]
foreach p = psdo
Result = ReplaceIt(Result, GenPattern(p[0], p[1], p[2]), '${A}${B}${C}')
end foreach
end function
function GenPattern(p0, p1, p2)
//var fact = '(?P<F>[/./d]+|/((?P>F)/))'
var fact = '(?P<F> [/./d]+ | /( (?P>F) [+/-*/] (?P>F) /) )'
Result = '(?P<A>[%s]) /( (?P<B>%s ([%s]%s)+)+ /)'
.Format(p0, fact, p1, fact.Replace('F', 'G'))
if p2 <> nil then
Result ~= '(?P<C>[%s]|$)'.Format(p2)
else
Result ~= '(?P<C>)'
end if
end function
function ReplaceIt(f, p, r)
Result =f
while Result.RegexIsMatch(p, 'x')do
Result = Result.RegexReplace(p, r, 'x')
Need = true
end while
end function
.>