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

Explain difference between early binding and late binding, What is the diff...

What is the difference between early binding and late binding? What are advantages of early binding? a.) Late binding refers to function calls which aren't resolved until run t

Application for build a toy program, With this assignment you will build a ...

With this assignment you will build a toy program that manipulates pointers to integers. You will develop the same main program fragment in both C and Assembler. Thus, you'll get t

Flowchart, questiCreate a flowchart that displays the student''''s average ...

questiCreate a flowchart that displays the student''''s average score for three quizzes. + Assume that there are 3 sections each having 5 students + The only valid number to be en

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

Quick sort , Hi, I need quick sort program without using recursion

Hi, I need quick sort program without using recursion

What are the debugging methods you employ while came across , What are the ...

What are the debugging methods you employ while came across a problem? A: Debugging with tools such as: 1.      DBG, GDB ,Forte, Visual Studio. 2.      Using tusc to trace

Pos console cpp unit testing, Pos Console CPP Unit Testing Project Descr...

Pos Console CPP Unit Testing Project Description: Do the CPPUnit Testing for the project. Have all the functions done. Skills required are Android, C Programming, C++ Prog

Program for swapping the two integers, Program for Swapping the Two Integer...

Program for Swapping the Two Integers                 For example, Swapping the two value                                 int x=4,y=6,t;                                 t

Is probable to encompass virtual constructor? if yes, Is it probable to enc...

Is it probable to encompass Virtual Constructor? If yes, how? If not, Why?

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