首页
社区
课程
招聘
[分享]NxN方格随机排列数字字符演示程序by批处理
发表于: 2025-12-7 14:15 285

[分享]NxN方格随机排列数字字符演示程序by批处理

2025-12-7 14:15
285

演示技术有:最大化显示、输入输出、字符串替换、计算字符串长度、计算平方根、调用函数、跳出循环、空值判断、随机排列、画NxN方格

更新日志:修复了问题,支持一般字符

测试环境:windows10

上代码:


@echo off
setlocal enabledelayedexpansion
title 演示NxN方格随机排列
echo 演示NxN方格随机排列
echo 演示技术有:最大化显示、输入输出、字符串替换、计算字符串长度、计算平方根、调用函数、跳出循环、空值判断、随机排列、画NxN方格
if "%1" equ "" (
start /max call "%0" 1
exit
)
goto :main

:main
echo.
set /p arrs=请输入英文逗号分隔的数字字符,数量可开平方,如1,2,3,4或a,b,c,d或1,a,2,b可生成2x2方格随机排列:
call :transStr
rem echo 数据数组=!arrs!
echo.
set lens=
for /f "tokens=*" %%a in ('powershell -command "& {(!arrs!).length}"') do (
set lens=%%a
)
rem echo 长度=!lens!
call :getN
rem echo N=!n!
if not "!n!" equ "" (
set idxs=
set idxseq=0
for /f "tokens=*" %%b in ('powershell -command "& {$arrs=get-random !arrs! -count !lens!;write-host $arrs -separator \"^,\" -nonewline}"') do (
set arrs=%%b
)
call :transStr
rem echo 数值数组=!arrs!
set /a maxIdx=!lens!-1
for /f "tokens=*" %%c in ('powershell -command "& {$arrs=0..!maxIdx!;$arrs=get-random $arrs -count !lens!;write-host $arrs -separator \"^,\" -nonewline}"') do (
set idxs=%%c
)
rem echo 索引数组=!idxs!
for /l %%d in (1,1,!n!) do (
set lines=
for /l %%e in (1,1,!n!) do (
set o=
for /f "tokens=*" %%f in ('powershell -command "& {$arrs=!arrs!;$idxs=!idxs!;write-host $arrs[$idxs[!idxseq!]]}"') do (
set o=%%f
)
set lines=!lines! !o! 
set /a idxseq+=1
)
echo !lines!
)
echo.
) else (
echo 无法组成NxN的方格!
echo.
)
echo 演示完毕!
echo.
goto :main

:getN
set n=
for /l %%b in (1,1,!lens!) do (
set /a r=%%b*%%b
if !r!==!lens! (
set n=%%b 
exit /b 
)
if !r! gtr !lens! (
exit /b
)
)

:transStr
for /f "tokens=*" %%x in ('powershell -command "& {('!arrs!').replace(\"^,\",\"^'^,^'\")}"') do (
set arrs=%%x
)
set arrs='!arrs!'



[培训]Windows内核深度攻防:从Hook技术到Rootkit实战!

最后于 2025-12-7 15:49 被babyfox编辑 ,原因:
上传的附件:
收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回