China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-23 07:30
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Original] Real number addition and subtraction operation script! View 6,227 Replies 23
Original Poster Posted 2006-10-06 23:42 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
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 ]
Attachments
浮点数加减运算脚本.rar (1.49 KiB, Credits to download 1 pts, Downloads: 49)
无限制实数加减运算脚本.rar (2.21 KiB, Credits to download 1 pts, Downloads: 49)
Floor 2 Posted 2006-10-07 01:51 ·  中国 湖北 武汉 电信
初级用户
Credits 118
Posts 34
Joined 2006-01-10 09:01
20-year member
UID 48727
Status Offline
Floor 3 Posted 2006-10-07 03:40 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 4 Posted 2006-10-07 04:25 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
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 ]
Floor 5 Posted 2006-10-07 06:25 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Found a strange phenomenon...


=============
Operation result!
=============

2222222222.222222222-1111111111.111111111=1111111111.111111111

--------------------------------------------------------------------

=============
Operation result!
=============

2222222222.222222222+1111111111.111111111=-961633962.1961633963
Floor 6 Posted 2006-10-07 06:37 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
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
Floor 7 Posted 2006-10-07 07:04 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
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 ]
Floor 8 Posted 2006-10-07 08:11 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 9 Posted 2006-10-07 08:18 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
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.
Floor 10 Posted 2006-10-08 05:25 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
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'>"
Floor 11 Posted 2006-10-08 05:57 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
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
Floor 12 Posted 2006-10-12 08:17 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
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 ]
Floor 13 Posted 2006-10-12 08:27 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
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...
Floor 14 Posted 2006-10-12 21:27 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
+4 2006-10-14 10:16
Attachments
实数加减运算脚本.rar (1.17 KiB, Credits to download 1 pts, Downloads: 20)
Floor 15 Posted 2006-10-12 23:01 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
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 ]
Forum Jump: