[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 批处理用递归实现中文数字解析为阿拉伯数字

前几天看到论坛的一个朋友写了一个中文数字解析为阿拉伯数字的程序,觉得可用递归一写,但从没试过用批处理写递归,也就思考了几天,今天动手写,
没想到一下就成功了。当时那种感觉不错。因为只是写耍,因此文本中只能含中文数字符号,就只能解析中文数字,其他功能没有。最大支持一亿久千万
好像是批处理支持的最大数,如果要想支持更大的数,可以改写,但不想怎了。其实目的只有一个,练练批处理中的递归。不多说了,出代码。
  1. @echo off
  2. setlocal EnableDelayedExpansion
  3. set __yiwuyun.txt=___yiwuyun.txt
  4. for /f "delims=" %%a in ('type 1.txt') do (
  5.   set "string=%%a"
  6.   echo String Before Parse:!string!
  7.   set "string=!string:零=!"
  8.   call :ParseString
  9.   echo String After Parse: !total!
  10. )
  11. exit /b 0
  12. :ParseString
  13. call :DisposeYi
  14. exit /b 0
  15. :DisposeYi
  16. setlocal
  17. call :GetTokenTextNAndCount "^!string^!" "亿"
  18. if !tokenTextCount! equ 1 (
  19.   call :DisposeWan
  20. ) else (
  21.   if not "!tokenText1!"=="" if not "!tokenText2!"=="" (
  22.   set string=!tokenText1!
  23.   call :ParseString
  24.   set /a yiLeft=!total!*100000000
  25.   set string=!tokenText2!
  26.   call :ParseString
  27.   set /a total=!total!+!yiLeft!
  28.   )
  29.   if not "!tokenText1!"=="" if "!tokenText2!"=="" (
  30.   set string=!tokenText1!
  31.   call :ParseString
  32.   set /a total=!total!*100000000
  33.   )
  34. )
  35. endlocal&set /a total=%total%
  36. exit /b 0
  37. :DisposeWan
  38. setlocal
  39. call :GetTokenTextNAndCount "^!string^!" "万"
  40. if !tokenTextCount! equ 1 (
  41.   call :DisposeQian
  42. ) else (
  43.   if not "!tokenText1!"=="" if not "!tokenText2!"=="" (
  44.   set string=!tokenText1!
  45.   call :ParseString
  46.   set /a wanLeft=!total!*10000
  47.   set string=!tokenText2!
  48.   call :ParseString
  49.   set /a total=!total!+!wanleft!
  50.   )
  51.   if not "!tokenText1!"=="" if "!tokenText2!"=="" (
  52.   set string=!tokenText1!
  53.   call :ParseString
  54.   set /a total=!total!*10000
  55.   )
  56. )
  57. endlocal&set /a total=%total%
  58. exit /b 0
  59. :DisposeQian
  60. setlocal
  61. call :GetTokenTextNAndCount "^!string^!" "千"
  62. if !tokenTextCount! equ 1 (
  63.   call :DisposeBai
  64. ) else (
  65.   if not "!tokenText1!"=="" if not "!tokenText2!"=="" (
  66.   set string=!tokenText1!
  67.   call :ParseString
  68.   set /a qianLeft=!total!*1000
  69.   set string=!tokenText2!
  70.   call :ParseString
  71.   set /a total=!total!+!qianLeft!
  72.   )
  73.   if not "!tokenText1!"=="" if "!tokenText2!"=="" (
  74.   set string=!tokenText1!
  75.   call :ParseString
  76.   set /a total=!total!*1000
  77.   )
  78. )
  79. endlocal&set /a total=%total%
  80. exit /b 0
  81. :DisposeBai
  82. setlocal
  83. call :GetTokenTextNAndCount "^!string^!" "百"
  84. if !tokenTextCount! equ 1 (
  85.   call :DisposeShi
  86. ) else (
  87.   if not "!tokenText1!"=="" if not "!tokenText2!"=="" (
  88.   set string=!tokenText1!
  89.   call :ParseString
  90.   set /a baiLeft=!total!*100
  91.   set string=!tokenText2!
  92.   call :ParseString
  93.   set /a total=!baiLeft!+!total!
  94.   )
  95.   if not "!tokenText1!"=="" if "!tokenText2!"=="" (
  96.   set string=!tokenText1!
  97.   call :ParseString
  98.   set /a total=!total!*100
  99.   )
  100. )
  101. endlocal&set /a total=%total%
  102. exit /b 0
  103. :DisposeShi
  104. setlocal
  105. call :GetTokenTextNAndCount "^!string^!" "十"
  106. if !tokenTextCount! equ 1 (
  107.   call :DisposeGe
  108. ) else (
  109.   if not "!tokenText1!"=="" if not "!tokenText2!"=="" (
  110.   set string=!tokenText1!
  111.   call :ParseString
  112.   set /a shiLeft=!total!*10
  113.   set string=!tokenText2!
  114.   call :ParseString
  115.   set /a shiRight=!total!
  116.   set /a total=!shiLeft!+!shiRight!
  117.   )
  118.   if not "!tokenText1!"=="" if "!tokenText2!"=="" (
  119.   set string=!tokenText1!
  120.   call :ParseString
  121.   set /a total=!total!*10
  122.   )
  123.   if "!tokenText1!"=="" if not "!tokenText2!"=="" (
  124.   set string=!tokenText2!
  125.   call :ParseString
  126.   set /a total=!total!+10
  127.   )
  128. )
  129. endlocal&set /a total=%total%
  130. exit /b 0
  131. :DisposeGe
  132. setlocal
  133. call :ParseDigit !string!
  134. set /a total=!num!
  135. endlocal&set /a total=%total%
  136. exit /b 0
  137. :ParseDigit
  138. setlocal
  139. if "%1"=="零" (
  140.   set /a num=0
  141. ) else if "%1"=="一" (
  142.   set /a num=1
  143. ) else if "%1"=="二" (
  144.   set /a num=2
  145. ) else if "%1"=="三" (
  146.   set /a num=3
  147. ) else if "%1"=="四" (
  148.   set /a num=4
  149. ) else if "%1"=="五" (
  150.   set /a num=5
  151. ) else if "%1"=="六" (
  152.   set /a num=6
  153. ) else if "%1"=="七" (
  154.   set /a num=7
  155. ) else if "%1"=="八" (
  156.   set /a num=8
  157. ) else if "%1"=="九" (
  158.   set /a num=9
  159. ) else (
  160.   echo 不是中文纯数字符号,解析的结果可能不正确。
  161.   set /a num=0
  162. )
  163. endlocal&set /a num=%num%
  164. exit /b 0
  165. :GetStringLength
  166. setlocal
  167. set string=!string!
  168. set /a n=0
  169. :startGetStringLength
  170. if not "!string!"=="" (
  171.   set string=!string:~1!
  172.   set /a n=!n!+1
  173.   goto startGetStringLength
  174. )
  175. set /a stringLength=!n!
  176. endlocal&set stringLength=%stringLength%
  177. exit /b 0
  178. :GetTokenPlaceNAndCount
  179. setlocal
  180. set "string=%~1"
  181. set "secondParameter=%~2"
  182. call :GetStringLength
  183. set /a nGetTokenPlaceNAndCount=!stringLength!-1
  184. set /a nToken=1
  185. set /a tokenPlace0=-1
  186. for /l %%a in (0,1,!nGetTokenPlaceNAndCount!) do (
  187.   if "!string:~%%a,1!"=="!secondParameter!" (
  188.      set /a tokenPlace!nToken!=%%a
  189.      set /a nToken+=1
  190.   )
  191. )
  192. set /a tokenPlace!nToken!=!stringLength!
  193. set /a tokenCount=!nToken!-1
  194. type nul>!__yiwuyun.txt!
  195. for /l %%a in (0,1,!nToken!) do (
  196.    echo set /a tokenPlace%%a=!tokenPlace%%a!>>!__yiwuyun.txt!
  197. )
  198. echo set /a tokenCount=!tokenCount!>>!__yiwuyun.txt!
  199. endlocal
  200. for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
  201.   rem echo %%a
  202.   %%a
  203. )
  204. if exist !__yiwuyun.txt! del !__yiwuyun.txt!
  205. exit /b 0
  206. :GetTokenTextNAndCount
  207. setlocal
  208. set "string=%~1"
  209. set "___secondParameter=%~2"
  210. call :GetTokenPlaceNAndCount "^!string^!" "^!___secondParameter^!"
  211. set /a ___next=0
  212. set /a tokenTextCount=!tokenCount!+1
  213. set /a ___n=0
  214. :startGetTokenTextNAndCount
  215.   set /a ___beforePlace=!tokenPlace%___next%!
  216.   set /a ___first=!___beforePlace!+1
  217.   set /a ___next=!___next!+1
  218.   set /a ___afterPlace=!tokenPlace%___next%!
  219.   set /a ___second=!___afterPlace!-!___beforePlace!-1
  220.   set /a ___n=!___n!+1
  221.   if "!string!"=="" (
  222.     set "tokenText!___n!="
  223.   ) else (
  224.     set tokenText!___n!=!string:~%___first%,%___second%!
  225.   )
  226. if !___n! lss !tokenTextCount! goto startGetTokenTextNAndCount
  227. type nul>!__yiwuyun.txt!
  228. for /l %%a in (1,1,!tokenTextCount!) do (
  229.   echo set "tokenText%%a=!tokenText%%a!">>!__yiwuyun.txt!
  230. )
  231. echo set /a tokenTextCount=!tokenTextCount!>>!__yiwuyun.txt!
  232. endlocal
  233. for /f "tokens=1 delims=" %%a in ('type %__yiwuyun.txt%') do (
  234.   rem echo %%a
  235.   %%a
  236. )
  237. if exist %__yiwuyun.txt% del %__yiwuyun.txt%
  238. exit /b 0
复制代码

返回列表