Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/119/11988/b.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 insertNode(nodeType* cur, char c)
   67 {
   68     /* FUNCTION insertNode */
   69     nodeType* tmp;
   70 
   71     tmp = malloc(sizeof(nodeType));
   72     if (NULL == tmp)
   73         {
   74             exit -99;
   75         }
   76     tmp->c = c;
   77     tmp->nxt = cur->nxt;
   78     cur->nxt = tmp;
   79     printf("char = %c     tmp=%d    cur=%d\n", tmp->c, tmp, cur);
   80 } /* FUNCTION insertNode */
   81 
   82 void process()
   83 {
   84     /* FUNCTION process */
   85     int i;
   86     nodeType* cur;
   87 
   88     printf("1\n");
   89     /* create dummy head and tail nodes to insert before and after */
   90     tail = malloc(sizeof(nodeType));
   91     tail->c = '>';
   92     tail->nxt = NULL;
   93     head = malloc(sizeof(nodeType));
   94     head->c = '<';
   95     head->nxt = tail;
   96     cur = head;
   97     printf("head=%d n=%d    tail=%d n=%d\n", head, head->nxt, tail, tail->nxt);
   98     printf("2\n");
   99 
  100     for (i=0; i<strlen(line); i++)
  101         {
  102             /* for each char in input line */
  103             switch (line[i])
  104                 {
  105                 /* all possible cases */
  106                 /*
  107                   case '[': cur = head; break;
  108                   case ']': cur = tail; break;
  109                 */
  110                 default:
  111                 {
  112                     /* actually insert a char */
  113                     insertNode(cur, line[i]);
  114                     cur = cur->nxt;
  115                     break;
  116                     } /* actually insert a char */
  117                 } /* all possible cases */
  118         } /* for each char in input line */
  119     /* now print each char */
  120     printf("head=%d n=%d    tail=%d n=%d\n", head, head->nxt, tail, tail->nxt);
  121     printf("3\n");
  122     cur = head;
  123     while (NULL != cur->nxt)
  124         {
  125             /* print a char */
  126             printf("%c", cur->c);
  127             printf("\ncur=%d n=%d\n", cur, cur->nxt);
  128             cur = cur->nxt;
  129             printf("cur=%d n=%d\n", cur, cur->nxt);
  130         } /* print a char */
  131     printf("\n");
  132     printf("4\n");
  133 } /* FUNCTION process */
  134 
  135 int main()
  136 {
  137     /* main */
  138     int moreToDo;
  139 
  140     init();
  141     moreToDo = getInput();
  142     while (moreToDo)
  143         {
  144             /* while */
  145             process();
  146             moreToDo = getInput();
  147         } /* while */
  148 
  149     return EXIT_SUCCESS;
  150 } /* main */
  151