本帖最后由 aloha20200628 于 2024-8-24 23:07 编辑
再练一把 ‘纯P进行到底’ 的脑力操...
给一个纯P版本的日期输入合法性检查代码,不用外部方法,不用正则匹配
算法简明得益于采用 ‘字典变量’,尽可能考虑了各种防错对策》分隔符及其错位,日月数值溢出,日月相关,平闰年天数区分... 还是回归日月均为两位数的要求。
以下代码采用循环输入以便完成各种测试,直至空回车退出...
- @echo off &setlocal
- set/a "_1=31,_2=28,_3=31,_4=30,_5=31,_6=30,_7=31,_8=31,_9=30,_10=31,_11=30,_12=31,y=%date:~,4%%%4"
- if %y% equ 0 set "_2=29"
- :[md_loop]
- set "md=" &set "c=" &set/p "md=输入日期(月值/日值):"
- if not defined md endlocal&exit/b
- setlocal enabledelayedexpansion &set k=!md:"=!
- if "!k!" neq "!md!" echo,非法&endlocal&goto[md_loop]
- endlocal &if "%md:~5%" neq "" echo,非法&goto[md_loop]
- if "%md:~4,1%"=="" echo,非法&goto[md_loop]
- for /f "tokens=1,2 delims=/" %%1 in ("%md%") do set "m=%%1"&set "d=%%2"
- if "%md%"=="%m%" echo,非法&goto[md_loop]
- for /f "delims=0123456789" %%n in ("%m%%d%") do set "c=1"
- if defined c echo,非法&goto[md_loop]
- if not defined d echo,非法&goto[md_loop]
- if "%md:~2,1%" neq "/" echo,非法&goto[md_loop]
- if "%m:~0,1%" equ "0" if "%m:~1,1%" neq "" set "m=%m:~1%"
- set "_m=_%m%" &set "w=非法" &setlocal enabledelayedexpansion
- if "%d:~0,1%" equ "0" if "%d:~1,1%" neq "" set "d=%d:~1%"
- if %d% gtr 0 if %d% leq !%_m%! set "w=合法"
- echo,!w!&endlocal&goto[md_loop]
复制代码
|