Computer Programming Contest Preparation

ToolBox - Source for: 119/11988/a.c



/home/toolbox/public_html/solutions/119/11988/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 100001
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date:
   20  * Purpose: fun
   21  * Problem: 11988 Broken Keyboard
   22  */
   23 
   24 /*
   25  * This template reads lines of data at a time until end of file.
   26  */
   27 
   28 typedef struct nodeStruct
   29 {
   30     /* node */
   31     char c;
   32     struct nodeStruct* nxt;
   33 } nodeType; /* node */
   34 
   35 char line[MAX_LINE+5];
   36 nodeType* head;
   37 nodeType* tail;
   38 
   39 void init()
   40 {
   41     /* FUNCTION init */
   42 } /* FUNCTION init */
   43 
   44 void dump()
   45 {
   46     /* FUNCTION dump */
   47 } /* FUNCTION dump */
   48 
   49 int getInput()
   50 {
   51     /* FUNCTION getInput */
   52     int dataReadFlag;
   53 
   54     fgets(line, MAX_LINE-1, stdin);
   55     if (feof(stdin))
   56         dataReadFlag = FALSE;
   57     else
   58         {
   59             /* something to read */
   60             dataReadFlag = TRUE;
   61             line[strlen(line)-1] = 0;
   62         } /* something to read */
   63     return (dataReadFlag);
   64 } /* FUNCTION getInput */
   65 
   66 void insertBefore(nodeType* cur, char c)
   67 {
   68     /* FUNCTION insertBefore */
   69     /* only used when inserting before tail */
   70     nodeType* tmp;
   71 
   72     tmp = malloc(sizeof(nodeType));
   73     if (NULL == tmp)
   74         {
   75             exit -99;
   76         }
   77     tmp->c = '>';
   78     tmp->nxt = NULL;
   79     tail->c = c;
   80     tail->nxt = tmp;
   81     tail = tmp;
   82     cur->nxt = tmp;
   83 } /* FUNCTION insertBefore */
   84 
   85 void insertAfter(nodeType* cur, char c)
   86 {
   87     /* FUNCTION insertAfter */
   88     nodeType* tmp;
   89 
   90     tmp = malloc(sizeof(nodeType));
   91     if (NULL == tmp)
   92         {
   93             exit -99;
   94         }
   95     tmp->c = c;
   96     tmp->nxt = cur->nxt;
   97     cur->nxt = tmp;
   98     DEBUG printf("char = %c     tmp=%d    cur=%d\n", tmp->c, tmp, cur);
   99 } /* FUNCTION insertNode */
  100 
  101 void process()
  102 {
  103     /* FUNCTION process */
  104     int i;
  105     nodeType* cur;
  106     int sl;
  107 
  108     /* create dummy head and tail nodes to insert before and after */
  109     tail = malloc(sizeof(nodeType));
  110     tail->c = '>';
  111     tail->nxt = NULL;
  112     head = malloc(sizeof(nodeType));
  113     head->c = '<';
  114     head->nxt = tail;
  115     cur = head;
  116 
  117     sl = strlen(line);
  118     for (i=0; i<sl; i++)
  119         {
  120             /* for each char in input line */
  121             switch (line[i])
  122                 {
  123                 /* all possible cases */
  124                 case '[':
  125                     cur = head;
  126                     break;
  127                 case ']':
  128                     cur = tail;
  129                     break;
  130                 default:
  131                 {
  132                     /* actually insert a char */
  133                     if (cur != tail)
  134                         insertAfter(cur, line[i]);
  135                     else
  136                         insertBefore(cur, line[i]);
  137                     cur = cur->nxt;
  138                     break;
  139                     } /* actually insert a char */
  140                 } /* all possible cases */
  141         } /* for each char in input line */
  142 
  143     /* now print each char */
  144     cur = head->nxt;
  145     while (NULL != cur->nxt)
  146         {
  147             /* print a char */
  148             printf("%c", cur->c);
  149             head = cur;
  150             cur = cur->nxt;
  151             free(head);
  152         } /* print a char */
  153     printf("\n");
  154 } /* FUNCTION process */
  155 
  156 int main()
  157 {
  158     /* main */
  159     int moreToDo;
  160 
  161     init();
  162     moreToDo = getInput();
  163     while (moreToDo)
  164         {
  165             /* while */
  166             process();
  167             moreToDo = getInput();
  168         } /* while */
  169 
  170     return EXIT_SUCCESS;
  171 } /* main */
  172