/* Henry M. Walker
   Box  Science Office
   Babysitter problem
*/

#include <stdio.h>

int main (void) 
{  /* pre-conditions:
          starting and ending times will be integers:
             hours must be between 0 and 12 inclusive
             minutes must be between 0 and 59 inclusive
      post-conditions:
          the printed value fee computes the babysitter's fee,
             based on this table:
                time between 6:00 pm and 9:00 pm  computed at 1.50/hour
                time between 9:00 pm and midnight computed at 1.00/hour
                time between midnight and 6:00 am computed at 1.25/hour
   */

   int starthr, startmin, endhr, endmin;
   double starttime, endtime;
   double fee = 0.0;
   const double early = 1.50;
   const double mid   = 1.0;
   const double late  = 1.25;

   printf ("babysitter problem\n");

   /* read starting time, and check time is within range */
   printf ("start time: ");
   scanf ("%d %d", &starthr, &startmin);
   if ((starthr < 0) || (starthr > 12) || (startmin < 0) || (startmin > 59)) {
     printf ("invalid start time\n");
     return (1);
   }

   /* read ending time, and check time is within range */
   printf ("end time: ");
   scanf ("%d %d", &endhr, &endmin);
   if ((endhr < 0) || (endhr > 12) || (endmin < 0) || (endmin > 59)) {
     printf ("invalid end time\n");
     return (1);
   }

   /* combine hours and minutes into a single decimal time;
      avoid ambiguity in hour after midnight by forcing times
      to be between 0.0 and 1.0 */
   starttime = starthr + ((double) startmin / 60.0);
   if (starthr >= 12.0) starttime -= 12.0;
   endtime = endhr + ((double) endmin / 60.0);
   if (endtime >= 12.0) endtime -= 12.0;
   
   /* cases: */
   if ((starttime >= 6.0) && (starttime < 9.0))
     {  
       if ((endtime > 6.0) && (endtime <= 9.0))
         fee = early * (endtime - starttime);
       else if ((endtime > 9.0) && (endtime <= 12.0))
         fee = early * (9.0 - starttime) + mid * (endtime - 9.0);
       else 
         fee = early * (9.0 - starttime) + mid * 3.0 + late * (endtime - 0.0);
     }

   else if ((starttime >= 9.0) && (starttime < 12.0))
     {
       if ((endtime > 9.0) && (endtime <= 12.0))
         fee = mid * (endtime - starttime);
       else 
         fee = mid * (12.0 - starttime) + late * (endtime - 0.0);
     }

   else 
     {
       fee = late * (endtime - starttime);
     }

   printf ("babysitter's fee: %8.2lf\n", fee);

   return 0;
}
