Computer Programming Contest Preparation

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



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