help cbm calculator

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
xhai
Posts: 39
Joined: 13 Jun 2013 09:33

help cbm calculator

#1 Post by xhai » 25 Jun 2015 06:39

hi guys, I have a little problem. how do I create a simple script like a calculator. The formula was L x W x H in cm / 1000000 = value.cbm

to do
-two decimal place
- ability to add 2 or more entries and add the total cbm.
-convert meter to centimeter to compute the cbm

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

Re: help cbm calculator

#2 Post by aGerman » 25 Jun 2015 13:02

a simple script

There is no simple way. The command processor can only recognize integral numbers from -2147483648 to 2147483647. Also the result of any calculation will be an integer. All decimals are automatically eliminated. There are several work-arounds using string manipulations that are all more or less complicated. A hybrid script could be a little easier:
.bat

Code: Select all

@if (@a)==(@b) @end /*

@echo off &setlocal
set "formula=2/3"
for /f %%i in ('cscript //nologo //e:jscript "%~fs0" "%formula%"') do set "result=%%i"
echo %result%
pause

exit /b &rem */ WScript.Echo(parseFloat(eval(WScript.Arguments.Item(0))).toFixed(2));

That code uses JScript. As you can see the expression in variable formula will be evaluated, rounded and assigned to variable result.

Regards
aGerman

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: help cbm calculator

#3 Post by Aacini » 25 Jun 2015 15:48

Although the arithmetic operations of SET /A command can only manage integers, it is relatively simple to achieve operations with a *fixed number* of decimals using the method described at this post. However, you must check that the range of values allowed by the 32-bits integers, less the part used for the decimals, is enough for your needs. Please, post a small table as example of input and output values and indicate which ones would be the maximum possible input values.

Also, it is important that you define if the L, W and H input values have also 2 decimals or just the calculated result.

Antonio

xhai
Posts: 39
Joined: 13 Jun 2013 09:33

Re: help cbm calculator

#4 Post by xhai » 27 Jun 2015 09:44

here's some sample input and output

ex. H =1280 W =223 L =134
l x w x h / 1000000 = 38.24 cbm

ex l = 90 x w = 110 h = 36
l x w x h / 1000000 = 0.85
H =1280 W =223 L =134
l x w x h / 1000000 = 38.24 cbm

0.85 + 38.24 = 39.09

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: help cbm calculator

#5 Post by Aacini » 27 Jun 2015 13:36

Code: Select all

@echo off
setlocal

set /A i=0, total=0
echo Enter L, W and H values separated by spaces (nothing to end):
:nextEntry
   set /A i+=1
   set "entry="
   set /P "entry=%i%- "
   if not defined entry goto endData
   for /F "tokens=1-3" %%a in ("%entry%") do set /A cbm=%%a*%%b*%%c/10000, total+=cbm
   echo    = %cbm:~0,-2%.%cbm:~-2% cbm
goto nextEntry
:endData
if %i% gtr 2 echo Total = %total:~0,-2%.%total:~-2%

Output example:

Code: Select all

C:\> test.bat
Enter L, W and H values separated by spaces (nothing to end):
1- 134 223 1280
   = 38.24 cbm
2-

C:\> test.bat
Enter L, W and H values separated by spaces (nothing to end):
1- 90 110 36
   = .35 cbm
2- 134 223 1280
   = 38.24 cbm
3-
Total = 38.59

You have an error in your example data; the cbm of 90, 110, 36 is 0.35, not 0.85.

Antonio

xhai
Posts: 39
Joined: 13 Jun 2013 09:33

Re: help cbm calculator

#6 Post by xhai » 27 Jun 2015 22:26

thank you very much Antonio ,
it's a very helpful tool in my work

xhai
Posts: 39
Joined: 13 Jun 2013 09:33

Re: help cbm calculator

#7 Post by xhai » 28 Jun 2015 06:39

small problem.. it accept strings how to i change it so it can only accept integers only..

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: help cbm calculator

#8 Post by foxidrive » 28 Jun 2015 07:49

You use a for /f loop to exclude numbers and then branch to your error routine, on the result.

I believe it was aGerman that posted the routine once more in the last week.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: help cbm calculator

#9 Post by Aacini » 28 Jun 2015 12:19

xhai wrote:small problem.. it accept strings how to i change it so it can only accept integers only..


This is funny: your new "small problem" is much more complex than the 2 decimals original one! If you want that the "entry" variable contain just numbers, so the arithmetic operation don't mark any errors, then there are two possible ways to achieve that.

The first one is to read the entry with SET /P and then review the entry, mark an error if is wrong and repeat the process until the entry is correct. You may read possible methods to check if the entry contain just digits at Check variable is number post. However, in this case your requirement is more complex, because the program needs three numbers separated by spaces! Also, you must specify other points; for example, if the entry contains more than three numbers, do you want to ignore the rest or mark an error? Good luck with that... Of course, you could read just one value per line and check it, so you need to define a subroutine to check the number and call it three times.

Another possible solution is to restrict the entry so it can only accept numbers; more specifically, to just accept precisely three integers of a maximum of 4 digits each separated by a space. This method is more pleasant for the user and provide a correct entry always. You may use my ReadFormattedLine subroutine to achieve this in a very simple way; just change this line:

Code: Select all

   set /P "entry=%i%- "

... by this one:

Code: Select all

   call :ReadFormattedLine entry="##### ##### #####" /M "%i%- " /F

Of course, you must also include the code of the subroutine in your Batch file, that is "just" 145 lines long! :mrgreen:

Finally, I state the solution that, in my opinion, is the best suited for your needs. After this line:

Code: Select all

echo Enter L, W and H values separated by spaces (nothing to end):

... insert this one:

Code: Select all

echo If you enter any wrong value, the program will fail^!
:D

Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: help cbm calculator

#10 Post by foxidrive » 28 Jun 2015 22:28

That is how to solve errors in batch scripts! :D

Aacini wrote:

Code: Select all

echo If you enter any wrong value, the program will fail^!
:D

Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: help cbm calculator

#11 Post by foxidrive » 28 Jun 2015 22:29

That is how to solve errors in batch scripts! :D

Aacini wrote:

Code: Select all

echo If you enter any wrong value, the program will fail^!
:D

Antonio



There are so many things that can break a batch file, if the input data is not as it is expected to be.

Post Reply