-
-
[分享]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编辑
,原因:
赞赏
他的文章
- [分享]卧龙给你算笔账 96
- [分享]倪海厦说观人术 352
- [分享]蔡志忠《道德经》 647
- [分享]赵玉平:人要学会找到自己的位置,才能坐稳人生的舞台! 582
- [分享]倪海厦(一) 617
谁下载
无
赞赏
雪币:
留言: