Computer Programming Contest Preparation

ToolBox - Source for: 5/579/a.c



/home/toolbox/public_html/solutions/5/579/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 #define MAX_LENGTH 20
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2014-11-17
   20  * Purpose: fun
   21  * Problem: 579 - Clockhands
   22  */
   23 
   24 /*
   25  * This template reads data until a terminating value is reached.
   26  */
   27 
   28 char line[MAX_LENGTH];
   29 int hour;
   30 int minute;
   31 
   32 void init()
   33 {
   34     /* FUNCTION init */
   35 } /* FUNCTION init */
   36 
   37 void dump()
   38 {
   39     /* FUNCTION dump */
   40 } /* FUNCTION dump */
   41 
   42 int getInput()
   43 {
   44     /* FUNCTION getInput */
   45     int dataReadFlag;
   46     int i;
   47 
   48     scanf(" %s ", line);
   49     if ( ':' == line[2])
   50         {
   51             /* 2 digit hour */
   52             hour = 10 * (line[0] - '0') + (line[1] - '0');
   53             i = 3;
   54         } /* 2 digit hour */
   55     else
   56         {
   57             /* 1 digit hour */
   58             hour = line[0] - '0';
   59             i = 2;
   60         } /* 1 digit hour */
   61 
   62     minute = 10 * (line[i] - '0') + (line[i+1] - '0');
   63     dataReadFlag = (0 != hour) || (0 != minute);
   64     return (dataReadFlag);
   65 } /* FUNCTION getInput */
   66 
   67 void process()
   68 {
   69     /* FUNCTION process */
   70     double angle;
   71 
   72     /* adjust hour to degrees */
   73     hour = hour * 30;
   74     if (360 == hour)
   75         {
   76             hour = 0;
   77         }
   78     /* adjust minute to degrees */
   79     angle = hour + (minute / 2.0); /* every minute moves the hour hand half a degree */
   80     minute = minute * 6;
   81 
   82     if (angle < minute)
   83         angle = minute - angle;
   84     else
   85         angle = angle - minute;
   86     if (180 < angle)
   87         {
   88             angle = 360 - angle;
   89         }
   90     printf("%.3lf\n", angle);
   91 } /* FUNCTION process */
   92 
   93 int main()
   94 {
   95     /* main */
   96     int moreToDo;
   97 
   98     init();
   99     moreToDo = getInput();
  100     while (moreToDo)
  101         {
  102             /* while */
  103             process();
  104             moreToDo = getInput();
  105         } /* while */
  106 
  107     return EXIT_SUCCESS;
  108 } /* main */
  109