Already have an account? Get multiple benefits of using own account!
Login in your account..!
Remember me
Don't have an account? Create your account in less than a minutes,
Forgot password? how can I recover my password now!
Enter right registered email to receive password!
Aim: To implement a program to handle stack overflow, underflow and odd number exception using Exception Handling.
Code:
#include
#define MAX 3
class stack
{
public:
class full;
class empty;
class oddinput;
private:
int stk[MAX];
int top;
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!";
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
char f_origin[40];
int f_val;
full(char o[40], int v)
strcpy(f_origin,o);
f_val=v;
};
class empty
char e_origin[40];
int e_val;
empty(char o[40], int v)
strcpy(e_origin,o);
e_val=v;
class oddinput
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: "<
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
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
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
Output:
Exception: Odd Input class
Added 8
Added 14
Exception: Odd Input!
Input was: 5Press any key to continue
Exception: Full class
Exception: Stack Overflow!
Input was: 14Press any key to continue
Exception : Empty class
Element popped: 14
Element popped: 8
Exception: Stack Empty!
Input was: -1Press any key to continue
Program of file data: FileData::FileData( ffblk& blk ) : fileName( blk.ff_name ), fileDate( (blk.ff_fdate >> 5) & 0x000F, blk.ff_fdate & 0x0001F
A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2 where + denotes string concatenation. For a string of t
programing code on board coloring in clanguage
You are required to give C++ implementation of following classes: 1. PropertyCategories 2. Plot 3. Flat 4. House Following is the description of all data member and member funct
Write a program called Series that will print/display a series of numbers starting with 1 and ending with the last number that is less than 30. The series should include only odd n
THIS PROGRAM IS TO DISPLAY A STOCK CONTAINING ITEM CODE,ITEM NAME,PRICE AND ANOTHER STOCK WITH CODE & QUANTITY AND DISPLAY COMPLETE INFORMATION #include #include #include
C program to print fibonacci series: int fibo(long int); void main() { long int a=0,n; printf ("how many terms\n");
: Write a program that prompts the user to enter five digit positive numbers. The program then outputs the digits of the number one digit per line. Eg if the user enters 32456, th
Pebble Merchant Problem Description There is a pebble merchant. He sells the pebbles, that are used for shining the floor. His main duty is to take the length of the room’s sides
write c program to do the following : -fill 2 dimensional array (square matrix array with size=4). -ask the user to enter any integer number and add this number to the diagonals -o
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!
whatsapp: +1-415-670-9521
Phone: +1-415-670-9521
Email: [email protected]
All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd