Question
SAMPLE PROGRAM DIALOGUE: Easy C Problem (STACK2 is located below) Just add the three member functions to the already completed STACK2. Once complete, take screenshot
SAMPLE PROGRAM DIALOGUE:
Easy C Problem (STACK2 is located below)
Just add the three member functions to the already completed STACK2. Once complete, take screenshot of the compiled output (which should look like or similar to the Sample Program Dialogue).
(1) Add OutputSTACK() as a new member function to the STACK2 abstract data type.
//--------------------------------------------------
void OutputSTACK(const STACK *stack,FILE *OUT)
//--------------------------------------------------
{
fprintf(OUT,"[%d:%d] ",GetSizeSTACK(stack),GetCapacitySTACK(stack));
if ( IsEmptySTACK(stack) )
printf("(empty)");
else
{
int offset;
int UB = GetSizeSTACK(stack)-1;
for (offset = 0; offset
fprintf(OUT,"%d%c",PeekSTACK(stack,offset),((offset == UB) ? '.' : ' '));
}
}
(2) Add CopySTACK() as a new member function to STACK2.
//--------------------------------------------------
void CopySTACK(STACK *LHS,const STACK *RHS)
//--------------------------------------------------
{
if ( LHS != RHS )
{
int offset;
DestructSTACK(LHS);
ConstructSTACK(LHS,RHS->capacity);
Student provides missing code to traverse *RHS, PeekSTACK()-ing all elements
contained in *RHS, PushSTACK()-ing each element, in turn, on to *LHS.
}
}
(3) Add EQSTACK() as a new member function to STACK2.
//--------------------------------------------------
bool EQSTACK(const STACK *LHS,const STACK *RHS)
//--------------------------------------------------
{
bool r;
int offset;
if ( LHS->size != RHS->size ) return( false );
Student provides missing code to traverse *LHS and *RHS, PeekSTACK()-ing
corresponding elements to ensure every pair of corresponding elements are
equal; that is, compute r with the following universal quantified proposition
r "offset [0,size-1] ( PeekSTACK(LHS,offset) = PeekSTACK(RHS,offset) )
return( r );
}
(4) Take screenshot of compiled output
//--------------------------------------------------
// Dr. Art Hanna
// STACK2.c
//--------------------------------------------------
#include
#include
#include
#include ".\Stack2.h"
#include "..\ADTExceptions.h"
//--------------------------------------------------
void ConstructSTACK(STACK *stack,const int capacity)
//--------------------------------------------------
{
if ( capacity
stack->size = 0;
stack->capacity = capacity;
stack->elements = (int *) malloc(sizeof(int)*capacity);
if ( stack->elements == NULL ) RaiseADTException(MALLOC_ERROR);
}
//--------------------------------------------------
void DestructSTACK(STACK *stack)
//--------------------------------------------------
{
free(stack->elements);
}
//--------------------------------------------------
void PushSTACK(STACK *stack,const int element)
//--------------------------------------------------
{
if ( IsFullSTACK(stack) ) RaiseADTException(STACK_OVERFLOW);
stack->elements[stack->size] = element;
++stack->size;
}
//--------------------------------------------------
void PopSTACK(STACK *stack)
//--------------------------------------------------
{
if ( IsEmptySTACK(stack) ) RaiseADTException(STACK_UNDERFLOW);
stack->size--;
}
//--------------------------------------------------
int PeekSTACK(const STACK *stack,const int offset)
//--------------------------------------------------
{
if ( !((0
return( stack->elements[stack->size-(offset+1)] );
}
//--------------------------------------------------
int GetSizeSTACK(const STACK *stack)
//--------------------------------------------------
{
return( stack->size );
}
//--------------------------------------------------
int GetCapacitySTACK(const STACK *stack)
//--------------------------------------------------
{
return( stack->capacity );
}
//--------------------------------------------------
bool IsFullSTACK(const STACK *stack)
//--------------------------------------------------
{
return( (stack->size == stack->capacity) ? true : false );
}
//--------------------------------------------------
bool IsEmptySTACK(const STACK *stack)
//--------------------------------------------------
{
return( stack->size == 0 );
}
//--------------------------------------------------
int PeekSTACK2(const STACK *stack,const int offset,bool *exception)
//--------------------------------------------------
{
if ( !((0
{
*exception = true;
return( 0 );
}
*exception = false;
return( stack->elements[stack->size-(offset+1)] );
}
//--------------------------------------------------
int PeekSTACK3(const STACK *stack,const int offset)
//--------------------------------------------------
{
if ( !((0
{
fprintf(stderr,"\a Exception \"%s\" ",STACK_OFFSET_ERROR);
system("PAUSE");
exit( 1 );
}
return( stack->elements[stack->size-(offset+1)] );
}
//----------------------------------------------------
// Dr. Art Hanna
// STACK2.h
//----------------------------------------------------
#ifndef STACK2_H
#define STACK2_H
#include
//==============================================================
// Data model definitions
//==============================================================
typedef struct STACK
{
int size;
int capacity;
int *elements;
} STACK;
//==============================================================
// Public member function prototypes
//==============================================================
void ConstructSTACK(STACK *stack,const int capacity);
void DestructSTACK(STACK *stack);
void PushSTACK(STACK *stack,const int element);
void PopSTACK(STACK *stack);
int PeekSTACK(const STACK *stack,const int offset);
int GetSizeSTACK(const STACK *stack);
int GetCapacitySTACK(const STACK *stack);
bool IsFullSTACK(const STACK *stack);
bool IsEmptySTACK(const STACK *stack);
int PeekSTACK2(const STACK *stack,const int offset,bool *exception);
int PeekSTACK3(const STACK *stack,const int offset);
//==============================================================
// Private utility member function prototypes
//==============================================================
// (none)
#endif
//--------------------------------------------------------------
// Dr. Art Hanna
// ADTExceptions.h
//--------------------------------------------------------------
#ifndef ADTEXCEPTIONS_H
#define ADTEXCEPTIONS_H
// ADT exception definitions
#define MALLOC_ERROR "malloc() error"
#define DATE_ERROR "DATE error"
#define STACK_CAPACITY_ERROR "STACK capacity error"
#define STACK_UNDERFLOW "STACK underflow"
#define STACK_OVERFLOW "STACK overflow"
#define STACK_OFFSET_ERROR "STACK offset error"
// ADT exception-handler prototype
void RaiseADTException(char exception[]);
#endif
//--------------------------------------------------------------
// Dr. Art Hanna
// ADTExceptions.c
//--------------------------------------------------------------
#include
#include
#include
#include ".\ADTExceptions.h"
//--------------------------------------------------------------
void RaiseADTException(char exception[])
//--------------------------------------------------------------
{
fprintf(stderr,"\a Exception \"%s\" ",exception);
system("PAUSE");
exit( 1 );
}
//--------------------------------------------------------------
// Dr. Art Hanna
// STACK ADT Problem #3
// Problem3.c
//--------------------------------------------------------------
#include
#include
#include
#include ".\STACK2.h"
#include "..\ADTExceptions.h"
//--------------------------------------------------------------
int main()
//--------------------------------------------------------------
{
int capacity;
STACK stack1,stack2;
int i,pushN;
printf("capacity? "); scanf("%d",&capacity);
printf("pushN? "); scanf("%d",&pushN);
ConstructSTACK(&stack1,capacity);
ConstructSTACK(&stack2,capacity+3);
printf(" After constructing stack1 and stack2 ");
printf("stack1 = "); OutputSTACK(&stack1,stdout); printf(" ");
printf("stack2 = "); OutputSTACK(&stack2,stdout); printf(" ");
for (i = 1; i
PushSTACK(&stack1,i);
printf(" After pushing %d-to-%d on stack1 ",1,pushN);
printf("stack1 = "); OutputSTACK(&stack1,stdout); printf(" ");
CopySTACK(&stack2,&stack1);
printf(" After copying stack1 to stack2 ");
printf("stack1 = "); OutputSTACK(&stack1,stdout); printf(" ");
printf("stack2 = "); OutputSTACK(&stack2,stdout); printf(" ");
printf("stack1 %s stack2 ",EQSTACK(&stack1,&stack2) ? "==" : "!=");
PushSTACK(&stack1,pushN+1);
printf(" After pushing %d on stack1 ",pushN+1);
printf("stack1 = "); OutputSTACK(&stack1,stdout); printf(" ");
PushSTACK(&stack2,pushN+2);
printf("After pushing %d on stack2 ",pushN+2);
printf("stack2 = "); OutputSTACK(&stack2,stdout); printf(" ");
printf("stack1 %s stack2 ",EQSTACK(&stack1,&stack2) ? "==" : "!=");
printf(" After popping %d off and ",PeekSTACK(&stack2,0));
PopSTACK(&stack2);
PushSTACK(&stack2,pushN+1);
PushSTACK(&stack2,pushN+2);
printf("pushing %d, %d on stack2 ",pushN+1,pushN+2);
printf("stack1 = "); OutputSTACK(&stack1,stdout); printf(" ");
printf("stack2 = "); OutputSTACK(&stack2,stdout); printf(" ");
printf("stack1 %s stack2 ",EQSTACK(&stack1,&stack2) ? "==" : "!=");
printf(" ");
DestructSTACK(&stack1);
DestructSTACK(&stack2);
system("PAUSE");
return( 0 );
}
EACOURSESCS13111Code ADTs STACKProblem3.exe capacity? ? pushN? 5 After constructing stack1 and stack2 stack!-[0-7 empty) stack2 = [0:10] empty) After pushing 1-to-5 on stack1 stack! [5:7] 5 4 3 2 1. After copying stack1 to stack2 stacki5:71 5 4 3 2 1. stack2-5:71 5 4 3 2 1. stack! =_ stack2 After pushing 6 on stack1 stacki6:71654 3 2 1. After pushing? on stack2 stack2-[6:717 5 4 3 2 1. stack1stack2 After popping? off and pushing 6. 7 on stack2 stacki[6:71 6 5 4 3 2 1. stack2[7:716 5 4 3 2 1. stack! ! stack2 Press any key to continue .. _ EACOURSESCS13111Code ADTs STACKProblem3.exe capacity? ? pushN? 5 After constructing stack1 and stack2 stack!-[0-7 empty) stack2 = [0:10] empty) After pushing 1-to-5 on stack1 stack! [5:7] 5 4 3 2 1. After copying stack1 to stack2 stacki5:71 5 4 3 2 1. stack2-5:71 5 4 3 2 1. stack! =_ stack2 After pushing 6 on stack1 stacki6:71654 3 2 1. After pushing? on stack2 stack2-[6:717 5 4 3 2 1. stack1stack2 After popping? off and pushing 6. 7 on stack2 stacki[6:71 6 5 4 3 2 1. stack2[7:716 5 4 3 2 1. stack! ! stack2 Press any key to continue .. _Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started