Computer Programming Contest Preparation

ToolBox - Source for: 102/10295/a.c



/home/toolbox/public_html/solutions/102/10295/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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /* fprintf(stderr, "functionName: message", varslist); */
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2020-09-04
   20  * Purpose: fun
   21  * Problem: 10295
   22  */
   23 
   24 #define MAX_WORD 20
   25 #define MAX_DICT 1005
   26 #define MAX_LINE 10026
   27 
   28 struct dictStruct
   29 {
   30     /* STRUCT dictStruct */
   31     char str[MAX_WORD];
   32     int amt;
   33 } /* STRUCT dictStruct */;
   34 
   35 struct dictStruct words[MAX_DICT];
   36 int numWords;
   37 int jobs;
   38 char line[MAX_LINE+2];
   39 
   40 void init()
   41 {
   42     /* FUNCTION init */
   43     scanf(" %d %d ", &numWords, &jobs);
   44 } /* FUNCTION init */
   45 
   46 void dump()
   47 {
   48     /* FUNCTION dump */
   49     int i;
   50 
   51     printf("Dictionary:\n");
   52     for (i=0; numWords>i; i++)
   53         {
   54             /*load each word */
   55             printf("%d: %s %d\n", i, words[i].str, words[i].amt);
   56         } /*load each word */
   57 } /* FUNCTION dump */
   58 
   59 void getDictionary()
   60 {
   61     /* FUNCTION getDictionary */
   62     int i;
   63 
   64     for (i=0; numWords>i; i++)
   65         {
   66             /* load each word */
   67             scanf(" %s %d ", &words[i].str, &words[i].amt);
   68             DEBUG printf("%d: [%s] [%d]\n", i, words[i].str, words[i].amt);
   69         } /* load each word */
   70     DEBUG dump();
   71 } /* FUNCTION getDictionary */
   72 
   73 int valuate(char wrd[])
   74 {
   75     /* FUNCTION valuate */
   76     int ret = 0;
   77     int i;
   78 
   79     for (i=0; i<numWords; i++)
   80         {
   81             /* for */
   82             if (0 == strcmp(wrd, words[i].str))
   83                 {
   84                     /* match found */
   85                     ret = words[i].amt;
   86                     i = numWords;
   87                 } /* match found */
   88         } /* for */
   89     return (ret);
   90 } /* FUNCTION valuate */
   91 
   92 int endCheck()
   93 {
   94     /* FUNCTION endCheck */
   95     char ch;
   96     int i;
   97     int slen;
   98     int cnt = 0;
   99     int ret;
  100 
  101     slen = strlen(line);
  102     for (i=0; slen>i; i++)
  103         {
  104             /* for */
  105             if (' ' != line[i])
  106                 {
  107                     /* nonblank */
  108                     cnt++;
  109                     ch = line[i];
  110                 } /* nonblank */
  111         } /* for */
  112     ret = (1 == cnt) && ('.' == ch);
  113 
  114     return ret;
  115 } /* FUNCTION endCheck */
  116 
  117 void process()
  118 {
  119     /* FUNCTION process */
  120     int tot = 0;
  121     char *token;
  122     const char delim[7] = " \t\n\v\f\r";
  123     char buffer[MAX_LINE];
  124     int i;
  125 
  126     fgets(line, MAX_LINE, stdin);
  127     line[strlen(line) - 1] = 0;
  128     while (! endCheck())
  129         {
  130             /* process each line */
  131             DEBUG printf("[%s]\n", line);
  132             /* tokenize line and check eaach word */
  133             token = strtok(line, delim);
  134             while (NULL != token)
  135                 {
  136                     /* while */
  137                     DEBUG printf("[%s]\n", token);
  138                     tot = tot + valuate(token);
  139                     token = strtok(NULL, delim);
  140                 } /* while */
  141             /* get next line */
  142             fgets(line, MAX_LINE, stdin);
  143             line[strlen(line) - 1] = 0;
  144         } /* process each line */
  145     printf("%d\n", tot);
  146 } /* FUNCTION process */
  147 
  148 int main()
  149 {
  150     /* main */
  151     int i;
  152 
  153     init();
  154     getDictionary();
  155     for (i=0; i<jobs; i++)
  156         {
  157             /* while */
  158             process();
  159         } /* while */
  160 
  161     return EXIT_SUCCESS;
  162 } /* main */
  163 
  164