Code:
/* Project 2:Reversing a user-defined sentence string */
/* Group :Ady Perrott, Amie Tran, Nathan Brickner, Andrew Peaso, */
/* Yeva Adamyan Chris Terborg Dennis Freeland, Khang Pham */
/* Revision 2 - 2/24/03 */
#include<stdio.h> /*Library needed to run this program*/
#define MAXSTACK 64 /*MAXSTACK is a symbolic constant, used to define*/
typedef char StackEntry; /*the maximum length of the user-defined sentence*/
typedef struct stack
{
int top;
StackEntry entry[MAXSTACK];
}Stack;
void Push(StackEntry item, Stack *s); /*Prototypes*/
void Pop(StackEntry *item, Stack *s);
void StackRevMain();
void CreateStack(Stack *s);
void StackRevMain(void);
bool StackFull(Stack *s);
bool StackEmpty(Stack *s);
int main(void) /*Main Program, calls the StackRevMain program*/
{
printf("This is a small program made to reverse a sentence inputted by a user.\n");
printf("Please type your sentence and hit enter when done. Thank you\n\n");
StackRevMain();
printf("\n");
return(0);
}
void StackRevMain(void) /*Desc: Most important function, calls all other functions */
{ /* and uses them to compile and reassemble the sentence */
StackEntry item; /*PRE: The preliminary function calls must all be completed */
Stack stack; /*POST: Reversed sentence is displayed to user */
CreateStack(&stack);
while(!StackFull(&stack)&&(item = getchar()) != '\n')
Push(item, &stack);
while(!StackEmpty(&stack))
{
Pop(&item, &stack);
putchar(item);
}
putchar('\n');
}
void Push(StackEntry item, Stack *s) /*Desc: Function adds a letter to the array */
{ /*Pre: The stack exists and is not full */
if(StackFull(s)) /*Post: An additional item is stored on top of the stack */
printf("Full Stack");
else
s->entry[s->top++] = item;
}
void Pop(StackEntry *item, Stack *s) /*Desc: Function removes a letter from the array */
{ /*Pre: The stack exists and is not empty */
if (StackEmpty(s)) /*Post: The top of the stack is removed and stored as *s */
printf("Stack Empty");
else
*item = s->entry[--s->top];
}
bool StackEmpty(Stack *s) /*Desc: Checks to see if stack is empty */
{ /*Pre: The stack must exist and have been initialized */
return s->top <= 0; /*Post: TRUE if stack is empty, FALSE if otherwise */
}
bool StackFull(Stack *s) /*Desc: Checks to see if stack is full */
{ /*Pre: The stack must exist and have been initialized */
return s->top >= MAXSTACK; /*Post: TRUE if stack is full, FALSE if otherwise */
}
void CreateStack(Stack *s) /*Desc: Creates a new, initialized but empty stack */
{ /*Pre: None */
s->top = 0; /*Post: The stack is created and is initialized to empty */
}