/* Sandra N. Kardaras-Flick 03/16/05 STACKS A program which allows the user to input integers & a string and returns the integers & string reversed. */ #include #include #include #define STACK_SIZE 50 #define TRUE 1 #define FALSE 0 /* define a new type, boolean, as a synonym for int */ typedef int boolean; /* function declarations */ void makeEmptyStack(void); /* function to make the stack empty */ boolean stackIsEmpty(void); /* function returns treu if stack is empty */ boolean stackIsFull(void); /* function returns treu if stack is full */ void push(int i); /* push the integer i onto the stack */ int pop(void); /* pop off the top integer from the stack */ void stackOverflow(void); /* error when you try to remove from empty stack */ void stackUnderflow(void); /* error whey you try to add to a full stack */ /* array of integers that stores the stack */ /* we add and remove from the end (top) of the stack */ int contentsOfStack[STACK_SIZE]; /* pointer to the top of the stack -- this starts off at 0 */ /* and goes up each time we add something to the stack (a.k.a. push) */ /* and goes down each time we remove something from the stack (a.k.a. pop */ int *top_ptr = &contentsOfStack[0]; int main() { int temp = 0; /* int to store each character of input */ /*****************************************/ /* step 1 - get numbers and reverse them */ /*****************************************/ makeEmptyStack(); /* set the stack empty */ printf("\n\n\n\n\t Numeric entry:\t"); /* loop getting characters until the newline is reached */ /* each character read is stored in temp */ while ((temp = getchar()) != '\n') { /* if the character is a digit '0' through '9' we'll add it to */ /* the stack -- other characters are ignored since isdigit() */ /* will return false */ if (isdigit(temp)) { /* convert the digit to a number by subtracting '0' from it */ temp = temp - '0'; /* push the number now in temp onto the stack */ push(temp); } } printf(" Numeric entry reversed:\t"); /* now go thorugh the stack popping off one digit at a time until it */ /* is empty. Since pop takes from the end of the stack, the first value */ /* popped will be the last one pushed, and so on - so the order reverses */ while (!stackIsEmpty()) /* when stack is empty the loop will exit */ { /* pop and int off the stack and print it to the screen */ printf("%d", pop()); } printf("\n\n\n\n"); /***********************************************/ /* step 2 - get characters and reverse them */ /* the only difference is how the input is */ /* altered before storage, and the printf used */ /***********************************************/ makeEmptyStack(); /* set the stack empty */ printf("\n\n\n\n\t Character entry:\t"); /* loop getting characters until end of line is reached */ /* each character read is stored in temp */ while ((temp = getchar()) != '\n') { /* we don't call isdigit since any character is okay */ /* and we don't subtract '0' since they are not digits */ /* push the character in temp onto the stack */ push(temp); } printf("Character entry reversed:\t"); /* now go thorugh the stack popping off one char at a time until it */ /* is empty. Since pop takes from the end of the stack, the first value */ /* popped will be the last one pushed, and so on - so the order reverses */ while (!stackIsEmpty()) /* when stack is empty the loop will exit */ { /* pop and int off the stack and print it to the screen */ /* we use "%c" instead of "%d" since this is a character */ /* not a number */ printf("%c", pop()); } printf("\n\n\n\n"); return 0; } /* make the stack empty. We do this by setting the top pointer */ /* to point to the 0th element of the array. */ /* since the top pointer always says where to put the next */ /* element, if it points to the start of the array that means */ /* the stack is empty */ void makeEmptyStack(void) { top_ptr = &contentsOfStack[0]; } /* check if the stack is empty. We do this by seeing if the top */ /* pointer points to the 0th element of the array. */ /* since the top pointer always says where to put the next */ /* element, if it points to the start of the array that means */ /* the array is empty */ boolean stackIsEmpty(void) { return top_ptr == &contentsOfStack[0]; } /* check if the stack is full. We do this by seeing if the top */ /* pointer points to the element beyond the end of the array. */ /* since the top pointer always says where to put the next */ /* element, if it points to the element after the end of the */ /* array that means the array if full */ boolean stackIsFull(void) { return top_ptr == &contentsOfStack[STACK_SIZE]; } /* add int i to the end of the array. If the array is full this */ /* produces a stack overflow error. If the array is not full then */ /* we add i to the array at the point pointed to by top_ptr, and */ /* then we increast top_ptr by one */ void push(int i) { if (stackIsFull()) /* can't add more to a full stack */ stackOverflow(); /* print overflow message and exit */ else *top_ptr++ = i; /* add i to the array, then increase top_ptr */ } /* we take the top int off the stack and return it. If the stack is */ /* empty then this is a stack underflow error. If the stack is not */ /* emtpy we decrease top_ptr by 1 and then return the int at *top_ptr */ int pop(void) { if (!stackIsEmpty()) /* can't pop from an empty stack */ return *--top_ptr; /* return the int at *top_ptr, and decrment top_ptr */ else stackUnderflow(); /* print error message and exit */ return 0; } /* print stack overflow error message and exit the program */ void stackOverflow(void) { printf("\nStack Overflow\n"); exit(EXIT_FAILURE); } /* print stack underflow error message and exit the program */ void stackUnderflow(void) { printf("\nStack Underflow\n"); exit(EXIT_FAILURE); }