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

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

Posted Date: 9/29/2012 4:18:42 AM | Location : United States







Related Discussions:- C program to handle stack using exception handling, Assignment Help, Ask Question on C program to handle stack using exception handling, Get Answer, Expert's Help, C program to handle stack using exception handling Discussions

Write discussion on C program to handle stack using exception handling
Your posts are moderated
Related Questions
string will contain only lower case alphabet& the ascii value starts from 1-26,(ie,a=1&z=26).it perform the operation like the following costs:add=2units,replace=1unit,delete=3unit

Create the following program or propose one of your own. The program must include at least one of each of the following. Decision structure Repetitive structure fu

Project Description: collect / modify person records / photo images/ lookup / facebook iphone and ipad App basic function : 1 user will configure facebook connection

#question. write a program number to alphabet in c++..

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

Hiding overloaded functions We cannot overload a base class function by redefining it in a derived class with a dissimilar argument list. Consider examples to see if similar fu

Question 1. Explain Briefly about class and objects. 2. Create a class string which stores a string value. Overload ++ operator which converts the characters of the string t

B-tree: A B-tree is an also called balanced m-way tree. A node of the tree may have many records or key and pointers to children. It is also called as the balanced sort tree. It s

The following is the formula that can be used to complete that calculation: Monthly Payment = Monthly Interest Rate / (1 - (1 + Monthly Interest Rate) -Payment Interval )) *

Implement, in rPeANUt, the a Fibonacci function by using the simple recursive approach. The Fibonacci function can be implemented in c as follow: int fib(int x) {   if (x