# vbs打乱字符串字符排列的顺序

2016/07/09 11:09

## 1、源码

Dim Str, Str1, count, XngShi, tt, t, i, j
count=0
XngShi=Inputbox("请输入字符内容","提示")
If XngShi="" then
Msgbox "输入不能为空!", 4096, "注意"
EndScript
End If
Msgbox "[输入内容]"& XngShi, 4096, "提示"
Do until count>=Len(XngShi)
Randomize
n=Int(Rnd*2*Len(XngShi))  '随机数范围是字符长度的两倍
Str1 = "."& n &"."
If Instr(Str, Str1)=0 then
Execute ("a"&n &"=""" & Mid(XngShi, count+1, 1)  & """")
Str = Str & Str1
Redim Preserve b(count)
b(count)=n
count = count+1
Else
End If
Loop
'解码顺序 变量名&Str的变量名
'冒泡排序
For j=1 to Len(XngShi)-1
For i=Len(XngShi)-1 to j Step -1
If b(i)<b(i-1) then
t=b(i)
b(i)=b(i-1)
b(i-1)=t
End If
Next
Next
For i=0 to Len(XngShi)-1
cc=b(i)
WW=WW & Eval("a"& cc)
'tt = tt & b(i) & vbcrlf '冒泡排序的结果
Next
'Msgbox tt
Msgbox "[随即排列]"& WW, 4096, "提示"
'下面用于还原字符串位置
Dim MyArray
MyArray = Split(Right(left(Str, Len(Str)-1), Len(Str)-2), "..")
' UBound(MyArray)+1 一定等于 Len(XngShi)
Dim xx, yy, zz
For xx=0 to UBound(MyArray)
yy=MyArray(xx)
zz = zz& Eval("a"& yy)
Next
Msgbox "[还原结果]"& zz, 4096, "提示"

0 评论
0 收藏
0