Question: VERY EASY C PROGRAM. Will positively rate. Problem There are two strategies for flow-of-control continuation after a throw-ed exception is handled (1) flow-of-control does not

VERY EASY C PROGRAM. Will positively rate.

Problem

There are two strategies for flow-of-control continuation after a throw-ed exception is handled (1) flow-of-control does not return to the throw point (that is, the point in the source code that diagnosed the exception and then throw-ed the exception) is called the termination model of exception handling; and (2) flow-of-control does return to the throw point (actually flow-of-control resumes just after the throw point) is called the resumption model of exception handling. We can choose to implement either strategy since C provides absolutely no direct language support (that is, syntax-and-semantics) for exception handling; we have to roll our own! Fortunately, C++ and Java do provide significant language support for the termination model of exception handling; therefore, we will choose to implement ADT exception handling using the termination model.

(1) Build the load module Problem2.exe using a project with the five (already fully completed) source files found at the bottom of this page. (1) ADTExceptions.h; (2) ADTExceptions.c; (3) Stack2.h; (4) Stack2.c; and (5) Problem2.c. . Notice that there are three versions of the PeekSTACK() member function in STACK2PeekSTACK(), PeekSTACK2(), and PeekSTACK3()and that the STACK member functions ConstructSTACK(), PushSTACK(), PopSTACK(), and PeekSTACK() have been modified to incorporate exception handling. Exception handling requires the two new source files ADTExceptions.h and ADTExceptions.c be added to every ADT project that is developed that uses exception handling.

(2) Play with Problem2.exe using Method (1) of exception handling until you understand how and why it works; modify Problem2.c to use Method (2) exception handling by turning-off Method (1) and turning-on Method (2); and modify Problem2.c again to use Method (3) by turning-off Method (2) and turning-on Method (3). Study the code in the client application (that is, Problem2.exe) and the code in the STACK2 abstract data type implementation (namely, STACK2.h and STACK2.c). Which exception-handling method (approach) to exception handling do you think is best?

TAKE SCREENSHOTS OF COMPILED RESULTS

//--------------------------------------------------------------

// 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

//--------------------------------------------------------------

// ADTExceptions.c

//--------------------------------------------------------------

#include

#include

#include

#include ".\ADTExceptions.h"

//--------------------------------------------------------------

void RaiseADTException(char exception[])

//--------------------------------------------------------------

{

fprintf(stderr,"\a Exception \"%s\" ",exception);

system("PAUSE");

exit( 1 );

}

//----------------------------------------------------

// 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

//--------------------------------------------------

// STACK2.c

//--------------------------------------------------

#include

#include

#include

#include ".\Stack2.h"

#include "..\ADTExceptions.h"

//--------------------------------------------------

void ConstructSTACK(STACK *stack,const int capacity)

//--------------------------------------------------

{

if ( capacity <= 0 ) RaiseADTException(STACK_CAPACITY_ERROR);

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 <= offset) && (offset <= GetSizeSTACK(stack)-1)) ) RaiseADTException(STACK_OFFSET_ERROR);

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 <= offset) && (offset <= GetSizeSTACK(stack)-1)) )

{

*exception = true;

return( 0 );

}

*exception = false;

return( stack->elements[stack->size-(offset+1)] );

}

//--------------------------------------------------

int PeekSTACK3(const STACK *stack,const int offset)

//--------------------------------------------------

{

if ( !((0 <= offset) && (offset <= GetSizeSTACK(stack)-1)) )

{

fprintf(stderr,"\a Exception \"%s\" ",STACK_OFFSET_ERROR);

system("PAUSE");

exit( 1 );

}

return( stack->elements[stack->size-(offset+1)] );

}

//--------------------------------------------------------------

// STACK ADT Problem #2

// Problem2.c

//--------------------------------------------------------------

#include

#include

#include

#include ".\STACK2.h"

#include "..\ADTExceptions.h"

//--------------------------------------------------------------

int main()

//--------------------------------------------------------------

{

int capacity;

printf("capacity? ");

while ( scanf("%d",&capacity) != EOF )

{

STACK stack;

int i,pushN,popN,peekN;

ConstructSTACK(&stack,capacity);

printf("pushN? "); scanf("%d",&pushN);

printf("peekN? "); scanf("%d",&peekN);

printf(" popN? "); scanf("%d",&popN);

for (i = 1; i <= pushN; i++)

PushSTACK(&stack,i);

/*

Method (1) Allow ADT to RaiseADTException() which provides a uniform

exception-handling approach for *ALL* ADT

*/

for (i = 1; i <= peekN; i++)

printf("%d ",PeekSTACK(&stack,i-1));

printf(" ");

/*

Method (2) ADT returns exception status (true or false) then client code

checks status after *EVERY* ADT exception-producing operation call

for (i = 1; i <= peekN; i++)

{

bool exception;

int x = PeekSTACK2(&stack,i-1,&exception);

if ( exception )

{

fprintf(stderr,"\a Exception \"%s\" ",STACK_OFFSET_ERROR);

system("PAUSE");

exit( 1 );

}

printf("% d",x);

}

printf(" ");

*/

/*

Method (3) ADT handles the exception (identical to Method (1)

from clients point-of-view)

for (i = 1; i <= peekN; i++)

printf("%d ",PeekSTACK3(&stack,i-1));

printf(" ");

*/

for (i = 1; i <= popN; i++)

PopSTACK(&stack);

DestructSTACK(&stack);

printf(" capacity? ");

}

system("PAUSE");

return( 0 );

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!