终于解决浮点运算对字符位数的限制, 理论上可以运算N位的数值范围.
算法:
1. 把输入的两个运算值, 分别以.号做为基准, 向两端补零对齐(在这之前已经去掉了无用的零), 小数和整数部分分别赋给不同变量.
2. 先运算小数部分, 将第1个数的最后一位和第2个数的最后一位做加法运算. 再将运算结果的最后一位赋给结果变量1.,如果运算结果存在两位数, 运算次低位时自动加1. 如此循环, 直到把小数的所有位数运算完成. 得到了小数部分的运算数.
2. 整数部分的运算和小数的一样. 得到结果变量2.
3. 结果变量1, 结果变量2. 将他们中间加.号就得到了准确的运算结果...
欢迎大家测试...
@echo off
if not %1*==* set num1=%1 & set num2=%2 & goto jmp
:input
cls
set num1=
set /p num1=input primary number:
set num2=
set /p num2=input number two number:
:jmp
set numx=%num1%
set numy=%num2%
echo %num1% | find "." >nul || set num1=%num1%.0
echo %num2% | find "." >nul || set num2=%num2%.0
if "%num1:~-1%"=="." set num1=%num1%0
if "%num2:~-1%"=="." set num2=%num2%0
if "%num1:~0,1%"=="." set num1=0%num1%
if "%num2:~0,1%"=="." set num2=0%num2%
set num=0
setlocal enabledelayedexpansion
for %%a in (%num1% %num2%) do (
set /a num+=1
for /f "tokens=1,2 delims=." %%i in ("%%a") do (
if "!num!"=="1" (set one=%%i) else (set one_=%%i)
if "!num!"=="1" (set two=%%j) else (set two_=%%j)
)
)
call :omit %one%
set one=%want%
call :omit %one_%
set one_=%want%
call :raze %two%
set two=%want_%
call :raze %two_%
set two_=%want_%
if "%one%"=="" set one=0
if "%one_%"=="" set one_=0
call :go %one%
set countx=%wish%
call :go %one_%
set countx_=%wish%
if "%two%"=="" set two=0
if "%two_%"=="" set two_=0
call :go %two%
set county=%wish%
call :go %two_%
set county_=%wish%
set after=
set front=0
if %countx% lss %countx_% (
set /a result=%countx_%-%countx%
call :fine !result! %one%
set one=!incept!
) else (
set /a result=%countx%-%countx_%
call :fine !result! %one_%
set one_=!incept!
)
set after=0
set front=
if %county% lss %county_% (
set /a result=%county_%-%county%
call :fine !result! %two%
set two=!incept!
) else (
set /a result=%county%-%county_%
call :fine !result! %two_%
set two_=!incept!
)
set outcome=0
call :scale @%two% @%two_%
set decimal=%numeral:~0,-1%
call :scale @%one% @%one_%
if "%note%"=="0" (
set integer=%numeral:~0,-1%
) else (
set integer=1%numeral:~0,-1%
)
call :raze %decimal%
set decimal=%want_%
if "%decimal%"=="" (set dot=) else (set dot=.)
if "%numx:~0,1%"=="." set numx=0%numx%
if "%numy:~0,1%"=="." set numy=0%numy%
if "%numx:~-1%"=="." set numx=%numx:~0,-1%
if "%numy:~-1%"=="." set numy=%numy:~0,-1%
cls
echo ===============
echo 运算结果^^!
echo ===============
echo.
echo.
echo %numx%+%numy%=%integer%%dot%%decimal%
pause >nul
goto :eof
:scale
set value=%1
set value_=%2
set numeral=
:repeat
set numeral=%outcome:~-1%%numeral%
set note=0%outcome:~0,-1%
if not "%value:~-1%"=="@" (
set xx=!value:~-1!
set value=!value:~0,-1!
)
if not "%value_:~-1%"=="@" (
set yy=!value_:~-1!
set value_=!value_:~0,-1!
call :operation !xx! !yy!
)
goto :eof
:operation
set refer=%1
set refer_=%2
set /a outcome=%refer%+%refer_%+%note%
goto repeat
:go
set remove=%1@
set wish=0
:go_
if not "!remove:~0,1!"=="@" (
set fault=1
for /l %%c in (0,1,9) do (
if "!remove:~0,1!"=="%%c" (
set /a wish+=1
set remove=!remove:~1!
set fault=2
goto go_
)
)
if "!fault!"=="1" goto error
goto go_
)
goto :eof
:fine
set take=%1
set incept=%2
:fine_
if not "%take%"=="0" (
set /a take-=1
set incept=%front%!incept!%after%
goto fine_
)
goto :eof
:omit
set want=%1
:omit_
if "%want:~0,1%"=="0" (
set want=%want:~1%
goto omit_
)
goto :eof
:raze
set want_=%1
:raze_
if "!want_:~-1!"=="0" (
set want_=!want_:~0,-1!
goto raze_
)
goto :eof
:error
cls
echo =====================================
echo.
echo 你的输入了非法字符^^!
echo.
echo =====================================
echo.
echo.
echo 按任意键重新输入...
pause >nul
goto input
[
Last edited by pengfei on 2006-10-11 at 09:07 ]