Computer Programming Contest Preparation

ToolBox - Source for: 7/706/judged.c



/home/toolbox/public_html/solutions/7/706/judged.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:
   17  *    Date:
   18  * Purpose:
   19  * Problem:
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_DIGITS 20
   27 #define SEGMENTS   15
   28 #define DIGITS     10
   29 
   30 int wide;                   /* width of display */
   31 int number[MAX_DIGITS];     /* array of digitst to display */
   32 int digitCount;             /* number of digists in number */
   33 int s;                      /* repeat count */
   34 int LCD[SEGMENTS][DIGITS];  /* oversize LCD simulator */
   35 
   36 void init()
   37 {
   38     /* BEGIN FUNCTION init */
   39     int i;
   40     int j;
   41 
   42     /* initialize LCD to have each real segment on (1) and fake segments off
   43        seven segment turned into 5 rows of 3. fake refers to the 8 spots that
   44        are not part of the real LCD. loop below sets all 7 real segments on
   45        for all 10 digits */
   46     for (j=0; j< DIGITS; j++)
   47         {
   48             /* for */
   49             LCD[0][j] = 0;
   50             LCD[1][j] = 1;
   51             LCD[2][j] = 0;
   52             LCD[3][j] = 1;
   53             LCD[4][j] = 0;
   54             LCD[5][j] = 1;
   55             LCD[6][j] = 0;
   56             LCD[7][j] = 1;
   57             LCD[8][j] = 0;
   58             LCD[9][j] = 1;
   59             LCD[10][j] = 0;
   60             LCD[11][j] = 1;
   61             LCD[12][j] = 0;
   62             LCD[13][j] = 1;
   63             LCD[14][j] = 0;
   64         } /* for */
   65 
   66     /* layout of 7-segment LCD */
   67     /*  0  1  2  -   (0 and 2 are always blank) */
   68     /*  3  4  5 | |  (4 is always blank) */
   69     /*  6  7  8  -   (6 and 8 are always blank) */
   70     /*  9 10 11 | |  (10 is always blank) */
   71     /* 12 13 14  -   (12 and 14 are always blank) */
   72     /*  */
   73 
   74     /* customize for each digit */
   75 
   76     /* digit 0 */
   77     LCD[7][0] = 0;
   78 
   79     /* digit 1 */
   80     LCD[1][1] = 0;
   81     LCD[7][1] = 0;
   82     LCD[13][1] = 0;
   83     LCD[3][1] = 0;
   84     LCD[9][1] = 0;
   85 
   86     /* digit 2 */
   87     LCD[3][2] = 0;
   88     LCD[11][2] = 0;
   89 
   90     /* digit 3 */
   91     LCD[3][3] = 0;
   92     LCD[9][3] = 0;
   93 
   94     /* digit 4 */
   95     LCD[1][4] = 0;
   96     LCD[13][4] = 0;
   97     LCD[9][4] = 0;
   98 
   99     /* digit 5 */
  100     LCD[9][5] = 0;
  101     LCD[5][5] = 0;
  102 
  103     /* digit 6 */
  104     LCD[5][6] = 0;
  105 
  106     /* digit 7 */
  107     LCD[7][7] = 0;
  108     LCD[13][7] = 0;
  109     LCD[3][7] = 0;
  110     LCD[9][7] = 0;
  111 
  112     /* digit 8 */
  113 
  114     /* digit 9 */
  115     LCD[9][9] = 0;
  116 
  117 } /* BEGIN FUNCTION init */
  118 
  119 void printCharacterRow(char a, char b, char c)
  120 {
  121     /* BEGIN FUNCTION printCharacterRow */
  122     int i;
  123 
  124     printf("%c", a);
  125     for (i=0; i < wide; i++)
  126         printf("%c", b);
  127     printf("%c", c);
  128 } /* BEGIN FUNCTION printCharacterRow */
  129 
  130 void printRow(int x, char One)
  131 {
  132     /* BEGIN FUNCTION printRow */
  133     int i;
  134     char c1, c2, c3;
  135     char disp[2] = {' ', ' '};
  136 
  137     disp[1] = One; /* set the symbol for a one */
  138     for (i=0; (digitCount-1) > i; i++)
  139         {
  140             /* for */
  141             c1 = disp[LCD[x][number[i]]];
  142             c2 = disp[LCD[x+1][number[i]]];
  143             c3 = disp[LCD[x+2][number[i]]];
  144             printCharacterRow(c1, c2, c3);
  145             printf(" ");
  146         } /* for */
  147     c1 = disp[LCD[x][number[i]]];
  148     c2 = disp[LCD[x+1][number[i]]];
  149     c3 = disp[LCD[x+2][number[i]]];
  150     printCharacterRow(c1, c2, c3);
  151     printf("\n");
  152 } /* END FUNCTION printRow */
  153 
  154 void process()
  155 {
  156     /* BEGIN FUNCTION process */
  157     int i;
  158 
  159     /* top row */
  160     printRow(0, '-');
  161     for (i=0; i<wide; i++)
  162         printRow(3, '|');
  163     printRow(6, '-');
  164     for (i=0; i<wide; i++)
  165         printRow(9, '|');
  166     printRow(12, '-');
  167     printf("\n");
  168 } /* END FUNCTION process */
  169 
  170 int getInput()
  171 {
  172     /* FUNCTION getInput */
  173     int dataReadFlag = TRUE;
  174     unsigned char buffer[MAX_DIGITS];
  175     int i;
  176 
  177     scanf(" %d ", &wide);
  178     if (0 == wide)
  179         {
  180             /* end of data */
  181             dataReadFlag = FALSE;
  182         } /* end of data */
  183     else
  184         {
  185             /* get rest of data */
  186             fgets(buffer, sizeof(buffer), stdin);
  187             buffer[strlen(buffer) - 1] = 0;
  188             for (i=0,digitCount = 0; i<strlen(buffer); i++)
  189                 {
  190                     /* for */
  191                     if (isdigit(buffer[i]))
  192                         {
  193                             /* digit found */
  194                             number[digitCount++] = buffer[i] - '0';
  195                         } /* digit found */
  196                 } /* for */
  197         } /* get rest of data */
  198 
  199     return (dataReadFlag);
  200 } /* FUNCTION getInput */
  201 
  202 int main()
  203 {
  204     /* main */
  205     int moreToDo;
  206 
  207     init();
  208     moreToDo = getInput();
  209     while (moreToDo)
  210         {
  211             /* while */
  212             process();
  213             moreToDo = getInput();
  214         } /* while */
  215 
  216     return EXIT_SUCCESS;
  217 } /* main */
  218