Computer Programming Contest Preparation

ToolBox - Source for: 4/444/a.c



/home/toolbox/public_html/solutions/4/444/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 #define MAX_LINE 257
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date:
   20  * Purpose: fun
   21  * Problem:
   22  */
   23 
   24 /*
   25  * This template reads lines of data at a time until end of file.
   26  */
   27 
   28 char line[MAX_LINE];
   29 
   30 void init()
   31 {
   32     /* FUNCTION init */
   33 } /* FUNCTION init */
   34 
   35 void dump()
   36 {
   37     /* FUNCTION dump */
   38 } /* FUNCTION dump */
   39 
   40 int getInput()
   41 {
   42     /* FUNCTION getInput */
   43     int dataReadFlag;
   44 
   45     fgets(line, MAX_LINE, stdin);
   46     if (feof(stdin))
   47         dataReadFlag = FALSE;
   48     else
   49         {
   50             /* something to read */
   51             dataReadFlag = TRUE;
   52             line[strlen(line)-1] = 0;
   53         } /* something to read */
   54     return (dataReadFlag);
   55 } /* FUNCTION getInput */
   56 
   57 void reverse(char t)
   58 {
   59     /* FUNCTION reverse */
   60     int strt;
   61 
   62     strt = t;
   63     while (0 < strt)
   64         {
   65             /* while */
   66             printf("%d", (strt%10));
   67             strt = strt / 10;
   68         } /* while */
   69 } /* FUNCTION reverse */
   70 
   71 void decode()
   72 {
   73     /* FUNCTION decode */
   74     /* sample 797218999 is czba */
   75     int i;
   76     char ans[MAX_LINE];
   77     int a = 0;
   78     int tmp;
   79 
   80     i = strlen(line) - 1;
   81     while (0<i)
   82         {
   83             /* while */
   84             tmp = 0;
   85             if ('1' == line[i])
   86                 {
   87                     /* 3 digit char */
   88                     tmp = 1;
   89                     i--;
   90                 } /* 3 digit char */
   91             /* process next two digits */
   92             tmp = tmp * 10 + (line[i] - '0');
   93             i--;
   94             tmp = tmp * 10 + (line[i] - '0');
   95             i--;
   96             ans[a] = tmp;
   97             a++;
   98         } /* while */
   99     ans[a] = 0;
  100     printf("%s\n", ans);
  101 
  102 } /* FUNCTION decode */
  103 
  104 void encode()
  105 {
  106     /* FUNCTION encode */
  107     int i;
  108 
  109     for (i=(strlen(line)-1); 0<= i; i--)
  110         {
  111             /* for each char */
  112             reverse(line[i]);
  113         } /* for each char */
  114     printf("\n");
  115 } /* FUNCTION encode */
  116 
  117 void process()
  118 {
  119     /* FUNCTION process */
  120     DEBUG printf("line = [%s]\n", line);
  121     if (isdigit(line[0]))
  122         decode();
  123     else
  124         encode();
  125 } /* FUNCTION process */
  126 
  127 int main()
  128 {
  129     /* main */
  130     int moreToDo;
  131 
  132     init();
  133     moreToDo = getInput();
  134     while (moreToDo)
  135         {
  136             /* while */
  137             process();
  138             moreToDo = getInput();
  139         } /* while */
  140 
  141     return EXIT_SUCCESS;
  142 } /* main */
  143