Computer Programming Contest Preparation

ToolBox - Source for: 4/477/a.c



/home/toolbox/public_html/solutions/4/477/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 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 2015-03-25
   18  * Purpose:
   19  * Problem: 477
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_FIGURES 11
   27 #define RECTANGLE 1
   28 #define CIRCLE 2
   29 
   30 /*
   31  *  Author: Isaac Traxler
   32  *    Date: 20120730
   33  * Purpose: Fun
   34  * Problem: Of Circles and Squares (10823)
   35  */
   36 
   37 struct pointStruct
   38 {
   39     /* STRUCT pointStruct */
   40     double x;
   41     double y;
   42 }; /* STRUCT pointStruct */
   43 
   44 struct figureStruct
   45 {
   46     /* STRUCT figureStruct */
   47     int knd;  /* 0 undefined, 1 square, 2 circle */
   48     struct pointStruct p[2];
   49     /* rectangle             circle
   50        [0] is upper left     [0] is center of circle
   51        [1] is lower right    [1].x is radius */
   52 }; /* STRUCT figureStruct */
   53 
   54 struct figureStruct figures[MAX_FIGURES];
   55 int figureCnt = 0;
   56 double x;
   57 double y;
   58 int pointCnt = 0;
   59 
   60 void init()
   61 {
   62     /* FUNCTION init */
   63     char ch;
   64 
   65     scanf("%c ", &ch);
   66     while ('*' != ch)
   67         {
   68             /* while */
   69             if ('r' == ch)
   70                 {
   71                     /* rectangle */
   72                     figures[figureCnt].knd = RECTANGLE;
   73                     scanf(" %lf %lf %lf %lf ", &figures[figureCnt].p[0].x, &figures[figureCnt].p[0].y, &figures[figureCnt].p[1].x, &figures[figureCnt].p[1].y);
   74                 } /* rectangle */
   75             else
   76                 {
   77                     /* circle */
   78                     figures[figureCnt].knd = CIRCLE;
   79                     scanf(" %lf %lf %lf ", &figures[figureCnt].p[0].x, &figures[figureCnt].p[0].y, &figures[figureCnt].p[1].x);
   80                 } /* circle */
   81             figureCnt++;
   82             scanf("%c ", &ch);
   83         } /* while */
   84 } /* FUNCTION init */
   85 
   86 void dump()
   87 {
   88     /* FUNCTION dump */
   89 } /* FUNCTION dump */
   90 
   91 int getInput()
   92 {
   93     /* FUNCTION getInput */
   94     int dataReadFlag;
   95 
   96     scanf(" %lf %lf ", &x, &y);
   97     DEBUG printf(" %lf %lf\n", x, y);
   98     dataReadFlag = (x != 9999.9) || (y != 9999.9);
   99     pointCnt++;
  100     return (dataReadFlag);
  101 } /* FUNCTION getInput */
  102 
  103 int check(double x, double y, struct figureStruct f)
  104 {
  105     /* FUNCTION check */
  106     int toReturn;
  107     double t1;
  108     double t2;
  109     double tmp;
  110 
  111     if (RECTANGLE == f.knd)
  112         {
  113             /* rectangle */
  114             toReturn = (x>f.p[0].x) && (x<f.p[1].x) && (y<f.p[0].y) && (y>f.p[1].y);
  115         } /* rectangle */
  116     else
  117         {
  118             /* circle */
  119             t1 = x - f.p[0].x;
  120             t2 = y - f.p[0].y;
  121             tmp = sqrt((t1 * t1) + (t2 * t2));
  122             toReturn = (tmp < f.p[1].x);
  123         } /* circle */
  124     return toReturn;
  125 } /* FUNCTION check */
  126 
  127 void process()
  128 {
  129     /* FUNCTION process */
  130     int i;
  131     int found = FALSE;
  132 
  133     for (i=0; i<figureCnt; i++)
  134         {
  135             /* for */
  136             if (check(x, y, figures[i]))
  137                 {
  138                     /* contained */
  139                     printf("Point %d is contained in figure %d\n", pointCnt, i+1);
  140                     found = TRUE;
  141                 } /* contained */
  142         } /* for */
  143     if (! found)
  144         {
  145             /* not found */
  146             printf("Point %d is not contained in any figure\n", pointCnt);
  147         } /* not found */
  148 } /* FUNCTION process */
  149 
  150 int main()
  151 {
  152     /* main */
  153     int moreToDo;
  154 
  155     init();
  156     moreToDo = getInput();
  157     while (moreToDo)
  158         {
  159             /* while */
  160             process();
  161             moreToDo = getInput();
  162         } /* while */
  163 
  164     return EXIT_SUCCESS;
  165 } /* main */
  166