home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 371 / advanc_c.cpp
Encoding:
C/C++ Source or Header  |  1996-08-06  |  6.0 KB  |  144 lines

  1. /**************************************************************************
  2. *    File Name:    stacks.cpp
  3. *    Description:    Using Stacks
  4. *    Author:        Arlyn K. Chesley
  5. *    Date:          2/15/96 started
  6. *
  7. *               Program description Purpose:
  8. * To understand how Stacks work.  A Stack is a FILO(first-in-last-out)
  9. * Use a debugger to step through this program.  Watch the stacks as
  10. * items are added to and removed from the stack.
  11. * Need to implement 4 functions stack_new, stack_push, stack_pop,
  12. * and stack_free
  13. *************************************************************************/
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17.  
  18.  
  19. /************************************************************************
  20. * function prototyping
  21. *************************************************************************/
  22. int stack_new(
  23.          int size,       /* I-size of a single element                 */
  24.          int count,      /* I-Maximum number of data elements          */
  25.          long *key);     /* O-Key to the newly created stack area      */
  26.  
  27. int stack_push(
  28.          long key,        /* I-Key to the stack to operate upon        */
  29.          char *data);     /* I-Pointer to data element to be saved     */
  30.  
  31. int stack_pop(
  32.          long key,        /* I-Key to the stack to operate upon        */
  33.          char *data);     /* O-Place retrieved data is to be copied to */
  34.  
  35. int stack_free(
  36.           long key);      /* I-Key to the stack to operate upon        */
  37.  
  38. #define RC_OK            0    /* Operation successful                      */
  39. #define RC_OVERFLOW      1    /* Stack overflow condition encountered      */
  40. #define RC_UNDERFLOW     2    /* Stack underflow condition encountered     */
  41. #define RC_MALLOC_ERROR  3    /* Memory allocation error occcurred         */
  42.  
  43. #include "d:\advanc_c\source_c\p1.cpp"
  44.  
  45. main()
  46.    {
  47.    char    str[80];            /* Buffer to hold the string                */
  48.    int     ndx;                /* Index into the string buffer             */
  49.    long    number_key;         /* Identifies stack which holds numbers     */
  50.    long    letter_key;         /* Identifies stack which holds letters       */
  51.    long    work_key;           /* Stack to use for current character       */
  52.    char    value;              /* Value popped from the stack              */
  53.    int     rc;                 /* Status return code                       */
  54.  
  55. /***************************************************************************
  56. * Allocate a stack to hold the numbers
  57. ****************************************************************************/
  58.  
  59. rc = stack_new(sizeof(char), 40, &number_key);
  60. if(rc != RC_OK)
  61.    {  /* Number failure */
  62.    printf("Error %d allocating number stack\n", rc);
  63.    exit(0);
  64.    } /* Number failure */
  65.  
  66. /***************************************************************************
  67. * Allocate a stack to hold the letters
  68. ****************************************************************************/
  69.  
  70. rc = stack_new(sizeof(char), 40, &letter_key);
  71. if(rc != RC_OK)
  72.    {  /* Letter failure */
  73.    printf("Error %d allocating character stack\n", rc);
  74.    exit(0);
  75.    } /* Letter failure */
  76.  
  77. /***************************************************************************
  78. * Process string read from the keyboard until an EOF is
  79. * encountered or until a empty string is retrieved
  80. ****************************************************************************/
  81. while(gets(str) !=NULL && str[0] != '\0')
  82.    { /* String found */
  83.    /************************************************************************
  84.    * Look at each character in the string. Seperate them into
  85.    * numeric characters and letter characters.
  86.    *************************************************************************/
  87.    for(ndx = 0; str[ndx] != '\0'; ndx++)
  88.      { /* Process string  */
  89.        if(str[ndx] >= '0' && str[ndx] <= '9')
  90.       work_key = number_key;   /* Save to the number stack */
  91.        else
  92.       work_key = letter_key;   /* Save to the letter stack */
  93.        /********************************************************************
  94.        * Push the character onto it's stack.
  95.        *********************************************************************/
  96.  
  97.        rc = stack_push(work_key, &str[ndx]);
  98.        if(rc != RC_OK)
  99.        { /* Error */
  100.        printf("Error %d pushing %c onto stack\n", rc, str[ndx]);
  101.        exit(0);
  102.        }  /* Error */
  103.      }    /* Process string  */
  104.  /*************************************************************************
  105.   * Print the numbers from the string in reverse order. Note: the
  106.   * print operation will empty the stack which should reset it for
  107.   * the next string
  108.   *************************************************************************/
  109.   printf("Numbers in reverse order are: ");
  110.   rc = stack_pop(number_key, &value);
  111.   while(rc == RC_OK)
  112.      { /* Print it */
  113.      printf("%c", value);
  114.      rc = stack_pop(number_key, &value);
  115.      } /* Print it  */
  116.  if(rc == RC_UNDERFLOW)
  117.     { /* Number print errror */
  118.     printf("Error %d while printing numbers\n", rc);
  119.     exit(0);
  120.     } /* Number print error */
  121.  /*************************************************************************
  122.   * Print the letters from the string in reverse order.  Note: the
  123.   * print operation will empty the stack which should reset it for the
  124.   * next string.
  125.   *************************************************************************/
  126.  
  127.   printf("\n\nLetters in reverse order are: ");
  128.   while(stack_pop(letter_key, &value) == RC_OK)
  129.      printf("%c", value);
  130.   if(rc == RC_UNDERFLOW)
  131.      { /* Number print error  */
  132.      printf("Error %d while printing letters\n", rc);
  133.      exit(0);
  134.      } /* Number print error */
  135.  printf("\n\n");    /* Give us some space between runs  */
  136.  } /* String found  */
  137. /*************************************************************************
  138.   * We're done.  Don't forget to free the stacks we allocated.
  139.   *************************************************************************/
  140.  
  141. stack_free(number_key);
  142. stack_free(letter_key);
  143. return(0);
  144. }  /* Main */