|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
     『楼 主』:
[原创]实数加减运算脚本!
使用 LLM 解释/回答一下
实数加减运算脚本已更新, 功能更加强大.
1. 可以进行多种加减法运算, 如:(X+Y, X-Y, -X+Y, -X-Y).
2. 加入了错误输入的检测.
3. 运算数不受位数限制, 理论上可以运算N位的范围.
可以替代计算器进行加减法运算. 却不用受位数的限制. 这是该脚本最大的优势.
由于时间关系没有进行全面测试, 不足的地方请指正, 大家一起探讨, 研究~~~!
@echo off
title 实数加减运算脚本
color 27
if not %1*==* set num1=%1 & set num2=%2 & goto jmp
:input
cls
echo ╭──────────────╮
echo │ │
echo ╭─────────┤ 实 数 加 减 运 算 脚 本 ├──────────╮
echo │ │ │ │
echo │ ╰──────────────╯ │
echo │ 该脚本理论上可以对N位的实数进行加减运算。 │
echo │ │
echo │ 运算规则: │
echo │ 输入两个运算数、,程序将自动计算。运算数与默认为加法 │
echo │ 运算。、的默认值为正, 也可在前面加"-"号。例:、 │
echo │ │
echo │ + 加法运算请输入第1个运算数后按回车键。接着输入第2个运算数 │
echo │ 回车即可. 例: │
echo │ - 减法运算请输入第1个运算数后按回车键。接着输入第2个运算数 │
echo │ 注意: 第2个运算数前必须加"-"号. 例: │
echo │ 更多的运算表达式: 、 │
echo │ │
echo │ 注意: 除阿拉伯数字外, 第一位允许输入符号"-", 以及最多一个"."号, │
echo │ 不允许输入其他字符。否则, 都将视为错误输入... │
echo │ │
echo │ author:pengfei@www.cn-dos.net │
echo ╰───────────────────────────────────╯
setlocal enabledelayedexpansion
set num1=
set num2=
set /p num1= input primary number :
set /p num2= input number two number :
:jmp
set numx=%num1%
set numy=%num2%
set auto=
set less=+
set less_=+
set adding=+
set norm=1
set sign=
set sign_=
if "%num1:~0,1%"=="-" (
if "%num2:~0,1%"=="-" (
set num1=!num1:~1!
set num2=!num2:~1!
set adding=
set sign=-
goto start
)
)
if "%num1:~0,1%"=="-" (
set num1=!num1:~1!
set auto=::
set less=-
set less_=-
set norm=2
set sign=-
set sign_=
goto start
)
if "%num2:~0,1%"=="-" (
set num2=!num2:~1!
set auto=::
set less=-
set less_=-
set adding=
set norm=2
set sign_=-
goto start
)
:start
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%
call :jerque "%num1%"
call :jerque "%num2%"
set num=0
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!
)
if "%norm%"=="2" (
call :how %one%%two% %one_%%two_%
)
:recur
if "%mu%"=="2" call :meet
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_%
call :omit %integer%
set integer=%want%
if "%integer%"=="" set integer=0
if "%integer%"=="0" if "%decimal%"=="" set sign=
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%
if "%numx%"=="" set numx=0
if "%numx%"=="-" set numx=-0
if "%numy%"=="-" set numy=-0
cls
echo =============
echo 运算结果^^!
echo =============
echo.
echo.
echo %numx%%adding%%numy%=%sign%%integer%%dot%%decimal%
pause >nul
exit
:scale
set value=@%1
set value_=@%2
set numeral=
:repeat
set outcome=%outcome_%
if "%outcome_:~0,-1%"=="-" (
set /a outcome=10%outcome_%
set note=1
) else (
set note=0
)
set numeral=%outcome:~-1%%numeral%
%auto%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%%less%%refer_%%less_%%note%
goto repeat
:go
set remove=%1@
set wish=0
:go_
if not "!remove:~0,1!"=="@" (
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=%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
:how
set first=%1@
set second=%2@
:how_
if not "%first:~0,1%"=="@" (
set first_=!first:~0,1!
set first=!first:~1!
)
if not "%second:~0,1%"=="@" (
set second_=!second:~0,1!
set second=!second:~1!
call :judg
)
goto :eof
:judg
if !first_! gtr !second_! (
set mu=1
goto recur
) else (
if !first_! lss !second_! (
set mu=2
goto recur
)
)
goto how_
:meet
set middle=%one%
set one=%one_%
set one_=!middle!
set middle_=%two%
set two=%two_%
set two_=!middle_!
set sign=%sign_%
goto :eof
:jerque
for /f "tokens=1* delims=." %%i in (%1) do (
echo %%i%%j|findstr "^*$">nul || goto error
)
goto :eof
:error
cls
echo ======================
echo 运算数不符合要求^^!
echo ======================
echo.
echo.
echo 请按任意键重新输入...
pause >nul
goto input
今天中秋节, 祝联盟所有朋友中秋快乐, 学习进步, 天天开心~~~! 送大家一个礼物"浮点数加减运算脚本".
代码刚写出来, 还有很多不足的地方, 大家一起探讨!
先前的代码有一个致使的缺陷, 现已修正. 请测试...
该脚本实现的功能:
1. 可以对浮点数进行加减运算, 有较好的容错性, 如过滤一些不正常输入.
2. 解决了批处理脚本在读取数值时遇到的进制问题, 详情请看下面"关于数值运算时的进制问题"的帖子.
3. 格式输出运算结果, 更加美观.
4. 该脚本可以在CMD下接收变量输入, 也可以直接运行批处理接收运算字符.
感谢qwe1234567版主提供建议和测试脚本!
@echo off
title 浮点数加减运算
mode con: cols=80 lines=25
color 27
echo ╭───────────────╮
echo │ │
echo ╭─────────┤ 浮 点 数 加 减 运 算 脚 本 ├─────────╮
echo │ │ │ │
echo │ ╰───────────────╯ │
echo │ │
echo │ 该脚本可以对浮点数进行加减法运算: │
echo │ │
echo │ +加法运算请输入第1个运算数后按回车键. 接着输入第2个运算数 │
echo │ 回车即可. │
echo │ │
echo │ -减法运算请输入第1个运算数后按回车键. 接着输入第2个运算数 │
echo │ 第2个运算数前必须加"-"号. │
echo │ │
echo │ 注意: │
echo │ 1.整数位最多可运算9位数, 小数位最多可运算8位数. │
echo │ 2.小数位整数位加起来最多可运算17位数 │
echo │ 否则,将会溢出或数值无效! │
echo │ │
echo │ author:pengfei@www.cn-dos.net │
echo │ │
echo ╰───────────────────────────────────╯
if not %1*==* (
set num1=%1
set num2=%2
goto jmp
)
set /p num1= input primary number:
set /p num2= input number two number:
:jmp
setlocal enabledelayedexpansion
set numx=%num1%
set numy=%num2%
set adding=+
set tack=1
set tack_=1
set norm=
if "%num2:~0,1%"=="-" (
set num2=!num2:~1!
set less=-
set less_=-
set adding=-
set tack=3
set tack_=1
set norm=
)
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%
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 :raze %two%
set two=%rear%
call :raze %two_%
set two_=%rear%
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 "%norm%"==" " (
if 1%one% lss 1%one_% call :metathesis
if 1%one% equ 1%one_% (
if 1%two% lss 1%two_% call :metathesis
)
)
if "%one%"=="" set one=0
if "%one_%"=="" set one_=0
set /a integer=%one%+%less%%one_%
set /a decimal=%tack%%two%+%less_%%tack_%%two_%
if "%decimal:~0,1%"=="2" (
set decimal=!decimal:~1!
) else (
set decimal=!decimal:~1!
set /a integer%adding%=1
)
if "%decimal%"=="" (set dot=) else (set dot=.)
if "%adding%"=="-" set adding=
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 %numx%%adding%%numy%=%sign%%integer%%dot%%decimal%
pause >nul
goto :eof
: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
:raze
set rear=%1
:raze_
if "!rear:~-1!"=="0" (
set rear=!rear:~0,-1!
goto raze_
)
goto :eof
:metathesis
set middle=%one%
set one=%one_%
set one_=!middle!
set middle_=%two%
set two=%two_%
set two_=!middle_!
set sign=-
goto :eof
相关讨论:
[共同参与][挑战思路][批处理处理浮点运算]
关于数值时的进制问题!
Last edited by namejm on 2006-10-19 at 00:39 ]
The real number addition and subtraction operation script has been updated, and the functions are more powerful.
1. Can perform various addition and subtraction operations, such as: (X+Y, X-Y, -X+Y, -X-Y).
2. Added detection of incorrect input.
3. The operands are not limited by the number of digits, and theoretically can operate in the range of N digits.
It can replace a calculator for addition and subtraction operations, but is not limited by the number of digits. This is the greatest advantage of this script.
Due to time constraints, a comprehensive test has not been carried out. Please point out the insufficient places, and everyone can discuss and study together~~~!
@echo off
title Real Number Addition and Subtraction Operation Script
color 27
if not %1*==* set num1=%1 & set num2=%2 & goto jmp
:input
cls
echo ╭──────────────╮
echo │ │
echo ╭─────────┤ 实 数 加 减 运 算 脚 本 ├──────────╮
echo │ │ │ │
echo │ ╰──────────────╯ │
echo │ This script can theoretically perform addition and subtraction operations on real numbers with N digits. │
echo │ │
echo │ Operation Rules: │
echo │ Enter two operands and , and the program will automatically calculate. The operands and are default addition │
echo │ operation. The default values of and are positive, and a "-" sign can also be added in front. For example: , │
echo │ │
echo │ + For addition operation, enter the first operand and press Enter. Then enter the second operand │
echo │ Press Enter to proceed. Example: │
echo │ - For subtraction operation, enter the first operand and press Enter. Then enter the second operand │
echo │ Note: The second operand must be preceded by a "-" sign. Example: │
echo │ More operation expressions: 、 │
echo │ │
echo │ Note: Except for Arabic numerals, the first digit is allowed to enter the symbol "-", and at most one "." sign, │
echo │ Other characters are not allowed. Otherwise, it will be regarded as incorrect input... │
echo │ │
echo │ author:pengfei@www.cn-dos.net │
echo ╰───────────────────────────────────╯
setlocal enabledelayedexpansion
set num1=
set num2=
set /p num1= input primary number :
set /p num2= input number two number :
:jmp
set numx=%num1%
set numy=%num2%
set auto=
set less=+
set less_=+
set adding=+
set norm=1
set sign=
set sign_=
if "%num1:~0,1%"=="-" (
if "%num2:~0,1%"=="-" (
set num1=!num1:~1!
set num2=!num2:~1!
set adding=
set sign=-
goto start
)
)
if "%num1:~0,1%"=="-" (
set num1=!num1:~1!
set auto=::
set less=-
set less_=-
set norm=2
set sign=-
set sign_=
goto start
)
if "%num2:~0,1%"=="-" (
set num2=!num2:~1!
set auto=::
set less=-
set less_=-
set adding=
set norm=2
set sign_=-
goto start
)
:start
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%
call :jerque "%num1%"
call :jerque "%num2%"
set num=0
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!
)
if "%norm%"=="2" (
call :how %one%%two% %one_%%two_%
)
:recur
if "%mu%"=="2" call :meet
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_%
call :omit %integer%
set integer=%want%
if "%integer%"=="" set integer=0
if "%integer%"=="0" if "%decimal%"=="" set sign=
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%
if "%numx%"=="" set numx=0
if "%numx%"=="-" set numx=-0
if "%numy%"=="-" set numy=-0
cls
echo =============
echo Operation Result^^!
echo =============
echo.
echo.
echo %numx%%adding%%numy%=%sign%%integer%%dot%%decimal%
pause >nul
exit
:scale
set value=@%1
set value_=@%2
set numeral=
:repeat
set outcome=%outcome_%
if "%outcome_:~0,-1%"=="-" (
set /a outcome=10%outcome_%
set note=1
) else (
set note=0
)
set numeral=%outcome:~-1%%numeral%
%auto%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%%less%%refer_%%less_%%note%
goto repeat
:go
set remove=%1@
set wish=0
:go_
if not "!remove:~0,1!"=="@" (
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=%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
:how
set first=%1@
set second=%2@
:how_
if not "%first:~0,1%"=="@" (
set first_=!first:~0,1!
set first=!first:~1!
)
if not "%second:~0,1%"=="@" (
set second_=!second:~0,1!
set second=!second:~1!
call :judg
)
goto :eof
:judg
if !first_! gtr !second_! (
set mu=1
goto recur
) else (
if !first_! lss !second_! (
set mu=2
goto recur
)
)
goto how_
:meet
set middle=%one%
set one=%one_%
set one_=!middle!
set middle_=%two%
set two=%two_%
set two_=!middle_!
set sign=%sign_%
goto :eof
:jerque
for /f "tokens=1* delims=." %%i in (%1) do (
echo %%i%%j|findstr "^*$">nul || goto error
)
goto :eof
:error
cls
echo ======================
echo Incorrect operand^^!
echo ======================
echo.
echo.
echo Please press any key to re-enter...
pause >nul
goto input
Today is Mid-Autumn Festival. I wish all friends in the alliance a happy Mid-Autumn Festival, progress in study, and be happy every day~~~! I give you a gift "Floating Point Number Addition and Subtraction Operation Script".
The code has just been written, and there are still many不足之处. Everyone can discuss together!
There was a fatal defect in the previous code, which has now been corrected. Please test...
The functions implemented by this script:
1. Can perform addition and subtraction operations on floating-point numbers, with good fault tolerance, such as filtering some abnormal inputs.
2. Solved the problem of the base system encountered when the batch processing script reads values. For details, please see the post "About the Base System Problem When Performing Numerical Operations" below.
3. Format the output of the operation result, which is more beautiful.
4. This script can receive variable input under CMD, and can also directly run the batch processing to receive the operation characters.
Thanks to moderator qwe1234567 for providing suggestions and testing the script!
@echo off
title Floating Point Number Addition and Subtraction Operation
mode con: cols=80 lines=25
color 27
echo ╭───────────────╮
echo │ │
echo ╭─────────┤ 浮 点 数 加 减 运 算 脚 本 ├─────────╮
echo │ │ │ │
echo │ ╰───────────────╯ │
echo │ │
echo │ This script can perform addition and subtraction operations on floating-point numbers: │
echo │ │
echo │ + For addition operation, enter the first operand and press Enter. Then enter the second operand │
echo │ Press Enter to proceed. │
echo │ │
echo │ - For subtraction operation, enter the first operand and press Enter. Then enter the second operand │
echo │ The second operand must be preceded by a "-" sign. │
echo │ │
echo │ Note: │
echo │ 1. The integer part can be operated up to 9 digits, and the decimal part can be operated up to 8 digits. │
echo │ 2. The sum of the decimal part and the integer part can be operated up to 17 digits │
echo │ Otherwise, it will overflow or the value will be invalid! │
echo │ │
echo │ author:pengfei@www.cn-dos.net │
echo │ │
echo ╰───────────────────────────────────╯
if not %1*==* (
set num1=%1
set num2=%2
goto jmp
)
set /p num1= input primary number:
set /p num2= input number two number:
:jmp
setlocal enabledelayedexpansion
set numx=%num1%
set numy=%num2%
set adding=+
set tack=1
set tack_=1
set norm=
if "%num2:~0,1%"=="-" (
set num2=!num2:~1!
set less=-
set less_=-
set adding=-
set tack=3
set tack_=1
set norm=
)
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%
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 :raze %two%
set two=%rear%
call :raze %two_%
set two_=%rear%
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 "%norm%"==" " (
if 1%one% lss 1%one_% call :metathesis
if 1%one% equ 1%one_% (
if 1%two% lss 1%two_% call :metathesis
)
)
if "%one%"=="" set one=0
if "%one_%"=="" set one_=0
set /a integer=%one%+%less%%one_%
set /a decimal=%tack%%two%+%less_%%tack_%%two_%
if "%decimal:~0,1%"=="2" (
set decimal=!decimal:~1!
) else (
set decimal=!decimal:~1!
set /a integer%adding%=1
)
if "%decimal%"=="" (set dot=) else (set dot=.)
if "%adding%"=="-" set adding=
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 Operation Result^^!
echo =============
echo.
echo %numx%%adding%%numy%=%sign%%integer%%dot%%decimal%
pause >nul
goto :eof
: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
:raze
set rear=%1
:raze_
if "!rear:~-1!"=="0" (
set rear=!rear:~0,-1!
goto raze_
)
goto :eof
:metathesis
set middle=%one%
set one=%one_%
set one_=!middle!
set middle_=%two%
set two=%two_%
set two_=!middle_!
set sign=-
goto :eof
Related Discussions:
[Join in together][Challenge ideas][Batch processing floating-point operation]
About the base system problem when dealing with numbers!
Last edited by namejm on 2006-10-19 at 00:39 ]
附件
1: 浮点数加减运算脚本.rar (2006-10-7 07:05, 1.49 KiB, 下载附件所需积分 1 点
,下载次数: 49)
附件
2: 无限制实数加减运算脚本.rar (2006-10-12 11:07, 2.21 KiB, 下载附件所需积分 1 点
,下载次数: 49)
|
|
2006-10-6 23:42 |
|
|
bg
初级用户
 
积分 118
发帖 34
注册 2006-1-10
状态 离线
|
|
2006-10-7 01:51 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
|
2006-10-7 03:40 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
不行, 9位会溢出...
111111111.111111111+111111111.111111111=222222223.2072745074 (溢出)
111111111.11111111+111111111.11111111=222222222.22222222 (正常)
本脚本最大运算极限是整数位为9位, 小数位为8位, 加起来最多17位数.
若只有整数则最多能运算9位, 如只有小数则最多可以运算8位.
Last edited by pengfei on 2006-10-7 at 06:17 ]
No, 9 digits will overflow...
111111111.111111111+111111111.111111111=222222223.2072745074 (overflow)
111111111.11111111+111111111.11111111=222222222.22222222 (normal)
The maximum operation limit of this script is 9 digits for the integer part and 8 digits for the decimal part, totaling up to 17 digits.
If there are only integers, it can operate up to 9 digits at most, and if there are only decimals, it can operate up to 8 digits at most.
Last edited by pengfei on 2006-10-7 at 06:17 ]
|
|
2006-10-7 04:25 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
发现一个怪现象...
=============
运算结果!
=============
2222222222.222222222-1111111111.111111111=1111111111.111111111
--------------------------------------------------------------------
=============
运算结果!
=============
2222222222.222222222+1111111111.111111111=-961633962.1961633963
Found a strange phenomenon...
=============
Operation result!
=============
2222222222.222222222-1111111111.111111111=1111111111.111111111
--------------------------------------------------------------------
=============
Operation result!
=============
2222222222.222222222+1111111111.111111111=-961633962.1961633963
|
|
2006-10-7 06:25 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
1:我那个代码却可以算小数位后9位,应该是方法的问题,我的是小数点后首位见0才在首位加1,而你的是所有小数位的首位都加1,所以你的代码小数位要少1位!
2:浮点数的减法运算有点问题: 看下下面的错误:
5.69-65.654=-59.6371
53.698-654.95=-600.16585
1: My code can calculate up to 9 decimal places. It should be a problem with the method. I add 1 to the first decimal place only when the first decimal place is 0, while yours adds 1 to the first digit of all decimal places, so your code has one fewer decimal place!
2: There is a problem with the subtraction operation of floating - point numbers: Look at the following errors:
5.69 - 65.654 = - 59.6371
53.698 - 654.95 = - 600.16585
|

我的网络U盘 我的网络第2个U盘
论坛软件下载链接
灵雨飘零论坛
论坛新手必读,所有人的基本行为准则
刷QQ空间人气、留言的小软件 |
|
2006-10-7 06:37 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
原来是执行顺序出错了, 代码已修正.
5.69-65.654=-59.964
53.698-654.95=-601.252
版主的代码为分析是零就添1, 当时我也想采用这种方法, 但稍微麻烦一点. 也就没有使用此方案.
而那种方案当小数位第一位为零时照样只能运算8位小数, 并不是都能运算9位小数. 所以我觉得无论是零与否都添1简单一点, 后面也要少用一个判断. 说明也要少一些文字^_^
Last edited by pengfei on 2006-10-7 at 07:10 ]
It turned out that the execution order was wrong, and the code has been corrected.
5.69 - 65.654 = -59.964
53.698 - 654.95 = -601.252
The version owner's code is to add 1 if it is zero for analysis. At that time, I also wanted to adopt this method, but it was a little more troublesome. So I didn't use this scheme.
And in that scheme, when the first digit of the decimal part is zero, it can only perform 8 - decimal - digit operation, not all can perform 9 - decimal - digit operation. So I think it is simpler to add 1 whether it is zero or not, and one less judgment is needed later. The explanation will also have fewer words ^_^
Last edited by pengfei on 2006-10-7 at 07:10 ]
|
|
2006-10-7 07:04 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
|
2006-10-7 08:11 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
发现的确是这样的, 而我的代码小数第一位为零可以运算9位数, 不为零则只可以运算8位数.
It is indeed like this. And in my code, if the first decimal digit is zero, it can perform operations on 9-digit numbers; if it is not zero, it can only perform operations on 8-digit numbers.
|
|
2006-10-7 08:18 |
|
|
electronixtar
铂金会员
      
积分 7493
发帖 2672
注册 2005-9-2
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
建议pengfei写一个bat的 math library,然后以后大家bat变成就直接include了,^_^
Suggest pengfei to write a bat math library, and then later everyone can directly include it when writing bats, ^_^
|

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>" |
|
2006-10-8 05:25 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Originally posted by pengfei at 2006-10-7 06:25:
发现一个怪现象...
=============
运算结果!
=============
2222222222.222222222-1111111111.111111111=1111111111.111111111
--------------------------- ...
那是因为在CMD里正数是0~2147483647而负数是-2147483648~-1,2147483648=-2147483648,4294967295=-1
set /a a=2147483647+1
-2147483648
set /a a=2147483649+1
-2147483646
Originally posted by pengfei at 2006-10-7 06:25:
Found a strange phenomenon...
=============
Operation result!
=============
2222222222.222222222-1111111111.111111111=1111111111.111111111
--------------------------- ...
That's because in CMD, positive numbers are 0~2147483647 and negative numbers are -2147483648~-1, 2147483648=-2147483648, 4294967295=-1
set /a a=2147483647+1
-2147483648
set /a a=2147483649+1
-2147483646
|

我的网络U盘 我的网络第2个U盘
论坛软件下载链接
灵雨飘零论坛
论坛新手必读,所有人的基本行为准则
刷QQ空间人气、留言的小软件 |
|
2006-10-8 05:57 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
看来是跟CMD的数据类型有直接关系, set /a运算时的数据存储类型应该是长整型. 而22...+11...超过了他的最大存储空间, 就会溢出, 所进的位把符号位填满了. 于是出现负数.
而22..-11...却没达到最大存储容量. 因此执行正常.
Last edited by pengfei on 2006-10-12 at 08:28 ]
It seems to be directly related to the data type of CMD. The data storage type during set /a operation should be long integer. And 22... + 11... exceeds its maximum storage space, so it overflows, and the carried bit fills the sign bit. Thus, a negative number appears.
While 22.. - 11... does not reach the maximum storage capacity. Therefore, it executes normally.
Last edited by pengfei on 2006-10-12 at 08:28 ]
|
|
2006-10-12 08:17 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
 『第 13 楼』:
使用 LLM 解释/回答一下
实数运算脚本已更新在一楼第1段, 功能更加强大, 无任何限制实现各种加减运算!
欢迎大家测试...
The real number operation script has been updated in the first paragraph of the first floor. The function is more powerful, and various addition and subtraction operations can be realized without any restrictions!
Welcome everyone to test...
|
|
2006-10-12 08:27 |
|
|
不得不爱
超级版主
         我爱DOS
积分 5310
发帖 2044
注册 2005-9-26 来自 四川南充
状态 离线
|
   『第 14 楼』:
使用 LLM 解释/回答一下
我也来1个!
@echo off
cls
title 实数加减运算脚本
color 17
setlocal enabledelayedexpansion
if not %1*==* (set num1=%1&if not %2*==* (set num2=%2&goto jmp) ELSE goto err)
:input
echo 实 数 加 减 运 算 脚 本
echo.
echo 该脚本理论上可以对N位的实数进行加减运算
echo.
echo 注意: 除阿拉伯数字外, 第一位允许输入符号"-", 以及最多一个"."号,
echo 不允许输入其他字符。否则, 都将视为错误输入...
echo.
echo 中国DOS联盟论坛 qwe1234567制作
set/p num1=请输入第一个小数:
set/p num2=请输入第二个小数:
:jmp
set fh1=
set fh2=
set fh=+
if %num1%*==* goto err
if %num2%*==* goto err
if %num1:~0,1%==- set fh1=-&set num1=%num1:~1%
if %num2:~0,1%==- set fh2=-&set num2=%num2:~1%
if %fh1%*==-* (if %fh2%*==* (set fh=-) else set fh0=-) else if %fh2%*==-* set fh=-
set err=0
echo %num1%|find ".">nul&&set num1=0%num1%0
call :qc %num1% num1
echo %num2%|find ".">nul&&set num2=0%num2%0
call :qc %num2% num2
if %err%==1 goto err
for /f "tokens=1* delims=." %%a in ("%num1%") do (set o1=%%a
set t1=%%b)
for /f "tokens=1* delims=." %%a in ("%num2%") do (set o2=%%a
set t2=%%b)
call :qs0 %o1%
set o1=%sw%
call :qs0 %o2%
set o2=%sw%
call :qw0 %t1%
set t1=%sw%
call :qw0 %t2%
set t2=%sw%
if not %t1%==0 (set o3=%o1%.%t1%) else set o3=%o1%
if not %t2%==0 (set o4=%o2%.%t2%) else set o4=%o2%
call :js %o1%
set s1=%sw%
call :js %o2%
set s2=%sw%
If %s1% Geq %s2% (set/a s3=%s1%-%s2%
call :js0 !s3! %o2%
set o2=!sw!
) ELSE (set/a s3=%s2%-%s1%
call :js0 !s3! %o1%
set o1=!sw!)
call :js %t1%
set s1=%sw%
call :js %t2%
set s2=%sw%
If %s1% Geq %s2% (set/a s3=%s1%-%s2%
call :j0 !s3! %t2%
set t2=!sw!
) ELSE (set/a s3=%s2%-%s1%
call :j0 !s3! %t1%
set t1=!sw!
set s1=%s2%)
set sw=%o1%%t1%
set sw1=%o2%%t2%
:bj
set sw2=1%sw:~0,9%
set sw3=1%sw1:~0,9%
if %sw%*==* goto ksjs
if %sw2% gtr %sw3% goto ksjs
if %sw2% lss %sw3% set fh3=-&goto ksjs
set sw=%sw:~9%
set sw1=%sw1:~9%
goto bj
:ksjs
if %fh3%*==-* (set tmp1=%o2%%t2%&set tmp2=%o1%%t1%) else set tmp1=%o1%%t1%&set tmp2=%o2%%t2%
if %fh3%*==-* (if %fh2%*==-* set fh0=-) else (if %fh1%*==-* set fh0=-)
set jg1=0
set jg=
:js2
set/a jg0=1%tmp1:~-1%%fh%%tmp2:~-1%+%jg1%
set/a jg1=0%jg0:~0,-1%-1
set jg=%jg0:~-1%%jg%
set tmp1=%tmp1:~0,-1%
set tmp2=%tmp2:~0,-1%
if %tmp1%*==* (if %jg1%==1 (set jg=1%jg%&goto xs) ELSE goto xs)
goto js2
:xs
set tmp1=!jg:~-%s1%!
set tmp2=!jg:~0,-%s1%!
call :qw0 %tmp1%
set tmp1=%sw%
call :qs0 %tmp2%
set jg=%sw%.%tmp1%
if %jg:~-1%==0 set jg=%jg:~0,-2%
echo 计算结果是:
if %fh2%*==-* set fh=
echo %fh1%%o3%%fh%%fh2%%o4%=%fh0%%jg%
pause
goto :eof
:qs0
set sw=%1
:qs1
if %sw%==0 goto :eof
if %sw:~0,1%==0 set sw=!sw:~1!&goto qs1
goto :eof
:qw0
set sw=%1
:qw1
if %sw%==0 goto :eof
if %sw:~-1%==0 set sw=!sw:~0,-1!&goto qw1
goto :eof
:js
set sw=0
set sw1=%1
if %sw1%==0 set sw=1&goto :eof
:js1
if not %sw1%*==* set sw1=%sw1:~1%&set/a sw+=1&goto js1
goto :eof
:qc
echo %1|find ".">nul||set %2=%1.0
for /f "tokens=1* delims=." %%i in ("%1") do echo %%i%%j|findstr "^*$">nul||set err=1
goto :eof
:j0
set sw=%2
if %1==0 goto :eof
for /l %%c in (1,1,%1) do set sw=!sw!0
goto :eof
:err
echo 你输入了非法字符^!
echo 请重新输入...
goto input
:js0
set sw=%2
if %1==0 goto :eof
for /l %%c in (1,1,%1) do set sw=0!sw!
goto :eof
Last edited by qwe1234567 on 2006-10-13 at 01:11 ]
I also add one!
@echo off
cls
title Real Number Addition and Subtraction Operation Script
color 17
setlocal enabledelayedexpansion
if not %1*==* (set num1=%1&if not %2*==* (set num2=%2&goto jmp) ELSE goto err)
:input
echo REAL NUMBER ADDITION AND SUBTRACTION OPERATION SCRIPT
echo.
echo This script can theoretically perform addition and subtraction operations on N-digit real numbers
echo.
echo Note: Except for Arabic numerals, the first digit allows entering the symbol "-", and at most one ".",
echo Other characters are not allowed. Otherwise, it will be regarded as incorrect input...
echo.
echo Made by qwe1234567 from China DOS Union Forum
set/p num1=Please enter the first decimal:
set/p num2=Please enter the second decimal:
:jmp
set fh1=
set fh2=
set fh=+
if %num1%*==* goto err
if %num2%*==* goto err
if %num1:~0,1%==- set fh1=-&set num1=%num1:~1%
if %num2:~0,1%==- set fh2=-&set num2=%num2:~1%
if %fh1%*==-* (if %fh2%*==* (set fh=-) else set fh0=-) else if %fh2%*==-* set fh=-
set err=0
echo %num1%|find ".">nul&&set num1=0%num1%0
call :qc %num1% num1
echo %num2%|find ".">nul&&set num2=0%num2%0
call :qc %num2% num2
if %err%==1 goto err
for /f "tokens=1* delims=." %%a in ("%num1%") do (set o1=%%a
set t1=%%b)
for /f "tokens=1* delims=." %%a in ("%num2%") do (set o2=%%a
set t2=%%b)
call :qs0 %o1%
set o1=%sw%
call :qs0 %o2%
set o2=%sw%
call :qw0 %t1%
set t1=%sw%
call :qw0 %t2%
set t2=%sw%
if not %t1%==0 (set o3=%o1%.%t1%) else set o3=%o1%
if not %t2%==0 (set o4=%o2%.%t2%) else set o4=%o2%
call :js %o1%
set s1=%sw%
call :js %o2%
set s2=%sw%
If %s1% Geq %s2% (set/a s3=%s1%-%s2%
call :js0 !s3! %o2%
set o2=!sw!
) ELSE (set/a s3=%s2%-%s1%
call :js0 !s3! %o1%
set o1=!sw!)
call :js %t1%
set s1=%sw%
call :js %t2%
set s2=%sw%
If %s1% Geq %s2% (set/a s3=%s1%-%s2%
call :j0 !s3! %t2%
set t2=!sw!
) ELSE (set/a s3=%s2%-%s1%
call :j0 !s3! %t1%
set t1=!sw!
set s1=%s2%)
set sw=%o1%%t1%
set sw1=%o2%%t2%
:bj
set sw2=1%sw:~0,9%
set sw3=1%sw1:~0,9%
if %sw%*==* goto ksjs
if %sw2% gtr %sw3% goto ksjs
if %sw2% lss %sw3% set fh3=-&goto ksjs
set sw=%sw:~9%
set sw1=%sw1:~9%
goto bj
:ksjs
if %fh3%*==-* (set tmp1=%o2%%t2%&set tmp2=%o1%%t1%) else set tmp1=%o1%%t1%&set tmp2=%o2%%t2%
if %fh3%*==-* (if %fh2%*==-* set fh0=-) else (if %fh1%*==-* set fh0=-)
set jg1=0
set jg=
:js2
set/a jg0=1%tmp1:~-1%%fh%%tmp2:~-1%+%jg1%
set/a jg1=0%jg0:~0,-1%-1
set jg=%jg0:~-1%%jg%
set tmp1=%tmp1:~0,-1%
set tmp2=%tmp2:~0,-1%
if %tmp1%*==* (if %jg1%==1 (set jg=1%jg%&goto xs) ELSE goto xs)
goto js2
:xs
set tmp1=!jg:~-%s1%!
set tmp2=!jg:~0,-%s1%!
call :qw0 %tmp1%
set tmp1=%sw%
call :qs0 %tmp2%
set jg=%sw%.%tmp1%
if %jg:~-1%==0 set jg=%jg:~0,-2%
echo The calculation result is:
if %fh2%*==-* set fh=
echo %fh1%%o3%%fh%%fh2%%o4%=%fh0%%jg%
pause
goto :eof
:qs0
set sw=%1
:qs1
if %sw%==0 goto :eof
if %sw:~0,1%==0 set sw=!sw:~1!&goto qs1
goto :eof
:qw0
set sw=%1
:qw1
if %sw%==0 goto :eof
if %sw:~-1%==0 set sw=!sw:~0,-1!&goto qw1
goto :eof
:js
set sw=0
set sw1=%1
if %sw1%==0 set sw=1&goto :eof
:js1
if not %sw1%*==* set sw1=%sw1:~1%&set/a sw+=1&goto js1
goto :eof
:qc
echo %1|find ".">nul||set %2=%1.0
for /f "tokens=1* delims=." %%i in ("%1") do echo %%i%%j|findstr "^*$">nul||set err=1
goto :eof
:j0
set sw=%2
if %1==0 goto :eof
for /l %%c in (1,1,%1) do set sw=!sw!0
goto :eof
:err
echo You entered illegal characters^!
echo Please re-enter...
goto input
:js0
set sw=%2
if %1==0 goto :eof
for /l %%c in (1,1,%1) do set sw=0!sw!
goto :eof
Last edited by qwe1234567 on 2006-10-13 at 01:11 ]
此帖被 +4 点积分 点击查看详情 评分人:【 】 | 分数: +4 | 时间:2006-10-14 10:16 |
|
附件
1: 实数加减运算脚本.rar (2006-10-13 01:10, 1.17 KiB, 下载附件所需积分 1 点
,下载次数: 20)
|

我的网络U盘 我的网络第2个U盘
论坛软件下载链接
灵雨飘零论坛
论坛新手必读,所有人的基本行为准则
刷QQ空间人气、留言的小软件 |
|
2006-10-12 21:27 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
请输入第一个小数:1111111111.1111111111
请输入第二个小数:-1111111111.1111111112
计算结果是:
1111111111.1111111111-1111111111.1111111112=9999999999.9999999999
看了一下, qwe1234567版主的代码写得还是不错. 你在进行各种加减法运算时走了捷径, 走的这个捷径却会出现致命错误. 另外在格式输出上控制得不太严格.
我的代码使用更多的可调用函数来实现各种加减法运算, 完全模拟了真实的人工计算.
Last edited by pengfei on 2006-10-12 at 23:10 ]
Please enter the first decimal: 1111111111.1111111111
Please enter the second decimal: -1111111111.1111111112
The calculation result is:
1111111111.1111111111 - 1111111111.1111111112 = 9999999999.9999999999
Looking at it, the code of moderator qwe1234567 is still pretty good. You took a shortcut when performing various addition and subtraction operations, and this shortcut will have a fatal error. In addition, the control on the format output is not strict enough.
My code uses more callable functions to implement various addition and subtraction operations, completely simulating real manual calculations.
Last edited by pengfei on 2006-10-12 at 23:10 ]
|
|
2006-10-12 23:01 |
|
|