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

Summations, How do you write the code for summations

How do you write the code for summations

C program to swap the two values, Program is to swap the two values which a...

Program is to swap the two values which are enter by user: Program is to swap the two values which are enter by user through the function with values changed using pointers

When should i use references, A: Use references when you can use, and use p...

A: Use references when you can use, and use pointers when you have to. References are generally preferred over pointers whenever you don't require "reseating". Usually this mean

Friend funtion.., Is friend function can be harmfull to make a secure progr...

Is friend function can be harmfull to make a secure program/sofware?

Car rental project, I need a project on car rental system using c programmi...

I need a project on car rental system using c programming only of college level

Beginning C++ Through Game Progammin, you are to create a text adventure ga...

you are to create a text adventure game that uses pointers. You have a rich, eccentric Uncle Billy who is soon to be deceased. How soon.....oops......he''s gone. He has left yo

Write a program to illustrate array with strings, Write a Program to illust...

Write a Program to illustrate Array with Strings? main() { static char name[]="devdas"; int i; i=0; while(name[i]!='\0') { printf("%c",name[i]); i=i+1; } } In the

Wap to print any name on screen 10 times, WAP TO PRINT ANY NAME ON SCREEN 1...

WAP TO PRINT ANY NAME ON SCREEN 10 TIMES void main () { int a=1; clrscr(); do { printf ("expertsmind\n"); a++; } while (a getch (); }

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