Dos Batch Calculator Help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Chunter
Posts: 4
Joined: 30 Sep 2009 14:33

Dos Batch Calculator Help

#1 Post by Chunter » 30 Sep 2009 14:37

Hi there, I'm a college student and am relatively new to Dos. Recently an instructor asked me to compile a Dos batchfile. This is my assignment:

A DOS batch file named CalcComm is to be coded to calculate the commission for each
sales person based on their daily sales for the week. The batch file accepts a minimum of
2 command line parameters and a maximum of 5 command line parameters
. At the end
of the batch file, the total sales, commission rate and commission dollars earned should
be displayed.


So basically my parameters are days of the week so there's a minimum of 2 days and a maximum of 5 because there's only 5 days in a week.

I could probably get this up and running easily, however, my instructor also specified that you can only do the calculations once. That's the one problem that I just can't get around. See a normal calculation would work like so:

set /a total = %1 + %2 + %3 + %4 + %5

However this is a little more complex because I can either have (I know this isn't the exact calculation needed to get commission, I'm just giving you a general idea)
%1 + %2 + %3 + %4 + %5
Or
%1 + %2 + %3 + %4
Or
%1 + %2 + %3
Or
%1 + %2

This is where I run into the problem say I only wanted to calculate sales for Thursday and Friday, 2 parameters of the possible 5 (so %1 + %2) that's going to leave 3 parameters unaccounted for and results in a missing operand.

I've been getting head aches just trying to contemplate how exactly to get around this problem. Basically what I'm asking is: How do I structure my batchfile to calculate more than one possible combination of parameters.

I'm having a very similar problem with my java class, although I don't think Java's your area of expertise but it uses a very similar logic.

Any help would be much appreciated.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 30 Sep 2009 19:20

Is it "legal" to use a bunch of if statements to determine what parameters exist and then add them . . ? Kinda like this:

Code: Select all

if not "%5"=="" (
   set /a total=%5+%4+%3+%2+%1
   set numdays=5
   ) else (
      if not "%4"=="" (
         set /a total=%4+%3+%2+%1
         set numdays=4
         ) else (
            if not "%3"=="" (
               set /a total=%3+%2+%1
               set numdays=3
               ) else (
                  set /a total=%2+%1
                  set numdays=2
            )
      )
)


You could also append a zero and divide by 10 -- I probably like this one best -- but I think it will fail if a 0 is passed as an argument.

set /a total=(%50+%40+%30+%20+%10)/10

looks like it'll probably work with a 0 passed to it.

Chunter
Posts: 4
Joined: 30 Sep 2009 14:33

#3 Post by Chunter » 01 Oct 2009 11:29

Well that wasn't quite the answer I was looking for but it certainly helped out a lot, I think I figured out how to do it now.

Post Reply