Computer Programming Contest Preparation

ToolBox - Source for: 1/122/b.c



/home/toolbox/public_html/solutions/1/122/b.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 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 #define MAX_LINE 280
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date:
   21  * Purpose: fun
   22  * Problem:
   23  */
   24 
   25 /*
   26  * This template reads lines of data at a time until end of file.
   27  */
   28 
   29 #define MAX_LEAVES 275
   30 
   31 typedef struct LEAF_STRUCT
   32 {
   33     /* struct LEAF_STRUCT */
   34     int vl;
   35     char pth[MAX_LINE];
   36 } /* struct LEAF_STRUCT */ LEAF_STRUCT;
   37 
   38 int endOfTree;
   39 int leafCnt;
   40 LEAF_STRUCT tree[MAX_LEAVES];
   41 
   42 void init()
   43 {
   44     /* FUNCTION init */
   45     leafCnt = 0;
   46     endOfTree = FALSE;
   47 } /* FUNCTION init */
   48 
   49 void dump()
   50 {
   51     /* FUNCTION dump */
   52 } /* FUNCTION dump */
   53 
   54 int getInput()
   55 {
   56     /* FUNCTION getInput */
   57     int dataReadFlag;
   58     char strng[MAX_LINE];
   59 
   60     dataReadFlag = (1 == scanf(" (%s) ", strng));
   61     if (dataReadFlag)
   62         {
   63             /* if a line of data was read */
   64             if (1 == strlen(strng))
   65                 {
   66                     /* hit end of tree */
   67                     printf("end of tree\n");
   68                     endOfTree = TRUE;
   69                 } /* hit end of tree */
   70             else
   71                 {
   72                     /* get next leaf */
   73                     strng[strlen(strng)-1] = 0; /* get rid of close parenthesis */
   74                     sscanf(strng, " %d , %s ", &tree[leafCnt].vl, tree[leafCnt].pth);
   75                     leafCnt++;
   76                     DEBUG printf("(len %d) [%s] (leafCnt %d) (vl %d) (pth [%s])\n", strlen(strng), strng, leafCnt, tree[leafCnt].vl, tree[leafCnt].pth);
   77                 } /* get next leaf */
   78         } /* if a line of data was read */
   79     return (dataReadFlag);
   80 } /* FUNCTION getInput */
   81 
   82 int validTree()
   83 {
   84     /* FUNCTION validTree */
   85     /*
   86      * sort by pth ascending (setting flag if dup found)
   87      * Invalid if:
   88      * 1) duplicate leaf
   89      *    if dup flag set
   90      * 2) missing parent of a leaf
   91      *    compare each leaf to next, ????
   92      *
   93      */
   94 } /* FUNCTION validTree */
   95 
   96 void dumpTree()
   97 {
   98     /* FUNCTION dumpTree */
   99 } /* FUNCTION dumpTree */
  100 
  101 void process()
  102 {
  103     /* FUNCTION process */
  104     while (! endOfTree)
  105         {
  106             /* process tree */
  107         } /* process tree */
  108     if (validTree())
  109         {
  110             /* found a valid tree */
  111             dumpTree();
  112         } /* found a valid tree */
  113     else
  114         {
  115             /* invlaid tree */
  116             printf("not complete\n");
  117         } /* invlaid tree */
  118     endOfTree = FALSE;
  119 } /* FUNCTION process */
  120 
  121 int main()
  122 {
  123     /* main */
  124     int moreToDo;
  125 
  126     init();
  127     moreToDo = getInput();
  128     while (moreToDo)
  129         {
  130             /* while */
  131             process();
  132             init();
  133             moreToDo = getInput();
  134         } /* while */
  135 
  136     return EXIT_SUCCESS;
  137 } /* main */
  138