Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/102/10260/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: Isaac Traxler
   18  *    Date: 2016-03-29
   19  * Purpose: fun
   20  * Problem: 10260 - Soundex
   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 #define B 1
   29 #define F 1
   30 #define P 1
   31 #define V 1
   32 #define C 2
   33 #define G 2
   34 #define J 2
   35 #define K 2
   36 #define Q 2
   37 #define S 2
   38 #define X 2
   39 #define Z 2
   40 #define D 3
   41 #define T 3
   42 #define L 4
   43 #define M 5
   44 #define N 5
   45 #define R 6
   46 
   47 
   48 char line[MAX_LINE];
   49 int tbl[256];
   50 
   51 void init()
   52 {
   53     /* FUNCTION init */
   54     int i;
   55 
   56     for (i=0; i<256; i++)
   57         {
   58             /* for */
   59             tbl[i] = 0;
   60         } /* for */
   61     /* set special */
   62     tbl['B'] =  B;
   63     tbl['F'] =  F;
   64     tbl['P'] =  P;
   65     tbl['V'] =  V;
   66     tbl['C'] =  C;
   67     tbl['G'] =  G;
   68     tbl['J'] =  J;
   69     tbl['K'] =  K;
   70     tbl['Q'] =  Q;
   71     tbl['S'] =  S;
   72     tbl['X'] =  X;
   73     tbl['Z'] =  Z;
   74     tbl['D'] =  D;
   75     tbl['T'] =  T;
   76     tbl['L'] =  L;
   77     tbl['M'] =  M;
   78     tbl['N'] =  N;
   79     tbl['R'] =  R;
   80 
   81 } /* FUNCTION init */
   82 
   83 void dump()
   84 {
   85     /* FUNCTION dump */
   86 } /* FUNCTION dump */
   87 
   88 int getInput()
   89 {
   90     /* FUNCTION getInput */
   91     int dataReadFlag;
   92 
   93     fgets(line, MAX_LINE, stdin);
   94     if (feof(stdin))
   95         dataReadFlag = FALSE;
   96     else
   97         {
   98             /* something to read */
   99             dataReadFlag = TRUE;
  100             line[strlen(line)-1] = 0;
  101         } /* something to read */
  102     return (dataReadFlag);
  103 } /* FUNCTION getInput */
  104 
  105 void process()
  106 {
  107     /* FUNCTION process */
  108     int prev = 0;
  109     int i;
  110     char otpt[MAX_LINE];
  111     int cnt = 0;
  112 
  113     for (i=0; strlen(line)>i; i++)
  114         {
  115             /* for */
  116             if ((0 != tbl[line[i]]) && (prev != tbl[line[i]]))
  117                 {
  118                     /* found another value */
  119                     otpt[cnt++] = '0' + tbl[line[i]];
  120                 } /* found another value */
  121             prev = tbl[line[i]];
  122         } /* for */
  123     otpt[cnt] = 0;
  124     printf("%s\n", otpt);
  125 
  126 } /* FUNCTION process */
  127 
  128 int main()
  129 {
  130     /* main */
  131     int moreToDo;
  132 
  133     init();
  134     moreToDo = getInput();
  135     while (moreToDo)
  136         {
  137             /* while */
  138             process();
  139             moreToDo = getInput();
  140         } /* while */
  141 
  142     return EXIT_SUCCESS;
  143 } /* main */
  144