Computer Programming Contest Preparation

ToolBox - Source for: 1/188/a.c



/home/toolbox/public_html/solutions/1/188/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 /*
   17  *  Author:
   18  *    Date:
   19  * Purpose:
   20  * Problem:
   21  */
   22 
   23 /*
   24  * This template reads lines of data at a time until end of file.
   25  */
   26 
   27 #define MAX_LINE 257
   28 char line[MAX_LINE];
   29 char orgLine[MAX_LINE];
   30 
   31 void init()
   32 {
   33     /* FUNCTION init */
   34 } /* FUNCTION init */
   35 
   36 void dump()
   37 {
   38     /* FUNCTION dump */
   39 } /* FUNCTION dump */
   40 
   41 int getInput()
   42 {
   43     /* FUNCTION getInput */
   44     int dataReadFlag;
   45 
   46     fgets(orgLine, MAX_LINE, stdin);
   47     if (feof(stdin))
   48         dataReadFlag = FALSE;
   49     else
   50         {
   51             /* something to read */
   52             dataReadFlag = TRUE;
   53             orgLine[strlen(orgLine)-1] = ' ';
   54             strcpy(line, orgLine);
   55         } /* something to read */
   56     return (dataReadFlag);
   57 } /* FUNCTION getInput */
   58 
   59 unsigned long word(char tmp[])
   60 {
   61     /* FUNCTION word */
   62     unsigned long tot = 0;
   63     int i;
   64 
   65     DEBUG printf("token = [");
   66     for (i=0; strlen(tmp)>i; i++)
   67         {
   68             /* for */
   69             DEBUG printf("%c", tmp[i]);
   70             tot = (32 * tot) + tmp[i] - 'a' + 1;
   71         } /* for */
   72     DEBUG printf("]\n");
   73     return tot;
   74 } /* FUNCTION word */
   75 
   76 void process()
   77 {
   78     /* FUNCTION process */
   79     unsigned long tot = 0;
   80     unsigned long t1 = 0;
   81     char *p;
   82 
   83     DEBUG printf("1 line = [%s]\n", line);
   84     p = strtok(line, " ");
   85     while (NULL != p)
   86         {
   87             /* while */
   88             DEBUG printf("2 line = [%s]\n", line);
   89             t1 = word(p);
   90             tot = tot + t1;
   91             printf(" [%s] tot = %lu     t1 = %d\n", p, tot, t1);
   92             DEBUG tot = tot + word(p);
   93             p = strtok(NULL, " ");
   94         } /* while */
   95 
   96 
   97     printf("%s\n%lu\n\n", orgLine, tot);
   98 } /* FUNCTION process */
   99 
  100 int main()
  101 {
  102     /* main */
  103     int moreToDo;
  104 
  105     init();
  106     moreToDo = getInput();
  107     while (moreToDo)
  108         {
  109             /* while */
  110             process();
  111             moreToDo = getInput();
  112         } /* while */
  113 
  114     return EXIT_SUCCESS;
  115 } /* main */
  116