Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/119/11988/c.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 
  107     /* create dummy head and tail nodes to insert before and after */
  108     tail = malloc(sizeof(nodeType));
  109     tail->c = '>';
  110     tail->nxt = NULL;
  111     head = malloc(sizeof(nodeType));
  112     head->c = '<';
  113     head->nxt = tail;
  114     cur = head;
  115 
  116     for (i=0; i<strlen(line); i++)
  117         {
  118             /* for each char in input line */
  119             switch (line[i])
  120                 {
  121                 /* all possible cases */
  122                 case '[':
  123                     cur = head;
  124                     break;
  125                 case ']':
  126                     cur = tail;
  127                     break;
  128                 default:
  129                 {
  130                     /* actually insert a char */
  131                     if (cur != tail)
  132                         insertAfter(cur, line[i]);
  133                     else
  134                         insertBefore(cur, line[i]);
  135                     cur = cur->nxt;
  136                     break;
  137                     } /* actually insert a char */
  138                 } /* all possible cases */
  139         } /* for each char in input line */
  140     /* now print each char */
  141     cur = head->nxt;
  142     while (NULL != cur->nxt)
  143         {
  144             /* print a char */
  145             printf("%c", cur->c);
  146             cur = cur->nxt;
  147         } /* print a char */
  148     printf("\n");
  149 } /* FUNCTION process */
  150 
  151 int main()
  152 {
  153     /* main */
  154     int moreToDo;
  155 
  156     init();
  157     moreToDo = getInput();
  158     while (moreToDo)
  159         {
  160             /* while */
  161             process();
  162             moreToDo = getInput();
  163         } /* while */
  164 
  165     return EXIT_SUCCESS;
  166 } /* main */
  167