批處理編程的異類——數(shù)學(xué)(Math)
1、數(shù)學(xué)——隨機
1)隨機數(shù)發(fā)生器:運行后可產(chǎn)生一個00~99的隨機整數(shù),并保存于環(huán)境變量 %_random% 中。
如果要產(chǎn)生更復(fù)雜的隨機數(shù),也可以利用這個程序進行修改來得到,比如兩次產(chǎn)生的隨機數(shù)組合,即可得到0000~9999的隨機數(shù);在隨機數(shù)前加上小數(shù)點,即可得到0.00-0.99的隨機小數(shù);將 prompt _$T$_:: 中的第一個下劃線去除,可以得到0~9的隨機數(shù);用debug可以去除某些隨機數(shù)的前綴0。根據(jù)以上方法,即可組合得到任意形式的隨機數(shù)了。
CODE: [Copy to clipboard]:: GenARnd.Bat - Generate a random integer (00~99)
:: Will Sort - 12:06 2005-8-18 - V2 - MSDOS7.10/CMD@WinXP/MSDOS6.22
@echo off
echo exit|%comspec% /k prompt _$T$_::> _GenARnd.bat
echo e 100 "set _random=">_GenARnd.asd
for %%s in (w q) do echo %%s>>_GenARnd.asd
debug _GenARnd.bat < _GenARnd.asd > nul
call _GenARnd.bat
for %%f in (_GenARnd*.*) do del %%f
if not "%_random%"=="" if not [%1]==[@] echo Random number : %_random%
:End
2)隨機陣列發(fā)生器:運行后將生成一張隨機概率分布矩陣圖。
此圖24行63列,由字符“-”(假)和“o”(真)組成,從上到下每三行為一組,呈現(xiàn)出o的分布概率依次降低。其中矩陣中的左圖為真假序列分布,右圖為真假數(shù)目對比。
CODE: [Copy to clipboard]:: RndArray.bat - Generate a serial of random numbers(- or o)
:: Will Sort - 2004/11/24
@echo off
if "%1"==":" goto %2
set no=
set num=0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
:loop
set no=I%no%
set line=
set -=
set o=
for %%c in (%num%) do call %0 : random %no%
echo %line% : %-%%o%
set line=
set -=
set o=
for %%c in (%num%) do call %0 : random %no%
echo %line% : %-%%o%
set line=
set -=
set o=
for %%c in (%num%) do call %0 : random %no%
echo %line% : %-%%o%
if not [%no%]==[IIIIIIII] goto loop
:quit
del _Random.tmp
for %%e in (- o no num line) do set %%e=
goto end
:random
echo. | time > _Random.tmp
::echo exit | %comspec% /k prompt $T>_Random.tmp
goto %3
find ".0"<_Random.tmp>nul
if not errorlevel 1 goto true
:I
find ".3"<_Random.tmp>nul
if not errorlevel 1 goto true
:II
find ".6"<_Random.tmp>nul
if not errorlevel 1 goto true
:III
find ".9"<_Random.tmp>nul
if not errorlevel 1 goto true
:IIII
find ".2"<_Random.tmp>nul
if not errorlevel 1 goto true
:IIIII
find ".5"<_Random.tmp>nul
if not errorlevel 1 goto true
:IIIIII
find ".8"<_Random.tmp>nul
if not errorlevel 1 goto true
:IIIIIII
find ".1"<_Random.tmp>nul
if not errorlevel 1 goto true
:IIIIIIII
find ".4"<_Random.tmp>nul
if not errorlevel 1 goto true
find ".7"<_Random.tmp>nul
if not errorlevel 1 goto true
::del _Random.tmp
:false
set line=%line%-
set -=-%-%
goto end
:true
set line=%line%o
set o=o%o%
goto end
:end
2、數(shù)學(xué)——表達式
1)2K/XP表達式計算批處理,由 Brglng 原創(chuàng)并維護。轉(zhuǎn)帖時精簡了程序中的文檔部分,可以通過以下鏈接查閱完整的代碼和文檔。
{16534}2K/XP表達式計算批處理
http://www.cn-dos.net/forum/viewthread.php?tid=16534
CODE: [Copy to clipboard]:: Eval.cmd - 2K/XP表達式計算批處理
:: Brglng - 2005.8.31
@echo off
setlocal
if /i "%1"=="/A" if "%2"=="" echo 必須指定文件路徑!& goto end
if /i not "%1"=="/A" if /i not "%1"=="/N" if not "%1"=="" goto EvalArgument
echo 請輸入表達式(輸入"Q"退出) & goto inputExpr
:EvalArgument
set /a EvalExpr="%1"
if "%EvalExpr%"=="" echo 表達式輸入錯誤! & goto end
echo %1=%EvalExpr%
if "%2"=="" goto end
echo %1=%EvalExpr% >>"%2"
echo 算式和計算結(jié)果已存入"%2"!
goto end
:inputExpr
set Expr=
set EvalExpr=
set /p Expr=^>
if "%Expr%"=="" goto inputExpr
if /i "%Expr%"=="Q" goto end
set /a EvalExpr="%Expr%"
if "%EvalExpr%"=="" echo 輸入錯誤! & goto inputExpr
echo 計算結(jié)果為:%EvalExpr%
if /i "%1"=="/A" goto autosave
if /i "%1"=="/N" goto inputExpr
:if_save
set SaveEval=
set /p SaveEval=是否將表達式和計算結(jié)果存入文件[Y/N]?
if /i "%SaveEval%"=="Y" goto save
if /i "%SaveEval%"=="N" goto inputExpr
goto if_save
:save
set /p SaveEvalPath=請輸入文件路徑及文件名(如C:\Windows\file.txt。若只輸入文件名則自動存入當前文件夾,留空則為上一次輸入的路徑):
echo "%Expr%"=%EvalExpr% >>"%SaveEvalPath%"
echo 表達式以及計算結(jié)果已存入"%SaveEvalPath%"!
goto inputExpr
:autosave
echo "%Expr%"=%EvalExpr% >>"%2"
goto inputExpr
:end