本帖最后由 aloha20200628 于 2024-3-27 11:08 编辑
第一段是测试代码
第二个封装函数/子过程,是母函数 - 分段求和函数
第三个是封装函数/子过程,是子函数 - 最小段位转换函数- @echo off &setlocal enabledelayedexpansion
- :[Loop] // 测试代码》中文数字转为阿拉伯数字(0-2147483647)
- set "cn="
- set/p cn="输入一个中文数字【零 - 二十一亿四千七百四十八万三千六百四十七】:"
- if not defined cn endlocal&exit/b
- (call :ChangeCnToAn !cn! an)
- echo,n=!an!
- goto [Loop]
-
- :: 分段求和函数》最大输出范围={ 0~2147483647 }
- :ChangeCnToAn // %1=中文数字符串(实值) %2=返回值变量名
- if "%~1"=="零" (set "%~2=0")&exit/b
- set "#cn#=%~1" &set "Yn=100000000"
- if "!#cn#:~-1!"=="亿" (
- (call :ChangeCnToAn9999 !#cn#:~,-1! #n)
- (set/a "%~2=!#n!*!Yn!")&exit/b
- )
- set "#cn#=!#cn#:*亿=!" &set "#cn=!#cn#:零=!"
- if "!#cn:万=!" neq "!#cn!" (for /f "tokens=1-2 delims=万" %%a in ("!#cn!") do (
- (call :ChangeCnToAn9999 %%a nL)
- set/a "#n=!nL!*10000"
- if "%%b" neq "" (
- (call :ChangeCnToAn9999 %%b nR)
- set/a "#n+=!nR!"
- )
- )) else (call :ChangeCnToAn9999 !#cn! #n)
- if "!#cn#!" neq "%~1" for /f "tokens=1-2 delims=亿" %%a in ("%~1") do (
- (call :ChangeCnToAn9999 %%a nY)
- (set/a "#n+=!nY!*!Yn!")
- )
- set "%~2=!#n!" &exit/b
-
- :: 最小段位转换函数》输出范围={ 0~9999 }
- :ChangeCnToAn9999 // %1=中文数字符串(实值) %2=返回值变量名
- if "%~1"=="零" set "%~2=0"&exit/b
- set "cn#=%~1"&set "cn#=!cn#:零=!"&set "uList=十,百,千"
- set/a "n#=0,一=1,二=2,三=3,四=4,五=5,六=6,七=7,八=8,九=9,十=10,百=100,千=1000"
- for /L %%k in (0, 2, 6) do if "!cn#:~%%k,1!" neq "" (
- set "n2c=!cn#:~%%k,2!" &set "nc1=!n2c:~0,1!" &set "nc2=!n2c:~1,1!"
- if "!nc2!"=="" (set/a n#+=!nc1!) else for %%c in (!nc2!) do (
- if "!uList:%%c=!" neq "!uList!" (set/a n#+=!nc1!*!nc2!) else (set/a n#+=!nc1!+!nc2!)
- )
- )
- set "%~2=!n#!" &exit/b
复制代码
|