Solved DOS Decimal Batch all except divide

February 23, 2017 at 11:59:26
Specs: Windows 7
Here is a DOS batch that can do decimal calculations. Please share everywhere.
Will do Add, Subtract & Multiply. Divide very difficult. Not included (for now).
NOTE: Can change to do variable sets from command line.
Must then change code to handle this and remove redo lines.

DOS CALCULATOR BATCH
@ECHO Off
:REDO
ClS
Set G=

Echo Enter S to Subtract - M to Multiply - Other to ADD
Set /P E=
If !%E%==!s Set E=S
If !%E%==!S GoTo ASM
If !%E%==!m Set E=M
If !%E%==!M GoTo ASM
Set E=A
:ASM
Echo Enter # of Value fraction positions
Set /P #=
Echo.
Echo ALL VALUES MUST HAVE SAME # OF FRACTION POSITIONS
Echo PAD WITH ZEROS IF NECESSARY
Echo ENTER VALUES WITH NO DECIMAL POINT
Echo.
:DO1
Echo Enter Entire 1st value
Set /P V1=
Set /A W=%V1%*1
If %V1%==%W% GoTo Do2
Set W=%V1%
Set R=Do1
GoTo Not#
:DO2
Echo Enter Entire 2nd value
Set /P V2=
Set /A W=%V2%*1
If %V2%==%W% GoTo Go
Set W=%V2%
Set R=Do2

:NOT#
ClS
Echo Entry %W% Not Numeric - Redo
Pause
ClS
GoTo %R%

:GO
ClS
Echo Function= %E%
Echo Positions= %#%
Echo 1st Value= %V1%
Echo 2nd Value= %V2%
Echo.
Echo Press Enter if all OK, Other to redo
Set /P G=
If Not !%G%==! GoTo Redo

ClS
Set D=1
Set RD=
Set P=.
If %#%==0 GoTo %E%
:SETD
Set D=%D%0
Set /A #=%#%-1
If Not %#%==0 GoTo SetD
:GOTD
If !%G%==!M GoTo M
Set /A V1D=%V1%-(%V1%/%D%*%D%)
Set /A V2D=%V2%-(%V2%/%D%*%D%)
GoTo %E%

:A
Set /A R=(%V1%+%V2%)/%D%
If %D%==1 GoTo END
Set /A RD=%V1D%+%V2D%
Set /A W=(%RD%/%D%)*%D%
Set /A RD=%RD%-%W%
GoTo END

:S
Set /A R=%V1%-%V2%
Set /A R=%R%/%D%
If %D%==1 GoTo END
Set /A RD=%V1D%-%V2D%+%D%
Set /A W=%RD%/%D%
If %W%==0 Set /A RD=(%D%*2)-%RD%
Set /A RD=%RD%-%D%
GoTo END

:M
Set /A D *=%D%
Set /A W=%V1%*%V2%
Set /A R=%W%/%D%
Set /A RD=%W%-(%R%*%D%)+%D%
Set /A W=%RD%/%D%
Set /A RD=%RD%-%D%
If Not %W%==1 GoTo END
If %D%==1 GoTo End
Set /A D/=10
:ADD0
Set /A W=%RD%/%D%
If Not %W%==0 GoTo End
Set /A D=/10
Set P=%P%0
If Not %D%==1 GoTo Add0

:END
If !%RD%==!0 Set RD=
If !%RD%==! Set P=
If !%P%==! GoTo Show
Set /A W=%RD%/10*10
Set /A W=%RD%-%W%
If Not %W%==0 GoTo Show
Set /A RD/=10
GoTo End

:SHOW
Echo RESULT= %R%%P%%RD%
Set E=
Set #=
Set V1=
Set V1D=
Set V2=
Set V2D=
Set R=
Set RD=
Set W=
Set D=
Echo.
Echo Enter G to do another calculation
Set /P G=
If !%G%==!g GoTo Redo
If !%G%==!G GoTo Redo
Set G=


See More: DOS Decimal Batch all except divide

Report •

#1
February 23, 2017 at 14:54:49
just call powershell from within batch

powershell 15/4

answer:3,75

Simple solutions are often the best


Report •

#2
February 23, 2017 at 19:47:02
✔ Best Answer
I agree with Hide: use a better interface. Batch sucks for anything but integer math, and even then it can trip you up. (try adding "08+1"). VBscript being native, I use that for all my batch and commandline calcs:
'Simple commandline calculator, but handy to have around (batch math is feeble)
'(a "front-end" to vbscript "eval").
'To use (example): cscript /nologo calcit.vbs ((172+6)*3) * 2^8/34 mod 4
'or another, demonstrating fractions: 12.7 / .22
'
'Also note it will do hex-to-decimal conversion and hex math, like: calcit &h70 + &hC, or &h70 + 12
'(but the results always rendered in decimal, alas.)
'Will also concatenate numbers using "&".
'========== begin VBscript 'CALCIT.VBS
set con=wscript.stdin
set cout=wscript.stdout
set t=wscript.arguments
if t.count=0 then
aa=0
bb=0
'__interactive" mode:
cout.write ("enter the math: ")
do while len(aa)>0
aa=con.readline
bb=eval(bb & aa)
cout.writeline("cur.ttl: -------- "&cstr(bb))
loop
else
dim a(99)
for i=0 to t.count-1
aa=aa&" "&t(i)
next
cout.writeline(eval(aa))
end if
'======== end VBscript 'CALCIT.VBS'

message edited by nbrane


Report •

#3
March 16, 2017 at 10:21:07
Oh, wow. Knew nothing about powershell.
Spent days making original batch more user friendly.
Built in both user entry and command line parameter options.
Also reworked large number calculations to process whole and
fraction portions separately and combine with correct decimal point.
Now know huge waste of time for latter.

OK, batch now greatly simplified.
Uses powershell. Couldn't figure out VBscript.
Tried various combinations. Nothing worked.

Anyhow, here is new batch. Works perfectly.
Note also 2nd called batch: M-CkV
= Value verifier used twice.

Career as main-frame senior analyst/programmer.
Methinks great interface.
Well commented for those who are interested.

Batch has some extra logic to handle optional
user choice decimal positions for division (only).
Includes user entry and command line parameter options.
For USER ENTRY METHOD subsequent runs,
allows optional retention of current function
and user entered division decimal positions.
Also can optionally set 1st value to current result.

Note: Included beeps at key points. Hope these work.
Recent move. Can't find my speakers or earphones.
Also found info about DOS batch "Beep" command. May also work.
Both methods accepted without error response in test runs.

@echo Off
REM DECIMAL MATH CALCULATOR BATCH
REM USING RUN TIME ENTERED VALUES OR
REM COMMAND PARAMETER ENTERED VALUES
REM COMMAND LINE THEN BECOMES- Math F V1 V2 #
REM F=Function (ASMD), V1=1st Value, V2=2nd Value, #=Decimal positions wanted (D only)
REM For D, decimal values are rounded to # positions if given


Set G=
Set /P=Set R=>M-SetR.txt<nul

:REDO
ClS
echo:DECIMAL MATH CALCULATOR BATCH
If !%1==! GoTo Start

echo:COMMAND PARAMETER ENTERED VALUES
Echo.
Set F=%1
Set V1=%2
Set V2=%3
Set #=%4
Set H#=%#%
GoTo CkF

:Start
echo:USING USER ENTERED VALUES
Echo.
If !%G%==!F GoTo GetV
If !%G%==!B GoTo GetV2

:GETF
echo:
Set /P F= Enter 1st letter of function= S M D A, Other=A:
:CKF
REM ***** Set Funtion
Set HF=%F%
If !%F%==!s Set HF=S
If !%HF%==!S GoTo GotF
If !%F%==!m Set HF=M
If !%HF%==!M GoTo GotF
If !%F%==!d Set HF=D
If !%HF%==!D GoTo GotF
Set HF=A
:GOTF
If %HF%==A Set F=+
If %HF%==S Set F=-
If %HF%==M Set F=*
If %HF%==D Set F=/
If !%G%==!V GoTo GetV2

:GETV
If !%1==! echo:
If !%1==! Set /P V1= Enter 1st value:
REM ***** Verify valid V1
Set Ck=%V1%
Call M-CkV
If Not %Ck%==X GoTo GetV2
If Not !%1==! GoTo Done
GoTo GetV

:GETV2
If !%1==! echo:
If !%1==! Set /P V2= Enter 2nd value:
REM ***** Verify valid V2
Set Ck=%V2%
Call M-CkV
If Not %Ck%==X GoTo Do#
If Not !%1==! GoTo Done
GoTo GetV2

:DO#
If Not %F%==/ GoTo Go
If !%G%==!K GoTo Go
If !%G%==!B GoTo Go
If !%1==! echo:
If !%1==! Set /P #= Enter # of Fraction positions if desired:
REM ***** Verify valid #
If !%#%==!0 Set #=
If !%#%==! GoTo Go
Set /A Ck=#*1
If %#%==%Ck% GoTo Go
Echo.
echo:
echo:# = %#% NOT NUMERIC
Echo.
If Not !%1==! GoTo Done
Pause
GoTo Do#

:GO
Set H#=%#%
Set G=
If Not !%1==! GoTo Calc
REM ***** Allow user to examine entries
echo:
echo:Function= %HF%
echo:1st Value= %V1%
echo:2nd Value= %V2%
If %F%==/ echo:Positions= %#%
Set /P G= Press Enter if all OK, Other to redo
If Not !%G%==! GoTo Redo
ClS
echo:DECIMAL MATH CALCULATOR BATCH
echo:USING USER ENTERED VALUES
Echo.

:CALC
REM ***** Do Calculation
Powershell %V1%%F%%V2%>Result.txt
Copy M-SetR.txt+Result.txt Result.bat>nul
Call Result.bat
If Not %F%==/ GoTo Show
If !%#%==! GoTo Show
Set /a RW=R*1
If Not !%D%==! GoTo DoD
Set D=1

:SETD
Set D=%D%0
Set /A #=#-1
If Not %#%==0 GoTo SetD

:DOD
REM ***** Get Whole
Powershell ((%R%-%RW%)*%D%0)+5>Result.txt
Copy M-SetR.txt+Result.txt Result.bat>nul
Call Result.bat
Set /A R=(R+1)/10
REM ***** Get Fraction & build Result
Powershell %RW%+(%R%/%D%)>Result.txt
Copy M-SetR.txt+Result.txt Result.bat>nul
Call Result.bat

:SHOW
echo:
echo:RESULT= %R%
Del Result.*>nul
Set Ck=
Set RW=

Echo.
If Not !%1==! GoTo Done
echo:
echo:Enter G, F, V or B to do another calculation
echo:F retains same Function and Decimal Positions
echo:V sets V1 to Current Result
echo:B does Both as F and V
Set /P G=
If !%G%==! GoTo Done
Set #=%H#%
If !%G%==!f Set G=F
If !%G%==!F GoTo Redo
Set V1=%R%
If !%G%==!b Set G=B
If !%G%==!B GoTo Redo
Set D=
If !%G%==!g Set G=G
If !%G%==!G GoTo Redo
If !%G%==!v Set G=V
If !%G%==!V GoTo Redo

:Done
Del M-SetR.txt>nul
Set G=
Set F=
Set #=
Set V1=
Set V2=
Set HF=
Set H#=
Set D=
Set R=

M-CkV.bat
@ECHO Off
REM Verify valid Value for math calculation
Set /a RW=Ck+0
Powershell (%Ck%-%RW%)+%RW%>Result.txt
If %errorlevel%==1 GoTo Inv
Copy M-SetR.txt+Result.txt Result.bat >nul
Call Result.bat
If %RW%==0 Set Ck=0%Ck%
If %Ck%==%R% GoTo X
:Inv
Echo.
echo:
Echo:V = %Ck% INVALID
Echo.
Pause
Set Ck=X
:X


Report •
Related Solutions


Ask Question