Pages

lab programs in c++


  OOPS  labexercise  for  the practical   2010  in  adhipasakthi engg college 




*************************************************************************************
EX: NO: 1[A] CLASS WITH STATIC DATA MEMBER
DATE: 15.7.10
*************************************************************************************
#include<iostream.h>
#include<conio.h>
class item
{
static int count;
int num;
public:
void getdata(int a)
{
num=a;
count++;
cout<<"\nNumber"<<num;
}
void showcount()
{
cout<<"\ncount"<<count;
}
}a,b,c;
int item::count;
int main()
{
clrscr();
a.showcount();
b.showcount();
c.showcount();
a.getdata(20);
b.getdata(30);
c.getdata(40);
a.showcount();
b.showcount();
c.showcount();
getch();
return 0;
}












************************************************************************************* OUTPUT FOR CLASS WITH STATIC DATA MEMBER
*************************************************************************************
count0
count0
count0
Number20
Number30
Number40
count3
count3
count3






























*************************************************************************************
EXP.NO:1[B] CLASS WITH STATIC MEMBER FUNCTION
DATE: 15.7.10
************************************************************************************
#include<iostream.h>
#include<conio.h>
class test
{
int code;
static int count;
public:
void getcode(void)
{
code=++count;
}
void showcode(void)
{
cout<<"OBJECT NUMBER:"<<code<<"\n";
}
static void showcount(void)
{
cout<<"COUNT:"<<count<<"\n";
}
};
int test::count;
int main()
{
test t1,t2;
clrscr();
t1.getcode();
t2.getcode();
test::showcount();
test t3;
t3.getcode();
test::showcount();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
return(0);
}



******************************************************************************
OUTPUT CLASS WITH STATIC MEMBER FUNCTION
******************************************************************************

COUNT:2
COUNT:3
COUNT:3
OBJECT NUMBER:1
OBJECT NUMBER:2
OBJECT NUMBER:3





































*************************************************************************************
EXP.NO:1[C] FUNCTION WITH DEFAULT ARGUMENT
DATE: 15.7.10
*************************************************************************************
#include<iostream.h>
#include<conio.h>
class cube
{
int x,y,z;
public:
cube(int i=3,int j=7,int k=5)
{
x=i;
y=j;
z=k;
}
int volume()
{
return x*y*z;
}
};
int main()
{
cube a(2,3,4),b;
clrscr();
cout<<"\t\t out put for the default arguments";
cout<<"\n cube value of object A="<<a.volume()<<"\n";
cout<<"cube value of object B="<<b.volume();
getch();
return(0);
}















*****************************************************************************
OUTPUT FUNCTION WITH DEFAULT ARGUMENT
******************************************************************************
cube value of object A=24
cube value of object B=105








































*************************************************************************************
EXP.NO: I[D] CLASS WITH FRIEND FUNCTION
DATE: 22.7.10
*************************************************************************************
#include<iostream.h>
#include<conio.h>
class distance1;
class distance
{
int dist;
public:
distance()
{
dist=10;
}
void showdist()
{
cout<<dist;}
friend distanceadd (distance,distance1);};
class distance1
{ int dist1;
public:
distance1()
{ dist1=20;}
void showdist1()
{cout<<dist1;}
friend distanceadd (distance,distance1);};
int distanceadd(distance a,distance1 b)
{return (a.dist+b.dist1);}
void main()
{
distance d;
clrscr();
distance1 d1;
cout<<"Distance of the class 1 object= ";
d.showdist();
cout<<"\n Distance of the class 2 object= ";
d1.showdist1();
cout<<"\n Added distance is: "<<distanceadd(d,d1);
getch();

******************************************************************************
OUTPUT FOR CLASS WITH FRIEND FUNCTION
******************************************************************************


Distance of the class 1 object= 10
Distance of the class 2 object= 20
Added distance is: 30








































*************************************************************************************
EXP.NO:2[A] IMPLEMENTIOPN OF COMPLEX NUMBER WITH OPREATOR OVERLOADING
DATE: 22.7.10
*************************************************************************************
#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:
complex()
{
}
complex(float a)
{
x=y=a; }
complex(float a, float b)
{ x=a;
y=b;}
complex sum(complex,complex);
void show(complex); };
complex complex::sum(complex c1,complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return c3;}
void complex::show(complex c)
{
cout<<c.x<<"+j"<<c.y<<"\n";}
void main()
{
complex();
clrscr();
complex A(2.3,3.5);
complex B(1.6);
complex c;
complex d;
c=d.sum(A,B);
cout<<"\n A=";
d.show(A);
cout<<"\n B=";
d.show(B);
cout<<"\n c=";
d.show(c);
getch();
}




































*************************************************************************************
OUTPUT FOR IMPLEMENTIOPN OF COMPLEX NUMBER WITH OPREATOR OVERLOADING
*************************************************************************************

A=2.3+j3.5

B=1.6+j1.6

c=3.9+j5.1
































******************************************************************
EXP.NO:2 [B] IMPLEMENTATIONCOMPLEX NUMBER WITH OPREATOROVERLOADING
DATE: 22.7.10
*************************************************************************************
#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:
complex()
{
}
complex(float a) {
x=y=a; }
complex (float a, float b){
x=a;y=b;}
complex operator +(complex,complex);
void show(complex);
}B(1.6);
complex complex::operator +(complex c1,complex c2){
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return c3;}
void complex::show(complex c) {
cout<<c.x<<"+j"<<c.y<<"\n"; }
void main() {
complex();
clrscr();
complex A(2.3,3.5);
c=a+b
c=d.sum(A,B);
cout<<"\n A=";
d.show(A);
cout<<"\n B=";
d.show(B);
cout<<"\n c=";
d.show(c);
getch();
*************************************************************************************
OUTPUT FOR IMPLEMENTATIONCOMPLEX NUMBER WITH OPREATOROVERLOADING
*************************************************************************************


A=1.6+j1.6

B=2.3+j3.5

C=3.9+j5.1





































*************************************************************************************
EXP.NO:3 IMPLEMENTATION OF CLASS WITH DYNAMIC MEMORY ALLOCATION
DATE: 29.7.10
*************************************************************************************
#include<iostream.h>
#include<process.h>
#include<conio.h>
const int TRUE=1;
const int FALSE=0;
class matrix
{
private:
int row;
int col;
int **p;
public:
matrix()
{
row=col=0;
p=NULL;

}
matrix(int r,int c);
~matrix();
void read();
void show();
void add(matrix &a,matrix &b);
void sub(matrix &a,matrix &b);
};
matrix::matrix(int r,int c)
{
row=r;
col=c;
p=new int*[row];
for(int i=0;i<row;i++)
p[i]=new int[col];
}
matrix::~matrix()
{
for(int i=0;i<row;i++)
delete p[i];
delete p;
}
void matrix::add(matrix &a,matrix &b)
{
int i,j;
row=a.row;
col=b.col;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
p[i][j]=a.p[i][j]+b.p[i][j];
}
void matrix::sub(matrix &a,matrix &b)
{
int i,j;
row=a.row;
col=b.col;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
p[i][j]=a.p[i][j]-b.p[i][j];
}
void matrix::read()
{
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
cout<<"MATRIX ["<<i<<","<<j<<"]=";
cin>>p[i][j];
}
}
void matrix::show()
{
for(int i=0;i<row;i++)
{
cout<<endl;
for(int j=0;j<col;j++)
cout<<p[i][j]<<"";
}

}

void main()
{
clrscr();
int m,n,p,q;
cout<<"enter the matrix:";
cout<<"\n how many rows:";
cin>>m;
cout<<"\n how many coloumns:";
cin>>n;
matrix a(m,n);
a.read();
cout<<"\n enter the b matrix:"<<endl;
cout<<"\n how many rows:";
cin>>p;
cout<<"\n how many coloumns:";
cin>>q;
matrix b(p,q);
b.read();
cout<<"matrix A is......";
a.show();
cout<<"matrix B is .....";
b.show();
matrix c(m,n);
c.add(a,b);
cout<<endl<<"c=a+b......";
c.show();
matrix d(p,q);
d.sub(a,b);
cout<<endl<<"d=a-b.....";
d.show();
getch();
}


















*************************************************************************************
OUTPUT IMPLEMENTATION OF CLASS WITH DYNAMIC MEMORY ALLOCATION
************************************************************************************
Enter the a matrix:

How many rows:2

How many columns:2

MATRIX [0,0]=1
MATRIX [0,1]=1
MATRIX [1,0]=1
MATRIX [1,1]=1

enter the b matrix:

how many rows:2

how many coloumns:2
MATRIX [0,0]=1
MATRIX [0,1]=1
MATRIX [1,0]=1
MATRIX [1,1]=1

matrix A is......

11
11
matrix B is .....
11
11
c=a+b......22
22
d=a-b.....
00
00








*************************************************************************************
EXP.NO:4 IMPLEMENTATION OF CLASS WITH DYNAMIC MEMORY ALLOCATION
DATE: 05.8.10
*************************************************************************************

#include<iostream.h>
#include<conio.h>
const int SIZE=10;
class vector
{
private:
int*array;
public:
void *operator new(size_t)
{
vector*my_vector;
my_vector->array=new int[SIZE];
return my_vector;
}
void operator delete(void *vec)
{
vector*my_vect;
my_vect=(vector*)vec;
delete(int*)my_vect->array;
::delete vec;
}
void read();
int sum();
};
void vector::read()
{
for(int i=0;i<SIZE;i++)
{
cout<<"Vector ["<<i<<"]=";
cin>>array[i];
}
}
int vector::sum()
{
int sum=0;
for(int i=0;i<SIZE;i++)
sum=sum+array[i];
return sum;
}
void main()
{
clrscr();
vector*my_vector=new vector;
cout<<"enter vector data....."<<endl;
my_vector->read();
cout<<"sum of vector="<<my_vector->sum();
delete my_vector;
getch();
}



































*************************************************************************************
OUTPUT FOR OVERLOAD THE NEW AND DELETE OPERATOR WITH DYNAMIC MEMORY
************************************************************************************
enter vector data.....
Vector [0]=1
Vector [1]=2
Vector [2]=3
Vector [3]=4
Vector [4]=5
Vector [5]=6
Vector [6]=7
Vector [7]=8
Vector [8]=9
Vector [9]=1
sum of vector=46






























************************************************************************************
EX.NO:5 TO DEVELOPE A TEMPLATE OF LINKED LIST CLASS AND ITS METHODS
DATE:19.08.10
*************************************************************************************
#include<iostream.h>
#include<conio.h>
#include<process.h>
template<class T>
class list
{
private:
T data;
list *next;
public:
list()
{
data=0.0;
next=NULL;
}
list(float dat)
{
data=dat;
next=NULL;
}
void insert(list<T>*node)
{
list<T>*last=this;
while(last->next)
last=last->next;
last->next=node;
}
void display(list*);
};

template<class T>
void list<T>::display(list<T>*first)
{
list<T>*traverse;
for(traverse=first;traverse;traverse=traverse->next)
cout<<traverse->data<<"->";
cout<<"NULL" ;
}

void main()
{
int ch;
float data;
clrscr();
list<float>*first=NULL;
list<float>*node;
while(1)
{
cout<<endl<<"\n Linked list";
cout<<"\n 1.Insert";
cout<<"\n 2.Display";
cout<<"\n 3.Exit";
cout<<" Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:cout<<"Enter data";
cin>>data;
node=new list<float>(data);
if(first==NULL)
first=node;
else
first->insert(node);
break;
case 2:first->display(first);
break;
case 3:exit(1);
break;
default:cout<<"Enter the correct choice";
}

getch();
}
}








******************************************************************************
OUTPUT OF TO DEVELOPE A TEMPLATE OF LINKED LIST CLASS AND ITS METHODS
******************************************************************************

Linked list
1.Insert
2.Display
3.Exit
Enter your choice:1
Enter data23

Linked list
1.Insert
2.Display
3.Exit
Enter your choice:1
Enter data45

Linked list
1.Insert
2.Display
3.Exit
Enter your choice:1
Enter data98

Linked list
1.Insert
2.Display
3.Exit
Enter your choice:2
23->45->98->NULL

Linked list
1.Insert
2.Display
3.Exit
Enter your choice:3










****************************************************************
EX.NO:6(A) TO DEVELOPE A TEMPLATE OF STANDARD SORTING ALGORITHMS SUCH AS BUBBLESORT
DATE:26.08.10
****************************************************************

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
enum Boolean{false,true};
template<class T>
void swap(T &x,T &y)
{
T t;
t=x;x=y;
y=t;
}

template<class T>
void bubb(T &s,int size)
{
Boolean swapped=true;
for(int i=0;(i<size-1)&&swapped;i++)
{
swapped=false;
for(int j=0;j<(size-1)-i;j++)

if(s[j]>s[j+1])
{
swapped=true;
swap(s[j],s[j+1]);
}
}
}


void main()
{

int num[25];
float floatnum[25];
int i,size;
clrscr();
cout<<"PROGRAM TO SORT"<<endl;
cout<<"===================="<<endl;
cout<<"ENTER THE SIZE OF INTEGER:";
cin>>size;
cout<<"ENTER THE ELEMENT:"<<endl;
for(i=0;i<size;i++)
cin>>num[i];
bubb(num,size);
cout<<"SORT VECTOR:"<<endl;
for(i=0;i<size;i++)
cout<<num[i]<<" ";
cout<<"ENTER THE SIZE OF FLOAT:";
cin>>size;
cout<<"ENTER THE ELEMENT:"<<endl;
for(i=0;i<size;i++)
cin>>floatnum[i];
bubb(floatnum,size);
cout<<"SORTED VECTOR:"<<endl;
for(i=0;i<size;i++)
cout<<floatnum[i]<<" ";
getch();
}






























****************************************************************
OUTPUT FOR PROGRAM TO DEVELOP A TEMPLATE OF STANDARD SORTING ALGORITHM SUCH AS BUBBLESORT
****************************************************************


PROGRAM TO SORT
====================
ENTER THE SIZE OF INTEGER:4
ENTER THE ELEMENT:
9
1
3
8
SORT VECTOR:
1 3 8 9 ENTER THE SIZE OF FLOAT:4
ENTER THE ELEMENT:
1.9
6.5
4.4
2.3
SORTED VECTOR:
1.9 2.3 4.4 6.5
























****************************************************************
EX NO:6(B) TO DEVELOP A TEMPLATE OF STANDARD SORTING SUCH AS BUBBLESORT,MERGESORT,INSERTION SORT AND QUICK SORT
DATE:9.09.10
****************************************************************
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
template<class T>
class sort
{
private:

T a[20],size;
int i,j;
public:
void input(int);
void bubble();
void insertion();
void merge(int,int,int,int);
void merge_split(int,int);
void quick(int,int);
void swap(T a[],int,int);
void percolatedown(int);
void heap();
void view();
void menu();
};
template<class T>
void sort<T>::menu()
{
cout<<"\t1.BUBBLE SORT\n\t2.INSERTION SORT\n\t3.QUICK SORT\n\t4.MERGE SORT\n\t5.HEAP SORT\n\t6.EXIT\n";
}
template<class T>
void sort<T>::input(int no)
{
size=no;
cout<<"Enter the element:";
for(i=1;i<=size;i++)
cin>>a[i];
}
template<class T>
void sort<T>::bubble()
{
int t;
for(i=1;i<=size;i++)
{
for(j=i+1;j<=size;j++)
if ( a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
template<class T>
void sort<T>::insertion()
{
for(i=2;i<=size;i++)
{
int t=a[i];
for(j=i;j>1&&t<a[j-1];j--)
a[j]=a[j-1];
a[j]=t;
}
}
template<class T>
void sort<T>::swap(T a[],int m,int n)
{
int t;
t=a[m]=a[n];
a[n]=t;
}
template<class T>
void sort<T>::quick(int first,int last)
{
int pivot;
if(first<last)
{
i=first;j=last;pivot=a[first];
while(i<j)
{
while(pivot>=a[i]&&i<last)
i++;
while(pivot<=a[j]&&j>first)
j--;
if(i<j)
swap(a,i,j);
}
swap(a,first,j);
quick(first,j-1);
quick(j+1,last);
}
}
template<class T>
void sort<T>::merge_split(int first,int last)
{
int mid;
if(first<last)
{
mid=(first+last)/2;
merge_split(first,mid);
merge_split(mid+1,last);
merge(first,mid,mid+1,last);
}
}
template<class T>
void sort<T>::merge(int f1,int l1,int f2,int l2)
{
int b[20],k=1;
i=f1;
j=f2;
while(i<=l1&&j<=l2)
{
if(a[i]<=a[j])
b[k++]=a[i++];
else
b[k++]=a[j++];
}
while(i<=l1)
b[k++]=a[j++];
while(j<=l2)
b[k++]=a[j++];
i=f1;
j=1;
while(j<k)
a[i++]=b[j++];
}
template<class T>
void sort<T>::heap()
{
int maxitem[10];
for (int i=size/2;i>0;i--)
percolatedown(i);
cout<<"The sorted order is:";
for(i=size;i>0;i--)
{
maxitem[i]=a[1];
cout<<maxitem[i]<<" ";
a[1]=a[size--];
percolatedown(1);
}
}
template<class T>
void sort<T>::percolatedown(int hole)
{
int child;
int temp=a[hole];
for(;hole*2<=size;hole=child)
{
child=hole*2;
if(child!=size&&a[child+1]<a[child])
child++;
if(a[child]<temp)
a[hole]=a[child];
else
break;
}
a[hole]=temp;
}
template<class T>
void sort<T>::view()
{
cout<<"The sorted element:";
for(i=1;i<=size;i++)
cout<<a[i]<<" ";
}
void main()
{
clrscr();
int size,ch;
sort<int>s;
s.menu();
cout<<"Enter the number of elements:";
cin>>size;
s.input(size);
while(1)
{
cout<<"\n\nEnter the choice:\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"BUBBLE SORT\n";
s.bubble();
s.view();
break;
case 2:
cout<<"INSERION SORT\n";
s.insertion();
s.view();
break;
case 3:
cout<<"QUICK SORT\n";
s.quick(1,size);
s.view();
case 4:
cout<<"MERGE SORT\n";
s.merge_split(1,size);
s.view();
break;
case 5:cout<<"HEAP SORT\n";
s.heap();
break;
default:exit(0);
}
}
}

























****************************************************************
OUTPUT FOR PROGRAM TO DEVELOP A TEMPLATE OF STANDARD SORTING SUCH AS BUBBLESORT,INSERTION SORT,MERGE SORT AND QUICK SORT
****************************************************************
1.BUBBLE SORT
2.INSERTION SORT
3.QUICK SORT
4.MERGE SORT
5.HEAP SORT
6.EXIT
Enter the number of elements:3
Enter the element:1
2
3


Enter the choice:
1
BUBBLE SORT
The sorted element:1 2 3

Enter the choice:
2
INSERION SORT
The sorted element:1 2 3

Enter the choice:
3
QUICK SORT
The sorted element:1 2 3 MERGE SORT
The sorted element:1 2 3

Enter the choice:
4
MERGE SORT
The sorted element:1 2 3

Enter the choice:
5
HEAP SORT
The sorted order is:1 2 3

Enter the choice:
6


****************************************************************
EX.NO:7 TO IMPLEMENT THE INHERITANCE CONCEPT
DATE:16.09.10
****************************************************************

#include<iostream.h>
#include<conio.h>
class base1
{
protected:
int m;
public:
void get_m();
};
class base2
{
protected:
int n;
public:
void get_n();
};
class derived : public base1 , public base2
{public:
void display();};
void base1::get_m()
{int x;
cout<<"ENTER THE X VALUE:";
cin>>x;
m=x;}
void base2::get_n()
{int y;
cout<<"ENTER THE Y VALUE:";
cin>>y;
n=y;}
void derived::display()
{cout<<"BASE1 CLASS X VALUE:"<<m<<"\n";
cout<<"BASE2 CLASS Y VALUE:"<<n<<"\n";
cout<<"DERIVED RESULT OF MULTIPLICATION:"<<m*n<<"\n";
cout<<"DERIVED RESULT OF ADDITITON:"<<m+n<<"\n";}
void main()
{derived a;
clrscr();
a.get_m();
a.get_n();
a.display();
getch();
}
****************************************************************
OUTPUT FOR PROGRAM TO IMPLEMENT THE INHERITANCE CONCEPT
****************************************************************


ENTER THE X VALUE: 5
ENTER THE Y VALUE: 4
BASE1 CLASS X VALUE: 5
BASE2 CLASS Y VALUE: 4
DERIVED RESULT OF MULTIPLICATION: 20
DERIVED RESULT OF ADDITITON: 9




































****************************************************************
EX.NO:7(B) TO IMPLEMENT THE VIRTUAL BASE CLASS
DATE:23.09.10
****************************************************************
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno;
public:
void get_number()
{
int a;
cout<<"\n Enter the roll no:";
cin>>a;
rollno=0;
}
void put_number()
{
cout<<"\n Roll no is:";
}
};
class test:virtual public student
{
protected:
float part1,part2;
public:
void get_marks()
{
float x,y;
cout<<"\n Enter the part1 marks:";
cin>>x;
cout<<"\n Enter the part2 marks:";
part1=x;
part2=y;
}
void put_marks()
{
cout<<"\n marks obtained:";
cout<<"\n Part1:"<<part1;
cout<<"\n Pasrt2:"<<part2;
}
};

class sports:virtual public student
{
protected:
float score;
public:
void get_score()
{
float s;
cout<<"\n Enter the sport score:";
score=s;
}
void put_score()
{
cout<<"\n sports:"<<score;
}
};

class result:public test,public sports
{
float total;
public:
void display();
};

void result::display()
{
total=part1+part2+score;
put_number();
put_marks();
put_score();
cout<<"\n Total score:"<<total;
}

int main()

{
clrscr();
result student1;
student1. get_number();
student1. get_marks();
student1. get_score();
student1.display();
getch();
return 0;
}





****************************************************************
OUTPUT FOR PROGRAM TO IMPLEMENT THE VIRTUAL BASE CLASS
****************************************************************
Enter the roll no:12

Enter the part1 marks:79.8

Enter the part2 marks:85.6

Enter the sports score:86.9

Roll no is :12
marks obtained:
part1:79.800008 part2:85.600003
sports:86.9997TOTAL SCORE :253.800003

































****************************************************************
EX.NO:8(A) TO IMPLEMENT THE QUEUE WITH EXCEPTION HANDLING
DATE:30.09.10
****************************************************************
#include<iostream.h>
#include<iomanip.h>
class queue
{
private:
int *q;
int max, front, rear, cnt;
public:
class FULL
{
};
class EMPTY
{
};
queue(int);
void enqueue(int);
int dequeue(void);
void display(void);
};
queue::queue(int m)
{
q=new int[m];
rear=0;
front=0;
cnt=0;
max=m;
}
void queue::enqueue(int item)
{
if(cnt < max)
{
front=front % max;
q[front ++]=item;
cnt++;
}
else
throw FULL();
}
int queue::dequeue(void)
{
if(cnt > 0)
{
cnt--;
rear=rear%max;
return q[rear++];
}
else
throw EMPTY();
}
void queue::display(void)
{
if(cnt > 0)
for(int i=0,j=rear;i<cnt;i++,j++)
cout<<" "<<q[j%max]<<" ";
else
throw EMPTY();
}
int main()
{
int item,size;
int ch=1;
cout<<"\n ENTER THE SIZE OF THE QUEUE ";
cin>>size;
queue q(size);
cout<<"\n queue operations using exception handling";
cout<<"\n \n \t MENU \n 1. ENQUEUE \n 2. DEQUEUE \n 3. SHOW ENQUEUE \n 4.EXIT";
cout<<"\n enter your choice";
cin>>ch;
do
{
switch(ch)
{
case 1:
cout<<"\n enter the item to insert intop queue:";
cin>>item;
try
{
q.enqueue(item);
}
catch(queue::FULL)
{
cout<<"\n *** QUEUE FULL***\n";
}
break;
case 2:
try
{
cout<<"\n REMOVED ITEM FROM Q IS"<<q.dequeue();
}
catch(queue::EMPTY)
{
cout<<"\n *** QUEUE EMPTY ***\n";
}
break;
case 3:
cout<<"\n the queue is \n";
try
{
q.display();
}
catch(queue::EMPTY)
{
cout<<"\n ***QUEUE EMPTY*** \n";
}
break;
case 4:
break;
}
cout<<"\n ENTER YOUR CHOICE";
cin>>ch;
}
while(ch < 5);
return 0;
}
























****************************************************************************
OUTPUT FOR QUEUE WITH EXCEPTION HANDLING
*****************************************************************************


ENTER THE SIZE OF THE QUEUE :2

queue operations using exception handling

MENU
1. ENQUEUE
2. DEQUEUE
3. SHOW ENQUEUE
4.EXIT
enter your choice1

enter the item to insert intop queue:50

ENTER YOUR CHOICE3

the queue is
50
ENTER YOUR CHOICE2

REMOVED ITEM FROM Q IS50
ENTER YOUR CHOICE3

the queue is

***QUEUE EMPTY***

ENTER YOUR CHOICE:4















****************************************************************
EX.NO:8(B) TO IMPLEMENT THE STACK WITH EXCEPTION HANDLING
DATE:30.09.10
****************************************************************
#include<iostream.h>
class stk
{
int x, top, stack[10];
public:
stk()
{
top=0; }
void push();
class FULL
{
};
class EMPTY
{};
void pop();
void display();
};
void stk::push()
{if(top==10) throw FULL();
// cout<<"\n STACK FULL";
else
top=top+1;
cout<<"\n ENTER THE NUMBER TO BE PUSHED:";
cin>>x;
stack[top]=x;
cout<<"\n THE VALUE IS PUSHED INTO THE STACK IS:"<<x;}
void stk::pop()
{ if(top==0) throw EMPTY();
// cout<<"\n STACK EMPTY\n";
else
{
x=stack[top];
top=top-1;
cout<<"\n THE VALUE RETRIEVED FROM STACK IS:"<<x;}}
void stk::display()
{
cout<<"\n CONTENTS OF STACK:";
for(int i=top;i>0;i--)
{
cout<<"\n"<<stack[i]<<"\t";
}
}
void main()
{
stk s;
int a;
cout<<"\n\t\t\t****\n";
cout<<"\n\t\t\tSTACK\n";
cout<<"\n\t\t\t*****\n";
do
{
cout<<"\n\n\t\t 1.PUSH \n\t\t 2.POP \n\t\t3.DISPLAY \n\t\t 4.EXIT";
cout<<"\n\t\t ENTER THE CHOICE:";
cin>>a;
switch(a)
{
case 1:
try
{
s.push();
}
catch(stk::FULL)
{
cout<<"STACK IS FULL\n";
}
break;
case 2:
try
{
s.pop();
}
catch(stk::EMPTY)
{
cout<<"STACK IS EMPTY";
}
break;
case 3:
try
{
s.display();
}
catch(stk::EMPTY)
{
cout<<"STACK EMPTY";}
break;
default:
cout<<"\n INVALID CHOICE";}}
while(a<4);
cout<<" ";
}
****************************************************************************
OUTPUT FOR STACK WITH EXCEPTION HANDLING
*****************************************************************************
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER THE CHOICE:1
ENTER THE NUMBER TO BE PUSHED:10
THE VALUE IS PUSHED INTO THE STACK IS:10
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER THE CHOICE:1
ENTER THE NUMBER TO BE PUSHED:20
THE VALUE IS PUSHED INTO THE STACK IS:20
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER THE CHOICE:1
ENTER THE NUMBER TO BE PUSHED:30
THE VALUE IS PUSHED INTO THE STACK IS:30
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER THE CHOICE:3
CONTENTS OF STACK:
30 20 10
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER THE CHOICE:2
THE VALUE RETRIEVED FROM STACK IS:30
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER THE CHOICE:3
CONTENTS OF STACK:20 10
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER THE CHOICE:4
****************************************************************
EX.NO:9 TO IMPLEMENT THE SPANNING TREE OF A GRAPH
DATE:07.10.10
****************************************************************

#include<iostream.h>
#include<conio.h>
class kruskal
{
private:
int n; //no of nodes
int noe; //no edges in the graph
int graph_edge[100][4];

int tree[10][10];

int sets[100][10];
int top[100];
public:
void read_graph();
void initialize_span_t();
void sort_edges();
void algorithm();
int find_node(int );
void print_min_span_t();
};

void kruskal::read_graph()
{
cout<<"*************\n";
cout<<"This program implements the kruskal algorithm\n";
cout<<"*************\n";
cout<<"Enter the no. of nodes in the undirected weighted graph ::";
cin>>n;
noe=0;
cout<<"Enter the weights for the following edges ::\n";
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
cout<<" < "<<i<<" , "<<j<<" > ::";
int w;
cin>>w;
if(w!=0)
{
noe++;
graph_edge[noe][1]=i;
graph_edge[noe][2]=j;
graph_edge[noe][3]=w;
}
}
}

// print the graph edges
cout<<"\n\nThe edges in the given graph are::\n";
for(i=1;i<=noe;i++)
cout<<" < "<<graph_edge[i][1]<<" , "<<graph_edge[i][2]<<" > ::"<<graph_edge[i][3]<<endl;
}

void kruskal::sort_edges()
{
/**** Sort the edges using bubble sort in increasing order**************/
for(int i=1;i<=noe-1;i++)
{
for(int j=1;j<=noe-i;j++)
{
if(graph_edge[j][3]>graph_edge[j+1][3])
{
int t=graph_edge[j][1];
graph_edge[j][1]=graph_edge[j+1][1];
graph_edge[j+1][1]=t;
t=graph_edge[j][2];
graph_edge[j][2]=graph_edge[j+1][2];
graph_edge[j+1][2]=t;
t=graph_edge[j][3];
graph_edge[j][3]=graph_edge[j+1][3];
graph_edge[j+1][3]=t;
}
}
}
// print the graph edges
cout<<"\n\nAfter sorting the edges in the given graph are::\n";
for(i=1;i<=noe;i++)
cout<<" < "<<graph_edge[i][1]<<" , "<<graph_edge[i][2]<<" > ::"<<graph_edge[i][3]<<endl;
}

void kruskal::algorithm()
{
// ->make a set for each node
for(int i=1;i<=n;i++)
{
sets[i][1]=i;
top[i]=1;
}
cout<<"\nThe algorithm starts ::\n\n";
for(i=1;i<=noe;i++)
{
int p1=find_node(graph_edge[i][1]);
int p2=find_node(graph_edge[i][2]);
if(p1!=p2)
{
cout<<"The edge included in the tree is ::"<<" < "<<graph_edge[i][1]<<" , "<<graph_edge[i][2]<<" > "<<endl<<endl;

tree[graph_edge[i][1]][graph_edge[i][2]]=graph_edge[i][3];
tree[graph_edge[i][2]][graph_edge[i][1]]=graph_edge[i][3];
// Mix the two sets
for(int j=1;j<=top[p2];j++)
{
top[p1]++;
sets[p1][top[p1]]=sets[p2][j];
}
top[p2]=0;
}
else
{
cout<<"Inclusion of the edge "<<" < "<<graph_edge[i][1]<<" , "<<graph_edge[i][2]<<" > "<<"forms a cycle so it is removed\n\n";
}
}
}

int kruskal::find_node(int n)
{
for(int i=1;i<=noe;i++)
{
for(int j=1;j<=top[i];j++)
{
if(n==sets[i][j])
return i;
}
}
return -1;
}int main()
{
clrscr();
kruskal obj;
obj.read_graph();
obj.sort_edges();
obj.algorithm();
getch();
return 0;
}
****************************************************************
OUTPUT FOR PROGRAM TO IMPLEMENT THE SPANNING OF A GRAPH
****************************************************************
*************
This program implements the kruskal algorithm
*************
Enter the no. of nodes in the undirected weighted graph ::4
Enter the weights for the following edges ::
< 1 , 2 > ::4
< 1 , 3 > ::6
< 1 , 4 > ::9
< 2 , 3 > ::8
< 2 , 4 > ::7
< 3 , 4 > ::5


The edges in the given graph are::
< 1 , 2 > ::4
< 1 , 3 > ::6
< 1 , 4 > ::9
< 2 , 3 > ::8
< 2 , 4 > ::7
< 3 , 4 > ::5


After sorting the edges in the given graph are::
< 1 , 2 > ::4
< 3 , 4 > ::5
< 1 , 3 > ::6
< 2 , 4 > ::7
< 2 , 3 > ::8
< 1 , 4 > ::9

The algorithm starts ::

The edge included in the tree is :: < 1 , 2 >

The edge included in the tree is :: < 3 , 4 >

The edge included in the tree is :: < 1 , 3 >

Inclusion of the edge < 2 , 4 > forms a cycle so it is removed

Inclusion of the edge < 2 , 3 > forms a cycle so it is removed

Inclusion of the edge < 1 , 4 > forms a cycle so it is removed

****************************************************************
EX.NO:10 PROGRAM TO GENERATE THE COMPLEX NUMBERS
DATE:07.10.10
****************************************************************
#include<fstream.h>
#include<conio.h>
#include <time.h>
#include<stdlib.h>

fstream f1,f2;
class Complex
{
friend void main();
friend Complex calc(Complex x,Complex y,char op);
float r,im;
Complex(float r1=0,float im1=0)
{
r=r1;im=im1;
}

void out()
{
cout<<"("<<r<<( (im<0)?"-i":"+i" )<< (im<0?-im:im)<<")";
}

};

Complex calc(Complex x,Complex y,char opr)
{

switch(opr)
{
case'+':
return Complex((x.r+y.r),(x.im+y.im) );
case'-':
return Complex( (x.r -y.r),(x.im -y.im) );
case'*':
return Complex( (x.r*y.r)-(x.im*y.im), (x.r)*(y.im) +(x.im)*(y.r) );

}

};
void main()
{
clrscr();
f1.open("C:\\file1.dat",ios::in | ios::out | ios::trunc);
f2.open("C:\\file2.dat",ios::in | ios::out | ios::trunc);
if(f1==NULL || f2==NULL){ cout<<"\nFile Creation error..";}
f1.seekg(0,ios::end);
char *s1;
time_t t1;
srand((unsigned) time(&t1));
float a=rand()%100,b=rand()%100;
char s,t,op;
f1<<"("<<a<<( (b<0)?"-i":"+i" )<< (b<0?-b:b)<<")";
f1<<"+"; a=rand()%100,b=rand()%100;
f1<<"("<<a<<( (b<0)?"-i":"+i" )<< (b<0?-b:b)<<")\n";
a=rand()%100,b=rand()%100;
f1<<"("<<a<<( (b<0)?"-i":"+i" )<< (b<0?-b:b)<<")";
f1<<"-"; a=rand()%100,b=rand()%100;
f1<<"("<<a<<( (b<0)?"-i":"+i" )<< (b<0?-b:b)<<")\n";
a=rand()%100,b=rand()%100;

f1<<"("<<a<<( (b<0)?"-i":"+i" )<< (b<0?-b:b)<<")";
f1<<"*"; a=rand()%100,b=rand()%100;
f1<<"("<<a<<( (b<0)?"-i":"+i" )<< (b<0?-b:b)<<")\n";
f1.seekg(0,ios::beg);
Complex A,B,C;
do
{
f1>>t>>A.r>>s>>t>>A.im>>t; if(s=='-')A.im=-A.im;
f1>>op;
f1>>t>>B.r>>s>>t>>B.im>>t>>t;if(s=='-')B.im=-B.im;
A.out();cout<<op;B.out();
C=calc(A,B,op);cout<<"=";C.out();
f2<<"("<<C.r<<( (C.im<0)?"-i":"+i" )<< (C.im<0?-C.im:C.im)<<")\n";
cout<<endl;
}
while(!f1.eof());
f1.close();
f2.close();
getch();

}







****************************************************************
OUTPUT FOR PROGRAM TO GENERATE THE COMPLEX NUMBERS
****************************************************************


(70+i40)+(63+i78)=(133+i118)
(90+i63)-(97+i80)=(-7-i17)
(12+i12)*(10+i12)=(120+i144)

















0 comments: