🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2072

Need help with Stacks

#2072
From: Arlyn Chesley
Date: Sun, 25 Feb 1996 18:12
268 lines
9298 bytes
Need help with getting data onto and off of stacks

Main program is not supposed to change.  Functions are supposed to
work with main.  right now I always get a 2 off of the stack no matter
what character, number or mixed string I put onto the two stacks.

My functions file follows the main

Any help is greatly appreciated
e-mail arlyn@airmail.net
or post
Thanks.


/**************************************************************************
*	File Name:	stacks.cpp
*	Description:    Using Stacks
*	Author:		Arlyn K. Chesley
*	Date:  		2/15/96 started
*
*		       Program description Purpose:
* To understand how Stacks work.  A Stack is a FILO(first-in-last-out)
* Use a debugger to step through this program.  Watch the stacks as
* items are added to and removed from the stack.
* Need to implement 4 functions stack_new, stack_push, stack_pop,
* and stack_free
*************************************************************************/

#include <stdio.h>
#include <stdlib.h>


/************************************************************************
* function prototyping
*************************************************************************/
int stack_new(
	     int size,       /* I-size of a single element                 */
	     int count,      /* I-Maximum number of data elements          */
	     long *key);     /* O-Key to the newly created stack area      */

int stack_push(
	     long key,        /* I-Key to the stack to operate upon        */
	     char *data);     /* I-Pointer to data element to be saved     */

int stack_pop(
	     long key,        /* I-Key to the stack to operate upon        */
	     char *data);     /* O-Place retrieved data is to be copied to */

int stack_free(
	      long key);      /* I-Key to the stack to operate upon        */

#define RC_OK            0    /* Operation successful
*/
#define RC_OVERFLOW      1    /* Stack overflow condition encountered
*/
#define RC_UNDERFLOW     2    /* Stack underflow condition encountered
*/
#define RC_MALLOC_ERROR  3    /* Memory allocation error occcurred
*/

#include "d:\advanc_c\source_c\p1.cpp"

main()
   {
   char    str[80];            /* Buffer to hold the string
*/
   int     ndx;                /* Index into the string buffer
*/
   long    number_key;         /* Identifies stack which holds numbers
*/
   long    letter_key;         /* Identifies stack which holds letters
*/
   long    work_key;           /* Stack to use for current character
*/
   char    value;              /* Value popped from the stack
*/
   int     rc;                 /* Status return code
*/

/***************************************************************************
* Allocate a stack to hold the numbers
****************************************************************************/

rc = stack_new(sizeof(char), 40, &number_key);
if(rc != RC_OK)
   {  /* Number failure */
   printf("Error %d allocating number stack\n", rc);
   exit(0);
   } /* Number failure */

/***************************************************************************
* Allocate a stack to hold the letters
****************************************************************************/

rc = stack_new(sizeof(char), 40, &letter_key);
if(rc != RC_OK)
   {  /* Letter failure */
   printf("Error %d allocating character stack\n", rc);
   exit(0);
   } /* Letter failure */

/***************************************************************************
* Process string read from the keyboard until an EOF is
* encountered or until a empty string is retrieved
****************************************************************************/
while(gets(str) !=NULL && str[0] != '\0')
   { /* String found */

/************************************************************************
   * Look at each character in the string. Seperate them into
   * numeric characters and letter characters.

*************************************************************************/
   for(ndx = 0; str[ndx] != '\0'; ndx++)
     { /* Process string  */
       if(str[ndx] >= '0' && str[ndx] <= '9')
	  work_key = number_key;   /* Save to the number stack */
       else
	  work_key = letter_key;   /* Save to the letter stack */

/********************************************************************
       * Push the character onto it's stack.

*********************************************************************/

       rc = stack_push(work_key, &str[ndx]);
       if(rc != RC_OK)
       { /* Error */
       printf("Error %d pushing %c onto stack\n", rc, str[ndx]);
       exit(0);
       }  /* Error */
     }    /* Process string  */

/*************************************************************************
  * Print the numbers from the string in reverse order. Note: the
  * print operation will empty the stack which should reset it for
  * the next string

*************************************************************************/
  printf("Numbers in reverse order are: ");
  rc = stack_pop(number_key, &value);
  while(rc == RC_OK)
     { /* Print it */
     printf("%c", value);
     rc = stack_pop(number_key, &value);
     } /* Print it  */
 if(rc == RC_UNDERFLOW)
    { /* Number print errror */
    printf("Error %d while printing numbers\n", rc);
    exit(0);
    } /* Number print error */

/*************************************************************************
  * Print the letters from the string in reverse order.  Note: the
  * print operation will empty the stack which should reset it for the
  * next string.

*************************************************************************/

  printf("\n\nLetters in reverse order are: ");
  while(stack_pop(letter_key, &value) == RC_OK)
     printf("%c", value);
  if(rc == RC_UNDERFLOW)
     { /* Number print error  */
     printf("Error %d while printing letters\n", rc);
     exit(0);
     } /* Number print error */
 printf("\n\n");    /* Give us some space between runs  */
 } /* String found  */
/*************************************************************************
  * We're done.  Don't forget to free the stacks we allocated.

*************************************************************************/

stack_free(number_key);
stack_free(letter_key);
return(0);
}  /* Main */



FUNCTIONS FILE
--------------------------

/**************************************************************************
*	File Name:	p1.cpp
*	Description:    Using Stacks
*	Author:		Arlyn K. Chesley
*	Date:  		2/16/96 started
*
*       This file contains four functions that are used in conjunction
*       with the main of stacks.cpp
*       The four functins are as follows
*       stack_new  - a function used to create new stacks
*       stack_push - used to push items onto the stack
*       stack_pop  - used to pop items off of the stack for use
*       stack_free - used to free the memory previously allocated to
stacks
*       I = an Input to the function, O = an output from the function
*************************************************************************/
int stack_new(
	     int size,       /* I-size of a single element                */
	     int count,      /* I-Maximum number of data elements         */
	     long *k)        /* O-Key to the newly created stack area     */
	     {
	     /*************************************************************
	     *  Structure definition for allocation of new stacks
	     **************************************************************/
		struct str_hdr
		  {
		  int tos;
		  int size;
		  int count;
		  char *data;
		  };

	     struct str_hdr *H;
	     H=(str_hdr*)malloc(sizeof(str_hdr));
	     H->tos = 0;
	     H->size = size;
	     H->count = count;
	     H->data =(char*)malloc(sizeof(size * count));
	     *k =(long)H;     /* pointer back to main for key */

	     if(H == NULL)
		return(RC_MALLOC_ERROR);
	     else
		return(RC_OK);
	     }

int stack_push(
	     long key,       /* I-Key to the stack to operate upon        */
	     char *data)     /* I-Pointer to data element to be saved     */
	     {
	     if(data != NULL)
		{
		(char)key = *data;
		key++;
		return RC_OK;
		}
	     else
		{
		return RC_OVERFLOW;
		}
	     }

int stack_pop(
	     long key,     /* I-Key to the stack to operate upon        */
	     char *data)     /* O-Place retrieved data is to be copied to */
	     {
	     if(data != NULL)
	      {
	      key = *data;
	      key--;
		 return(RC_OK);
	      }
	     else if(data == NULL)
		return(RC_UNDERFLOW);
	     else
		return(0);
	     }

int stack_free(
	     long key)       /* I-Key to the stack to operate upon        */
	     {
	     free((long *) key);
	     return(0);
	     }
Technical Details
Message-ID: <4gq8pl$or2@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: arlyn@airmail.net (Arlyn Chesley)
Newsgroups: comp.lang.c.moderated
Subject: Need help with Stacks
Date: 25 Feb 1996 12:12:05 -0600
Organization: customer of Internet America
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4gq8pl$or2@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Newsreader: Forte Free Agent v0.55