Computer Programming Contest Preparation

ToolBox - Source for: 4/483/j.c



/home/toolbox/public_html/solutions/4/483/j.c
    1 #include <stdio.h>
    2 #include <ctype.h>
    3 #include <string.h>
    4 #include <sys/types.h>
    5 #include <sys/stat.h>
    6 #include <fcntl.h>
    7 #include <stdint.h>
    8 #include <math.h>
    9 #include <stdlib.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 #define MAX_STACK 1024
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2015-02-06
   21  * Purpose:
   22  * Problem: 483
   23  */
   24 
   25 /*
   26  * This template reads data until a terminating value is reached.
   27  */
   28 
   29 char stack[MAX_STACK];
   30 int top = 0;
   31 
   32 void push(char x)
   33 {
   34     /* FUNCTION push */
   35     if (MAX_STACK > top)
   36         {
   37             /* room to push */
   38             stack[top++] = x;
   39         } /* room to push */
   40 } /* FUNCTION push */
   41 
   42 char pop()
   43 {
   44     /* FUNCTION pop */
   45     char ret = 0;
   46 
   47     if (0 < top)
   48         {
   49             /* something to pop */
   50             ret = stack[--top];
   51         } /* something to pop */
   52     return ret;
   53 } /* FUNCTION pop */
   54 
   55 int isEmpty()
   56 {
   57     /* FUNCTION isEmpty */
   58     return (1 > top);
   59 } /* FUNCTION isEmpty */
   60 
   61 void dumpWord()
   62 {
   63     /* FUNCTION dumpWord */
   64     char c;
   65 
   66     while (! isEmpty())
   67         {
   68             /* while */
   69             c = pop();
   70             printf("%c", c);
   71         } /* while */
   72 } /* FUNCTION dumpWord */
   73 
   74 void process()
   75 {
   76     /* FUNCTION process */
   77     char c;
   78 
   79     while (! feof(stdin))
   80         {
   81             /* while a char to read */
   82             c = fgetc(stdin);
   83             if (isspace(c))
   84                 {
   85                     /* found whitespace */
   86                     dumpWord();
   87                     printf("%c", c);
   88                 } /* found whitespace */
   89             else
   90                 {
   91                     /* "normal" character */
   92                     push(c);
   93                 } /* "normal" character */
   94         } /* while a char to read */
   95 
   96 } /* FUNCTION process */
   97 
   98 int main ()
   99 {
  100     /* main */
  101     process();
  102 
  103     return EXIT_SUCCESS;
  104 } /* main */
  105