Greatly enhanced Math batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Bluejay
Posts: 8
Joined: 23 Feb 2017 13:33

Greatly enhanced Math batch

#1 Post by Bluejay » 30 Mar 2017 20:14

Greatly enhanced Math batch. Works perfectly.
Note also 2nd called batch (Value verifier used twice)= M-CkV

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. Echo:
No idea if these work. Delete if you wish.

No doubt some real pros out there can tweak improve my structure.
Please, feel free. Many! hours to make. Best I could do.

Code: Select all

@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 lower case made upper, defaults to A if not ASMD or smd]
REM F=Function (ASMD), V1=1st Value, V2=2nd Value, (D only) #=Decimal positions wanted
REM For D, decimal values are rounded to # positions if given


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

:REDO
Color 0C
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:    USER ENTERED VALUES
Echo.
If !%G%==!F Echo:Retained Function= %HF%
If !%G%==!B Echo:Retained Function= %HF%
If !%G%==!F GoTo GetV
If !%G%==!B GoTo GetV2

:GETF
Echo:
Echo:Enter 1st letter of function
Choice /C ASMD
If Errorlevel==1 Set F=A
If Errorlevel==2 Set F=S
If Errorlevel==3 Set F=M
If Errorlevel==4 Set F=D

: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
If Not !%HF%==!A 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
Color 0E
If !%G%==!B GoTo RT1
If !%G%==!V GoTo RT1
If %F%%G%==/K GoTo RTF
GoTo NoRtn
:RT1
Echo:Retained 1st Value= %V1%
If Not %F%%G%==/B GoTo Pause
:RTF
Echo:Retained Fraction Positions= %#%
:PAUSE
Pause

:NORTN
Color 0C
If !%1==! Echo:
:REGETV2
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 ReGetV2

: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:
Color 0F
Echo:# = %#% NOT NUMERIC
Echo.
If Not !%1==! GoTo Done
Pause
Color 0C
GoTo Do#

:GO
Set H#=%#%
Set G=
If Not !%1==! GoTo Calc
REM ***** Allow user to examine entries
ClS
Echo:
Color 02
ClS
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
Color 0C
ClS
Echo:DECIMAL MATH CALCULATOR BATCH
Echo:    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

Setlocal EnableDelayedExpansion
For /L %%I in (1,1,!#!) Do Set "D=!D!0"
Endlocal &Set "D=%D%"

: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%
Set /A R#=%R#%+1
Set R%R#%=%R%
Set Ck=
Set RW=

Echo.
If Not !%1==!  GoTo Clear
Echo:
Echo:Enter G, F, V or B to do Another Calculation or Enter if Done
Echo:F - Function and Decimal Positions retained
Echo:V - V1 set to Current Result
Echo:B - 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
If %R#%==1 GoTo Clear
Color 0D
ClS
Echo:ALL RESULTS
Echo.
Setlocal EnableDelayedExpansion
For /F "tokens=1*delims==" %%A in ('Set R') Do Echo:%%A= %%B
Endlocal
Pause

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


M-CkV.bat
@ECHO Off
REM ***** Verify valid Value for math calculation
If !%Ck%==! Set Ck=Null
If %Ck%==Null GoTo Inv
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 %Ck%==%R% GoTo X
If Not %RW%==0 GoTo Inv
If 0%Ck%==%R% GoTo X
:Inv
Echo.
Echo:
Color 0E
Echo:V = %Ck% - INVALID
Echo.
Pause
Set Ck=X
Color 0C
:X
Last edited by aGerman on 21 Aug 2017 12:30, edited 1 time in total.
Reason: code tags added

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Greatly enhanced Math batch

#2 Post by ShadowThief » 30 Mar 2017 22:07

"Hey guys, I found out that you can do non-integer math if you use PowerShell instead!"

...yeah?

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Greatly enhanced Math batch

#3 Post by Sounak@9434 » 31 Mar 2017 04:36

Are [code]"Code tags"[/code] this uncommon now days?

Bluejay
Posts: 8
Joined: 23 Feb 2017 13:33

Re: Greatly enhanced Math batch

#4 Post by Bluejay » 21 Aug 2017 08:17

Have been hoping someone would post reply to Sounak@9434
>30 years using DOS. Never saw (or don't recognize)

Code: Select all

"Code tags"

Never seen in many searches for additional command syntax and details.
Additional clarification would be appreciated. Just curious.
Assume posted here as possible batch benefit.
DOS search term might be helpful.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Greatly enhanced Math batch

#5 Post by aGerman » 21 Aug 2017 12:42

I had a hard time understanding your reply. Writing entire sentences would have been helpful. I assume you didn't understand what code tags are. It's what Sounak@9434 already wrote and what you should have recognized after quoting it. It's the way how to post formatted code in a forum in order to avoid BBCode in your source code and to preserve code indentations (if any). There is a button "Code" in the editor box that will write code tags to the text-cursor position. I added them to your initial post.

Steffen

Bluejay
Posts: 8
Joined: 23 Feb 2017 13:33

Re: Greatly enhanced Math batch

#6 Post by Bluejay » 24 Aug 2017 07:28

Thank you Steffen.
Correct- didn't understand what code tags are.
No clue from what Sounak@9434 wrote.
Very rarely use forums.
1st time, I think, ever posted a full DOS batch.
Just prepared in text editor, copy/paste and submit.
Didn't look for any whistles and bells.
Wouldn't have recognized Code in any case.

Thanks for adding to my batch.
Incidentally, also now see check box to Disable BBCode.
Will remember both for possible future use.

Post Reply