本帖最后由 abcdshenji 于 2011-8-30 20:58 编辑
发一段自己写的。。没什么效率可言。。主要是体现个人思路。。- @echo off
- call :Add 314649645665.32188 694784.876998798
- pause&exit/b
- :Add number1 number2 ||@by zj
- ::不支持负数
- Setlocal EnableDelayedExpansion
- set "bjs=%~1"&set "js=%~2"
- set "bjs=%bjs:-=%"&set "js=%js:-=%"
- for %%i in (bjs js) do (
- ((echo !%%i!)|findstr /ic:"."||set "%%i=!%%i!.0")>nul
- for /f "tokens=1,2 delims=." %%j in ("!%%i!") do set "z_%%i=%%j"&set "x_%%i=%%k"
- )
- ::::设定两数的整数和小数部分,整数的给z_XXX,小数的给x_XXX
- call :lp z_bjs z_js z_max
- for /l %%i in (1 1 %lg_z_max%) do set "z_bjs=0!z_bjs!"&set "z_js=0!z_js!"
- call :sub !z_bjs:~-%lg_z_max%!%x_bjs% !z_js:~-%lg_z_max%!%x_js%
- ::调用sub求出两数各位数的和,大于10的进1,截取倒数第一位
- goto :EOF
- :sub num1 num2 ||小数部分(左补零对齐)
- Setlocal
- set ".bjs=%~1"&set ".js=%~2"
- call :lp .bjs .js .max
- set/a lg_.max-=1
- for /l %%i in (%lg_.max% -1 0) do (
- set "x=!.bjs:~%%i,1!"&set "y=!.js:~%%i,1!"
- for %%j in (x y p) do (if not defined %%j set "%%j=0")
- set/a bit_sum=!x!+!y!+!p:~,1!
- set "bit_sum=0!bit_sum!"&set "p=!bit_sum:~-2,1!"
- set "sum=!bit_sum:~-1!!sum!"
- )
- call :lp x_bjs x_js x_max
- set "sum=!sum:~,-%lg_x_max%!.!sum:~-%lg_x_max%!"
- if "%sum:~-2,2%" equ ".0" (echo %sum:~,-2%) else echo %sum%
- Endlocal&goto :EOF
- ::最左位则全部截取,这里的sub函数是关键
- :LgStr
- set "n=0"
- :_LgStr
- set "str=%~1"&set/a n+=1
- if not "!str:~%n%!"=="" goto :_LgStr
- goto :EOF
- :lp
- for %%i in (%~1 %~2) do (call :lgstr !%%i!&set "lg_%%i=!n!")
- if !lg_%~1! geq !lg_%~2! (set "lg_%~3=!lg_%~1!") else set "lg_%~3=!lg_%~2!"
- goto :EOF
复制代码 我的思路是模拟笔算,跟分组计算其实也差不多。。把两数的计算全部当成小数部分,前面补零,最后用截取来确定所得数的小数点位置。。大概就这样吧。。纯主观,仅参考。。讲的有点乱不知道楼主有没有明白。。个人觉得大量使用CALL是个遗憾。。但只求完成任务。。
说实在的我我不太会表达,可能注释了还是没清楚,将就的看吧,或者不看也罢 |