/******************************************* ** File: proj1.c ** Author: D. Frey ** Date: 1/30/00 ** Section: 101 ** SSN: 123-45-6789 ** EMail: frey@cs.umbc.edu ** ** This program calculates connection fees ** for user's of the FB Internet Service. ** Start and end times are input as 24-hour ** military time. It accepts start/end times ** until 0 is entered as both start time and end time. ** ** The connection fees are based on the time of day. ** Each 6-hour period (0000 - 0600, 0600 - 1200, 1200 - 1800 ** and 1800 - 0000) is a different rate. When complete, the ** program displays the number of minutes connected during ** each quarter of the day and the total user connection fee ** ** Algorithm: ** while the start time and end time not both zero ** change start/end time to "minutes since midnight" ** for each minute between start and end ** update appropriate quarters connection minutes ** print results ** ** Notes: ** in this algorithm, the for-loop index indicates the ** time at which the minute begins. So, for example, when 'minute' is ** 359 (1 minute before 6AM) that's a 2-cent minute and ** when 'minute' is 360 (6AM) that's a 3-cent minute) ** ** Assumptions ** 1. the user connect time does not cross midnight ** (i.e startTime is always less than stopTime ** 2. There will be no invalid input ** ******************************************************/ #include /* constants -- ** the day is divided into 4 parts -- I call them "time-zones" ** so my #defines and variables start with TZ or tz ********************************************************** #define TZ1START 0 /* first quarter of day starts at 0000 */ #define TZ1RATE 0.02 /* 2 cents per minute midnight to 6AM */ #define TZ2START (6 * 60) /* 6:00 AM as minutes since midnight */ #define TZ2RATE 0.03 /* 3 cents per minute 6:01 - noon */ #define TZ3START (12 * 60) /* 12:00 noon as mins since midnight */ #define TZ3RATE 0.05 /* 5 cents per minute 12:01 to 6PM */ #define TZ4START (18 * 60) /* 6PM as minutes since midnight */ #define TZ4RATE 0.06 /* 6 cents per minute 6PM to midnight */ main ( ) { double totalFee; /* total fee for all connect time */ int tz1Minutes = 0; /* min connected in 1st time-zone */ int tz2Minutes = 0; /* min connected in 2nd time-zone */ int tz3Minutes = 0; /* min connected in 3rd time-zone */ int tz4Minutes = 0; /* min connected in 4th time-zone */ int startTime, endTime; /* military hours & minutes */ int startMin, endMin; /* as "minutes since midnight" */ int minute; /* for-loop index (start of minu */ /* print greeting and directions to the user */ printf ("\n"); printf ("Welcome to the B & F ISP Connection Fee Calculator\n" "Please enter connection start time and end time\n" "on the same line in 24-hour military time\n" "Enter BOTH start and stop time as 0000 to quit\n\n"); /* get the first start/end time ** this is the "priming read" */ printf ("Please enter start time & stop time: "); scanf ("%d%d", &startTime, &endTime); /* process start/end times until 0 is entered for both */ while (startTime != 0 || endTime != 0) { /* change military times into "minutes since midnight" ** assumes valid input *****************************************************/ startMin = (startTime / 100) * 60 + (startTime % 100); endMin = (endTime / 100) * 60 + (endTime % 100); /* minute is the time-of-day the minute starts ** update number of minutes for appropriate time-zone ***************************************************/ for (minute = startMin; minute < endMin; minute++) { /* does minute start in first time-zone? */ if (TZ1START <= minute && minute < TZ2START) { tz1Minutes++; } /* does minute start in 2nd time-zone? */ else if (TZ2START <= minute && minute < TZ3START) { tz2Minutes++; } /* does minute start in 3rd time-zone? */ else if (TZ3START <= minute && minute < TZ4START) { tz3Minutes++; } /* must be 4th time-zone */ else { tz4Minutes++; } } /* get the next user start/end times */ printf ("Please enter start time & stop time: "); scanf ("%d%d", &startTime, &endTime); } /* print the times and fees for each 6-hour time-zone ** print the total user fee *****************************************************/ printf ("\nYour connection time and fees:\n"); printf ("%3d minutes at %0.2f cost $%8.2f\n", tz1Minutes, TZ1RATE, tz1Minutes * TZ1RATE); printf ("%3d minutes at %0.2f cost $%8.2f\n", tz2Minutes, TZ2RATE, tz2Minutes * TZ2RATE); printf ("%3d minutes at %0.2f cost $%8.2f\n", tz3Minutes, TZ3RATE, tz3Minutes * TZ3RATE); printf ("%3d minutes at %0.2f cost $%8.2f\n", tz4Minutes, TZ4RATE, tz4Minutes * TZ4RATE); totalFee = (TZ1RATE * tz1Minutes) + (TZ2RATE * tz2Minutes) + (TZ3RATE * tz3Minutes) + (TZ4RATE * tz4Minutes); printf ("------------------------------------\n"); printf ("\t\tTotal Cost: $%0.2f\n\n", totalFee); }