本楼第一段的代码为最新修改的. 功能更加强大, 欢迎大家测试...
整数浮点数分段运算代码:
一般情况下整数位最多可运算9位数, 小数位最多可运算9位数.
小数位整数位加起来最多可运算18位数.
否则会溢出或为无效数字...
该脚本的功能:
1. 可以运算任何实型数值.
2. 有判断错误输入的功能. 如运算数中存在非法字符或当字符位数超过脚本的运算范围时会提示你重新输入.
3. 解决了进制错误的问题.
4. 运算结果格式输出.
@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
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_%
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%
if %countx% gtr 9 goto error
if %countx_% gtr 9 goto error
set after=0
set front=
if %county% lss %county_% (
set count=%county_%
set /a result=%county_%-%county%
call :fine !result! %two%
set two=!incept!
) else (
set count=%county%
set /a result=%county%-%county_%
call :fine !result! %two_%
set two_=!incept!
)
call :omit %two%
set two=%want%
call :omit %two_%
set two_=%want%
call :go %two%
set county=%wish%
call :go %two_%
set county_=%wish%
if %county% gtr 9 goto error
if %county_% gtr 9 goto error
if "%one%"=="" set one=0
if "%one_%"=="" set one_=0
if "%two%"=="" set two=0
if "%two_%"=="" set two_=0
set /a decimal=%two%+%two_%
set carry=0!decimal:~0,-%count%!
set decimal=!decimal:~-%count%!
set refer=%decimal%
call :go %refer%
set countz=%wish%
set /a spare=%count%-%countz%
set after=
set front=0
call :fine %spare% %decimal%
set decimal=%incept%
set /a integer=%one%+%one_%+%carry%
set dot=.
call :raze %decimal%
set decimal=%want_%
if "%decimal%"=="" set dot=
cls
echo ============
echo 运算结果!
echo ============
echo.
echo %num1%+%num2%=%integer%%dot%%decimal%
pause >nul
goto :eof
: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 1. 输入了除阿拉伯数字外的非法字符.
echo.
echo 2. 整数或小数位数超过了9位.
echo.
echo ========================================================
echo.
echo.
echo 按任意键重新输入...
pause >nul
goto input
下面一段也是通用的, 但小数只能运算8位数, 且没有判断错误输入和判断输入的数值是否符合要求的功能.
整数浮点数分段运算代码:
整数位最多可运算9位数, 小数位最多可运算8位数.
小数位整数位加起来最多可运算17位数.
否则会溢出或为无效数字...
可以运算任何类型的数值, 浮点型数值已不受位数限制.
解决了小数第一位为零且后面跟8或9的数字会出现进制错误的问题.
运算结果格式输出!
此段代码已经通用. 欢迎大家测试...
@echo off
set /p number1=请输入运算数一:
set /p number2=请输入运算数二:
echo %number1% | find "." >nul || set number1=%number1%.0
echo %number2% | find "." >nul || set number2=%number2%.0
if "%number1:~-1%"=="." set number1=%number1%0
if "%number2:~-1%"=="." set number2=%number2%0
if "%number1:~0,1%"=="." set number1=0%number1%
if "%number2:~0,1%"=="." set number2=0%number2%
setlocal enabledelayedexpansion
for %%a in (%number1% %number2%) 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 :go %two%
set count=%wish%
call :go %two_%
set count_=%wish%
if %count% lss %count_% (
set /a result=%count_%-%count%
call :fine !result! %two%
set two=!incept!
) else (
set /a result=%count%-%count_%
call :fine !result! %two_%
set two_=!incept!
)
if "%one%"=="" set one=0
if "%one_%"=="" set one_=0
set /a integer=%one%+%one_%
set /a decimal=1%two%+1%two_%
if "%decimal:~0,1%"=="2" (
set decimal=%decimal:~1%
) else (
set decimal=%decimal:~1%
set /a integer=%integer%+1
)
:raze
if "!decimal:~-1!"=="0" (
set decimal=!decimal:~0,-1!
goto raze
)
if "%decimal%"=="" (
set dot=
) else (
set dot=.
)
echo %integer%%dot%%decimal%
pause
exit
:go
set remove=%1
set wish=0
:go_
if not "!remove:~0,1!"=="~" (
for /l %%c in (0,1,9) do (
if "!remove:~0,1!"=="%%c" (
set /a wish+=1
set remove=!remove:~1!
goto go_
)
)
)
goto :eof
:fine
set take=%1
set incept=%2
:fine_
if not "%take%"=="0" (
set /a take-=1
set incept=!incept!0
goto fine_
)
goto :eof
:omit
set want=%1
:again
if "%want:~0,1%"=="0" (
set want=%want:~1%
goto again
)
goto :eof
相关问题讨论:
关于数值运算时的进制问题!
[
Last edited by pengfei on 2006-10-9 at 05:01 ]