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~~~!
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...
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. 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 ]
Attachments
