Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/4/400/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_LINE 257
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2020-09-04
   20  * Purpose: fun
   21  * Problem: 400
   22  */
   23 
   24 /*
   25  * This template reads lines of data at a time until end of file.
   26  */
   27 
   28 #define FILE_NAME_LENGTH 65
   29 #define MAX_FILES 105
   30 
   31 int numFiles;
   32 char files[MAX_FILES][FILE_NAME_LENGTH];
   33 char line[MAX_LINE];
   34 int maxLength;
   35 
   36 void init()
   37 {
   38     /* FUNCTION init */
   39 } /* FUNCTION init */
   40 
   41 void dump()
   42 {
   43     /* FUNCTION dump */
   44     int i;
   45 
   46     for (i=0; numFiles>i; i++)
   47         {
   48             printf("%3d: %s\n", i, files[i]);
   49         }
   50 } /* FUNCTION dump */
   51 
   52 int getInput()
   53 {
   54     /* FUNCTION getInput */
   55     int dataReadFlag;
   56     int tmp;
   57     int i;
   58 
   59     fgets(files[0], MAX_LINE, stdin);
   60     if (feof(stdin))
   61         dataReadFlag = FALSE;
   62     else
   63         {
   64             /* something to read */
   65             dataReadFlag = TRUE;
   66             maxLength = 0;
   67             sscanf(files[0], " %d ", &numFiles);
   68             for (i=0; numFiles>i; i++)
   69                 {
   70                     /* for */
   71                     fgets(files[i], MAX_LINE, stdin);
   72                     tmp = strlen(files[i]) - 1;
   73                     files[i][tmp] = 0;
   74                     maxLength = (maxLength < tmp) ? tmp : maxLength;
   75                 } /* for */
   76         } /* something to read */
   77     return (dataReadFlag);
   78 } /* FUNCTION getInput */
   79 
   80 static int compString(const void *a, const void *b)
   81 {
   82     /* FUNCTION compString */
   83     return (strcmp((char *)a, (char *)b));
   84 } /* FUNCTION compString */
   85 
   86 void process()
   87 {
   88     /* FUNCTION process */
   89     int i;
   90     int j;
   91     int cnt;
   92     int tot;
   93     int numRows;
   94     int tmp;
   95 
   96     DEBUG dump();
   97     qsort(files, numFiles, sizeof(files[0]), compString);
   98     DEBUG dump();
   99     for (i=0; 60>i; i++)
  100         {
  101             printf("-");
  102         }
  103     printf("\n");
  104     /* calculate how many colums we have */
  105     tot = maxLength;
  106     cnt = 0;
  107     while (61 > tot)
  108         {
  109             /* while */
  110             tot = tot + maxLength + 2;
  111             cnt++;
  112         } /* while */
  113     /* calculate number of rows needed */
  114     numRows = (numFiles + cnt - 1) / cnt;
  115     /* print out each row */
  116     DEBUG printf("(numFiles %d)  (rows %d)  (cols %d)\n", numFiles, numRows, cnt);
  117     for (i=0; numRows>i; i++)
  118         {
  119             /* for each row */
  120             /* first column */
  121             DEBUG printf("%2d:", i);
  122             printf("%-*s", maxLength, files[i]);
  123             for (j=1; cnt>j; j++)
  124                 {
  125                     /* columns 2 thru n */
  126                     tmp = i + (j * numRows);
  127                     if (numFiles > tmp)
  128                         {
  129                             /* filename to print */
  130                             DEBUG  printf("  %2d:", tmp);
  131                             printf("  %-*s", maxLength, files[tmp]);
  132                         } /* filename to print */
  133                 } /* columns 2 thru n */
  134             printf("\n");
  135         } /* for each row */
  136 } /* FUNCTION process */
  137 
  138 int main()
  139 {
  140     /* main */
  141     int moreToDo;
  142 
  143     init();
  144     moreToDo = getInput();
  145     while (moreToDo)
  146         {
  147             /* while */
  148             process();
  149             moreToDo = getInput();
  150         } /* while */
  151 
  152     return EXIT_SUCCESS;
  153 } /* main */
  154