批量打字测试创建

时间:2013-12-12 01:38:04

标签: batch-file

我正在尝试创建一个显示pangram的批处理脚本 - 在这种情况下,众所周知的“快速棕色狐狸跳过懒狗。” - 用户速度的倍数,将此速度转换为每分钟字母数(来自每秒的字母数),并且还显示错误(如“2个缺失的字母”或“不正确的大小写”)。

到目前为止,我已经能够根据本网站提供的批量计时器进行测试:

@echo off
title 

pause
cls
echo The quick brown fox jumps over the lazy dog.
set starttime=%time%
for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo %starttime: =0%`) do set /a start100s=1%%f*360000+1%%g*6000+1%%h*100+1%%i-36610100
set /p typed=
set stoptime=%time%
for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo %stoptime: =0%`) do set /a stop100s=1%%f*360000+1%%g*6000+1%%h*100+1%%i-36610100
if %stop100s% LSS %start100s% set /a stop100s+=8640000
set /a elapsedtime=%stop100s%-%start100s%
set paddedtime=0%elapsedtime%
echo.
set speed=Typed in %elapsedtime:~0,-2%.%paddedtime:~-2% seconds
echo %speed%

pause
exit

有人可以告诉我如何完成我所拥有的其余部分并给我一个解释,为什么必须做的事情是这样做的?

我的困境:

我已经展示了我已经尝试过的东西,就像在巨大的灰色斑点中看到的那样。我不知道如何使用%typed%语句正确比较字符串The quick brown fox jumps over the lazy dog.if。每次尝试时,批处理文件都会在设置%typed%后退出。

另外,我试图将3/5除以包含%elapsedtime:~0,2%.%paddedtime::~-2%的变量,但该除法无法正常工作。我也无法检查错误,因为此处使用if语句进行比较时出现问题。

所以这是我能够在Magoo的帮助下完成的最终产品:

@echo off
title 

C:
cd %windir%\System32
pause
for /l %%s in (5,-1,2) do (
cls
echo %%s seconds
ping localhost -n 2 >nul)
cls
echo 1 second
ping localhost -n 2 >nul
cls
set pangram=The quick brown fox jumps over the lazy dog.
echo %pangram%
set starttime=%time%
set /p typed= 
set stoptime=%time%
if not defined typed goto zero
for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo %starttime: =0%`) do set /a start100s=1%%f*360000+1%%g*6000+1%%h*100+1%%i
for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo %stoptime: =0%`) do set /a stop100s=1%%f*360000+1%%g*6000+1%%h*100+1%%i
if %stop100s% lss %start100s% set /a stop100s+=8640000
set /a elapsedtime=%stop100s%-%start100s%
set paddedtime=0%elapsedtime%
set total=%elapsedtime:~0,-2%.%paddedtime:~-2% seconds
if "%pangram%"=="%typed%" (set "accuracy=accurately") else (set "accuracy=inaccurately")
set /a nkeys=0
:keys
set /a nkeys+=1
set typed=%typed:~1%
if defined typed goto keys
set /a kps=%nkeys%0000 / %elapsedtime%
set /a kpm=(%nkeys%*600000) / %elapsedtime%
echo.
if %nkeys% neq 44 (echo %nkeys% key typed inaccurately in %total%
goto results) else (echo %nkeys% keys typed %accuracy% in %total%
goto results)
:zero
echo 0 keys typed inaccurately in %total%
echo.
echo 0 keys typed per second
echo 0 keys typed per minute
echo.
goto exit
:results
echo.
echo %kps:~0,-2%.%kps:~-2% keys typed per second
echo %kpm:~0,-2%.%kpm:~-2% keys typed per minute
echo.
goto exit
:exit

pause
exit

此代码可以进一步缩小吗?

1 个答案:

答案 0 :(得分:2)

@echo off
title 

pause
cls
set test=The quick brown fox jumps over the lazy dog.
echo %test%
set starttime=%time%
set /p typed=
set stoptime=%time%
IF NOT DEFINED typed ECHO Nothing typed?&GOTO :EOF 

for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo %starttime: =0%`) do set /a start100s=1%%f*360000+1%%g*6000+1%%h*100+1%%i
for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo %stoptime: =0%`) do set /a stop100s=1%%f*360000+1%%g*6000+1%%h*100+1%%i
if %stop100s% LSS %start100s% set /a stop100s+=8640000
set /a elapsedtime=%stop100s%-%start100s%
set paddedtime=0%elapsedtime%
echo.
set speed=Typed in %elapsedtime:~0,-2%.%paddedtime:~-2% seconds
echo %speed%
if "%test%"=="%typed%" (echo 100%% accurate) else (echo there were errors)
set /a lps=440000 / %elapsedtime%
echo %lps:~0,-2%.%lps:~-2% letters per second
pause
exit

第一项:只需抓住开始/停止时间。稍后处理,否则处理时间包含在已过去的时间内。

第二:无需减去校正因子,因为 stop-factor-(start-factor) = stop-start

第三:比较可能包含分隔符的字符串"quote the strings",因为IF语法严格IF string1 operator string2 ...
此外:如果您使用的是IF condition (thenaction) else (elseaction),那么序列 Space else Space 必须出现在相同的物理线,if也必须出现在同一物理线上。

第四:批量数学仅限于整数。由于您处理的是百分之一秒,4400为100 * 44(44是字符串的长度)。


cls
set test=The quick brown fox jumps over the lazy dog.
echo %test%
set starttime=%time%
set /p typed=
set stoptime=%time%
for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo %starttime: =0%`) do set /a start100s=1%%f*360000+1%%g*6000+1%%h*100+1%%i
for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo %stoptime: =0%`) do set /a stop100s=1%%f*360000+1%%g*6000+1%%h*100+1%%i
if %stop100s% LSS %start100s% set /a stop100s+=8640000
set /a elapsedtime=%stop100s%-%start100s%
set paddedtime=0%elapsedtime%
echo.
set speed=Typed in %elapsedtime:~0,-2%.%paddedtime:~-2% seconds
echo %speed%
if "%test%"=="%typed%" (echo 100%% accurate) else (echo there were errors)
SET /a nletters=0
:clloop
SET /a nletters+=1
SET typed=%typed:~1%
IF DEFINED typed GOTO clloop
ECHO %nletters% typed
set /a lps=%nletters%0000 / %elapsedtime%
echo %lps:~0,-2%.%lps:~-2% letters per second
set /a lpm=(%nletters%*600000) / %elapsedtime%
echo %lpm:~0,-2%.%lpm:~-2% letters per minute

修改,增强和纠正 - lps的显示有问题。

计算字符串类型的长度,自动调整也是如此。

请注意,因为环境变量总是字符串,所以在末尾添加0000与乘以10000相同。

请注意,如果您要将set test命令更改为

set test=%*

然后您可以使用参数

运行此批处理
thisbatch a test string

并且测试将针对“测试字符串”


满足不入场费用的附录:添加了“IF NOT DEFINED typed ..”行。

如果没有输入,typed将保持为空(实际上,它将保持不变,所以一般来说,

set var=defaultvalue
set /p var=
如果在提示符下没有输入,

会将var设置为defaultvalue

请注意使用if not defined&级联指令。

并预测你如何回应& - 你用一个插入符号 ^ “逃避”它:{{1} }


压缩OP的派生代码:

^&