C program to handle stack using exception handling, C/C++ Programming

Assignment Help:

Aim: To implement a program to handle stack overflow, underflow and odd number exception using Exception Handling.

Code:

#include

#include

#include

#define MAX 3

 

class stack

{

                  public:

                  class full;

                  class empty;

                  class oddinput;

                  private:

                  int stk[MAX];

                  int top;

                  public:

                  stack()

{

                  top=-1;

}

void push(int item)

{

if(item%2!=0)

{

char o[40]="Odd Input!";

int n=item;

throw oddinput(o,n);

}

if(top>=MAX-1)

{

char o[40]="Stack Overflow!";

int n=item;

throw full(o,n);

}

stk[top]=item;

top++;

}

int pop()

{

if(top<0)

{

char o[40]="Stack Empty!";

int n=top;

throw empty(o,n);

}

int i=stk[top];

top--;

return i;

}

class full

{

public:

char f_origin[40];

int f_val;

full(char o[40], int v)

{

strcpy(f_origin,o);

f_val=v;

}

};

class empty

{

public:

char e_origin[40];

int e_val;

empty(char o[40], int v)

{

strcpy(e_origin,o);

e_val=v;

}

};

class oddinput

{

public:

char o_origin[40];

int o_val;

oddinput(char o[40],int v)

{

strcpy(o_origin,o);

o_val=v;

                  }

};

};

 

void main()

{

stack s1;

int i;

try

{

s1.push(8);

cout<<"\nAdded 8";

s1.push(14);

cout<<"\nAdded 14";

s1.push(5);

cout<<"\nAdded 5";                                    //This will not print

i=s1.pop();

cout<<"\nElement popped: "<

i=s1.pop();

cout<<"\nElement popped: "<

i=s1.pop();

cout<<"\nElement popped: "<

i=s1.pop();

cout<<"\nElement popped: "<

i=s1.pop();

cout<<"\nElement popped: "<

}

catch(stack::oddinput oi)

{

cout<<"\nException: "<

}

catch(stack::full f)

{

cout<<"\nException: "<

}

catch(stack::empty e)

{

cout<<"\nException: "<

}

}                     

Output:

Exception: Odd Input class

Added 8

Added 14

Exception: Odd Input!

 Input was: 5Press any key to continue

 

Exception: Full class

Added 8

Added 8

Added 8

Exception: Stack Overflow!

Input was: 14Press any key to continue

 

Exception : Empty class

Added 8

Added 8

Added 14

Element popped: 14

Element popped: 8

Element popped: 8

Exception: Stack Empty!

Input was: -1Press any key to continue


Related Discussions:- C program to handle stack using exception handling

program generates cards at random, #include #include #include ...

#include #include #include #include #include //*Variables Used in Programs*// int k; int l; int d; int won; int loss; int cash = 500;

Sp, Write a program to find the area under the curve y = f(x) between x = a...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

How many ways are there to initialize an int with a constant, There are two...

There are two ways for initializes in C++ as shown in the example that follows. The first way uses the traditional C notation. The second way uses constructor notation. int foo

How does c++ help with the tradeoff of security vs.usability, A: In C, enca...

A: In C, encapsulation was completed by making things static in a compilation unit or module. It prevented another module from accessing the static stuff. (Incidentally, now static

Change to palindrome, A palindrome is a string that reads the same from bot...

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

Type compatibility, Define  T y pe c o m p a t i b i li t y? ...

Define  T y pe c o m p a t i b i li t y? T o a s s i g n i n t t o s m a l l i n t t h e v a r i a b l e should b e

Memory management system, 1. Basic Heap: Each memory location in our model ...

1. Basic Heap: Each memory location in our model of the RAM will be an instance of type Memory: 2. typedef union Memory_u Memory; 3. union Memory_u{ 4. char character;

Cipher: Decrypt and Encrypt, You must write a program that can both decrypt...

You must write a program that can both decrypt and encrypt a single word that is entered by the user. The initial choice of encryption and decryption is left up to the user. Addi

Decode the Code, Smugglers are becoming very smart day by day. Now they hav...

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd