Computer Programming Contest Preparation

ToolBox - Source for: 3/394/a-input-2.c



/home/toolbox/public_html/solutions/3/394/a-input-2.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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /* fprintf(stderr, "functionName: message", varslist); */
   17 
   18 #define MAX_NAME_LENGTH 15
   19 #define MAX_NUMBER_DIMENSIONS 15
   20 #define MAX_ARRAYS 10000
   21 
   22 /*
   23  *  Author: Isaac Traxler
   24  *    Date: 2021-08-26
   25  * Purpose: fun
   26  * Problem: 394 - Mapmaker
   27  */
   28 
   29 
   30 /*
   31  * This template reads data a specified number of times.
   32  */
   33 
   34 typedef struct ARRAY_STRUCT
   35 {
   36     char name[MAX_NAME_LENGTH];
   37     int base;
   38     int size;
   39     int dimCnt;
   40     int frst[MAX_NUMBER_DIMENSIONS];
   41     int last[MAX_NUMBER_DIMENSIONS];
   42 } ARRAY_STRUCT;
   43 
   44 
   45 
   46 int N;
   47 int R;
   48 ARRAY_STRUCT a[MAX_ARRAYS];
   49 char query[MAX_NAME_LENGTH];
   50 int indicies[MAX_NUMBER_DIMENSIONS];
   51 int qIdx;
   52 
   53 void init()
   54 {
   55     /* FUNCTION init */
   56     scanf(" %d %d ", &N, &R);
   57 } /* FUNCTION init */
   58 
   59 void dumpArrays()
   60 {
   61     /* FUNCTION dumpArrays */
   62     int i;
   63     int j;
   64 
   65     for (i=0; N>i; i++)
   66         {
   67             /* for each array */
   68             printf("%8d(%1d): %-11s[(%d..%d)", a[i].base, a[i].size, a[i].name, a[i].frst[1], a[i].last[1]);
   69             for (j=2; a[i].dimCnt>=j; j++)
   70                 {
   71                     /* for each additional dimension */
   72                     printf(", (%d..%d)", a[i].frst[j], a[i].last[j]);
   73                 } /* for each additional dimension */
   74             printf("]\n");
   75         } /* for each array */
   76 } /* FUNCTION dumpArrays */
   77 
   78 void dumpQuery()
   79 {
   80     /* FUNCTION dumpQuery */
   81     int i;
   82 
   83     printf("query: %s[%d", query, indicies[1]);
   84     for (i=2; a[qIdx].dimCnt>=i; i++)
   85         {
   86             /* for 2 thru last index of query */
   87             printf(", %d", indicies[i]);
   88         } /* for 2 thru last index of query */
   89     printf("]\n");
   90 } /* FUNCTION dumpQuery */
   91 
   92 void getArrayDefinitions()
   93 {
   94     /* FUNCTION getArrayDefinitions */
   95     int i;
   96     int j;
   97 
   98     for (i=0; N>i; i++)
   99         {
  100             /* for each array definitions */
  101             scanf(" %s ", a[i].name);
  102             scanf(" %d ", &a[i].base);
  103             scanf(" %d ", &a[i].size);
  104             scanf(" %d ", &a[i].dimCnt);
  105             for (j=1; j<=a[i].dimCnt; j++)
  106                 {
  107                     /* for each dimension */
  108                     scanf(" %d %d ", &a[i].frst[j], &a[i].last[j]);
  109                 } /* for each dimension */
  110         } /* for each array definitions */
  111 } /* FUNCTION getArrayDefinitions */
  112 
  113 int findArrayName(char name[])
  114 {
  115     /* FUNCTION findArrayName */
  116     int i;
  117     int fnd=-1;
  118 
  119     for (i=0; (N>i) && (0 != fnd); i++)
  120         {
  121             /* hunt for matching name */
  122             DEBUG printf("comparing |%s| to a[%d]|%s|", name, i, a[i].name);
  123             fnd = strcmp(name, a[i].name);
  124             DEBUG printf(" with result of %d\n", fnd);
  125         } /* hunt for matching name */
  126     return (i - 1);
  127 } /* FUNCTION findArrayName */
  128 
  129 void getReference()
  130 {
  131     /* FUNCTION getReference */
  132     int i;
  133 
  134     scanf(" %s ", query);
  135     DEBUG printf("query = [%s]\n", query);
  136     qIdx = findArrayName(query);
  137     DEBUG printf("%s is a[%d].name(%s) with %d indicies\n", query, qIdx, a[qIdx].name, a[qIdx].dimCnt);
  138     for (i=1; a[qIdx].dimCnt>=i; i++)
  139         {
  140             /* get each index */
  141             scanf(" %d ", &indicies[i]);
  142         } /* get each index */
  143 } /* FUNCTION getReference */
  144 
  145 void process()
  146 {
  147     /* FUNCTION process */
  148     dumpQuery();
  149 } /* FUNCTION process */
  150 
  151 int main()
  152 {
  153     /* main */
  154     int i;
  155 
  156     init();
  157     getArrayDefinitions();
  158     dumpArrays();
  159     for (i=0; i<R; i++)
  160         {
  161             /* while */
  162             DEBUG dumpArrays();
  163             getReference();
  164             DEBUG dumpArrays();
  165             process();
  166         } /* while */
  167 
  168     return EXIT_SUCCESS;
  169 } /* main */
  170 
  171