1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <math.h>
    4 #include <string.h>
    5 
    6 #define	GAL_TO_LITERS	(3.7854)
    7 #define	LITERS_TO_GAL	(0.2642)
    8 #define	LB_TO_KG		(0.4536)
    9 #define	KG_TO_LB		(2.2046)
   10 
   11 int main()
   12 {
   13   char ibuf[256], *s, *t;
   14   int i, n;
   15   double v, cf;
   16 
   17   /* Get # problems */
   18   if (fgets(&(ibuf[0]), sizeof(ibuf), stdin) == NULL)
   19     {
   20       return(0);
   21     }
   22   n = atoi(&(ibuf[0]));
   23 
   24   /* Process each dataset */
   25   for (i = 1; i <= n; i++)
   26     {
   27       /* Get next value */
   28       if (fgets(&(ibuf[0]), sizeof(ibuf), stdin) == NULL)
   29         {
   30           break;
   31         }
   32       v = atof(&(ibuf[0]));
   33       s = strchr(&(ibuf[0]), ' ');
   34       if (s == NULL)
   35         {
   36           continue;
   37         }
   38       s++;
   39       cf = 1;
   40       t = "?";
   41       switch (*s)
   42         {
   43         case 'g':
   44           /* Gallons to litres */
   45           cf = GAL_TO_LITERS;
   46           t = "l";
   47           break;
   48 
   49         case 'l':
   50           /* pounds, litres */
   51           if (s[1] == 'b')
   52             {
   53               /* Pounds to kg */
   54               cf = LB_TO_KG;
   55               t = "kg";
   56             }
   57           else
   58             {
   59               /* liters to gallons */
   60               cf = LITERS_TO_GAL;
   61               t = "g";
   62             }
   63           break;
   64 
   65         case 'k':
   66           /* kg to pounds */
   67           cf = KG_TO_LB;
   68           t = "lb";
   69           break;
   70         }
   71       /* Convert */
   72       v = v*cf;
   73       printf("%d %.4f %s\n", i, v, t);
   74     }
   75   return(0);
   76 }
   77