Computer Programming Contest Preparation

ToolBox - Source for: 15/1586/a.c



/home/toolbox/public_html/solutions/15/1586/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-01
   20  * Purpose: fun
   21  * Problem: 1586
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 #define MAX_LINE 132
   29 
   30 const double CW = 12.01;
   31 const double HW = 1.008;
   32 const double OW = 16.00;
   33 const double NW = 14.01;
   34 
   35 int numberOfTimes;
   36 char str[MAX_LINE];
   37 
   38 void init()
   39 {
   40     /* FUNCTION init */
   41     scanf("%d ", &numberOfTimes);
   42 } /* FUNCTION init */
   43 
   44 void dump()
   45 {
   46     /* FUNCTION dump */
   47 } /* FUNCTION dump */
   48 
   49 void getInput()
   50 {
   51     /* FUNCTION getInput */
   52     fgets(str, MAX_LINE - 1, stdin);
   53 } /* FUNCTION getInput */
   54 
   55 void process()
   56 {
   57     /* FUNCTION process */
   58     double wt = 0.0;
   59     int i;
   60     int sl;
   61     double tmp;
   62     int cnt;
   63 
   64     sl = strlen(str) - 1; /* subtract 1 to ignore newline */
   65     for (i=0; i<sl;)
   66         {
   67             /* for each char */
   68             cnt = 1;
   69             switch (str[i])
   70                 {
   71                     /* switch */
   72                 case 'C':
   73                     tmp = CW;
   74                     break;
   75                 case 'H':
   76                     tmp = HW;
   77                     break;
   78                 case 'N':
   79                     tmp = NW;
   80                     break;
   81                 case 'O':
   82                     tmp = OW;
   83                     break;
   84                     /* default needed to ignore whitespace */
   85                 default:
   86                     tmp = 0.0;
   87                 } /* switch */
   88             i++;
   89             if (isdigit(str[i]))
   90                 {
   91                     /* got a digit */
   92                     cnt = str[i] - '0';
   93                     i++;
   94                     if (isdigit(str[i]))
   95                         {
   96                             /* got a second digit */
   97                             cnt = cnt * 10 + str[i] - '0';
   98                             i++;
   99                         } /* got a second digit */
  100                 } /* got a digit */
  101             wt = wt + (cnt * tmp);
  102         } /* for each char */
  103     printf("%.3f\n", wt);
  104 
  105 } /* FUNCTION process */
  106 
  107 int main()
  108 {
  109     /* main */
  110     int i;
  111 
  112     init();
  113     for (i=0; i<numberOfTimes; i++)
  114         {
  115             /* while */
  116             getInput();
  117             process();
  118         } /* while */
  119 
  120     return EXIT_SUCCESS;
  121 } /* main */
  122 
  123