Computer Programming Contest Preparation

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



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