Pages

OOPS LAB MANUAL




om sakthi
ADHIPARASAKTHI ENGINEERING COLLEGE
MELMARUVATHUR

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Description: D:\APEC Data\IN CHARGES\GUEST LECTURE\25 years logo.JPG

LAB MANUAL
FOR
CS 2209 - OBJECT ORIENTED PROGRAMMING LAB




By,    Balamurugan.R
Assistant Professor
Department of Computer Science and Engineering


CONTENT

Ex. No.
Name of the Experiment
1 (A)
Class with Static Data Member
1 (B)
Class with Static Member Function
1 (C)
Function with Default Argument
1 (D)
Class with Friend Function
2 (A)
Implementation of Complex number with Operator Overloading
2 (B)
Implementation of Complex number with Function  Overloading
3
Implementation of class with Dynamic memory allocation
4
Overload the New and Delete Operator with Custom  Dynamic Memory Allocation
5
To develop a Template of Linked List class and its methods
6 (A)
To develop a Template of standard sorting algorithms such as Bubble sort
6 (B)
To develop a Template of standard sorting such as Bubble sort, Merge sort, Insertion sort and Quick sort
7 (A)
To implement the inheritance concept
7 (B)
To implement the Virtual Base Class
8 (A)
To implement the Queue with Exception Handling
8 (B)
To implement the Stack with Exception Handling
9
To implement the Spanning Tree of a Graph
10
Program to Randomly Generate the Complex numbers





**********************************************************************************
EX: NO: 1[A]                          
CLASS WITH STATIC DATA MEMBER
*********************************************************************************

AIM:
To implement a program for static data member of class is shared by all instances of the class.
ALGORITHM:
Step 1:Start the program.
Step 2:Create a class item.
Step 3:Create a static data member as count.
Step 4:Declare the variables and function.           
Step 5:Define the member function to implement the value of count.
Step 6:Define the static data members using scope resolution operator.
Step 7:Create the object for class item.
Step 8:Callthe object for the class item.
Step 9:Display the count value.
Step 10:Stop the program.

//PROGRAM
#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

RESULT:
                Thus the implementation of a program for static data member of class is shared by all instances of the class successfully.


**********************************************************************************
EXP.NO:1[B]
CLASS WITH STATIC MEMBER FUNCTION
**********************************************************************************

AIM:
To implement the static member function in a class.
ALGORITHM:
Step 1:Start the program.
Step 2:Create the class test.
Step 3:Create static data member as count and show count as static member function.
Step 4:Declare the variables and functions.
Step 5:Define the static member function.
Step 6:Increase the scope of count using scope resolution operator.
Step 7:Create objects for class test.
Step 8:Call the member function using the objects.
Step 9:Display the count values.
Step 10:Stop the program.

//PROGRAM
#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

RESULT:
                Thus the implementing  the static member function in a class is successfully verified.









**********************************************************************************
EXP.NO:1[C]         
FUNCTION WITH DEFAULT ARGUMENT
**********************************************************************************

AIM:
                To implement function with default arguments in a class.
ALGORITHM:
Step 1:Start the program.
Step 2:Create the class cube.
Step 3:Declare the variable that are to be used to class are under private sections.
Step 4:Define the constructor with default arguments.
Step 5:Create the objects to the class cube.
Step 6:Declare and define the member function.
Step 7:Call the constructor by a object with parameter and without parameters.
Step 8: perform the operator that are in the function body.
Step 9: print the values of the operations performed in the function body.
Step 10: stop the program.

//PROGRAM
#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


RESULT:
Thus the implementing the function with default arguments is successfully verified.

**********************************************************************************
EXP.NO: 1[D]                     
CLASS WITH FRIEND FUNCTION
**********************************************************************************

AIM:
                Write a c++ program to implement the friend function using class
ALGORITHM:
Step 1: start the program.
 Step 2: create the class distance  and distance 1
Step 3: define the variable in the private section
Step 4: define the constructor.
Step 5: Define the member function.
Step 6:declare the friend function as distanceadd.
Step 7:Declare the variable and function for the class distance 1.
Step 8:Define the distance as friend function.
Step 9:Create the object for the class.
Step 10:Stop the program.

//PROGRAM
#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


















RESULT:
Thus the c++ program to implement complex number in class with necessary function overloading.


**********************************************************************************
EXP.NO:2[A]
IMPLEMENTIOPN OF COMPLEX NUMBER WITH FUNCTION OVERLOADING
*********************************************************************************
AIM:
                To implement complex number in class with necessary function overloading.
ALGORITHM:
Step 1:Start the program.
Step 2:Create the class complex.
Step 3:Declare the variable for a class in private section.
Step 4:Declare the default constructor and define the parameterised constructors.
Step 5:Declare the member function.
Step 6:Declar the member function outside of the class using scope resolution operator.
Step 7:Create the object for the class.
Step 8:Call the member function using the object.
Step 9:Display the result.
Step 10:stop the program.
//PROGRAM
#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





RESULT:
Thus the c++ program to implement complex number with necessary function  overloading is verified successfully.


****************************************************************
EXP.NO:2 [B]
IMPLEMENTATIONCOMPLEX NUMBER WITH OPREATOR OVERLOADING
**********************************************************************************

AIM:
                To implement complex number in class with necessary operator overloading.
ALGORITHM:
Step 1:Start the program.
Step 2:Create the class complex.
Step 3:Declare the variable for a class in private section.
Step 4:Declare the default constructor and define the parameterised constructors.
Step 5:Declare the member function.
Step 6:Declare the variable operator overloading.
Step 7:Define the member function.
Step 8:Create the object for the class.
Step 9:Display the result.
Step 10:stop the program.

//PROGRAM
#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 OPREATOR OVERLOADING
**********************************************************************************
A=1.6+j1.6
B=2.3+j3.5
C=3.9+j5.1

















RESULT:
Thus the c++ program to implement complex number with necessary operator overloading is verified successfully.



**********************************************************************************
EXP.NO:3    
IMPLEMENTATION OF CLASS WITH DYNAMIC MEMORY ALLOCATION
**********************************************************************************

AIM:
To write a c++ program to implement the class with dynamic memory allocation.
ALGORITHM:
Step 1:start the program
Step 2:Create the class matrix
Step 3:Declare the data member in private section
Step 4:Declare and define the member function
Step 5:Declare the constructor , destructor and copy constructor
Step 6:Define the constructor , destructor and copy constructor
Step 7:Create  the object for the class
Step 8:Call the member function using the object
Step 9:Display the result
Step 10:stop the program.

//PROGRAM
#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
RESULT:
                Thus the program for implementation of class with  dynamic memory allocation is verified successfully.

**********************************************************************************
EXP.NO:4                
OVERLOAD THE NEW AND DELETE OPERATOR WITH DYNAMIC MEMORY ALLOCATION
**********************************************************************************
AIM:
                to   Overload the new and delete operator with dynamic memory allocation
Using c++ program.
ALGORITHM:
Step 1: Start the program
Step 2: Create the class as vector
Step 3: Declare the data member in the private section
Step 4: Declare the member function in the public section
Step 5: Define the member function
Step 6: Create the pointer to class vector
Step 7: Allocate the memory space using the new operator
Step 8: Call the member function using object
Step 9: Programming read operator over the class
Step 10: Stop the program
//PROGRAM
#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();
}





**********************************************************************************
Overload the new and delete operator with dynamic memory allocation
**********************************************************************************
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
















RESULT:
                Thus the program to  overload the new and delete operator with dynamic memory allocation.








**********************************************************************************
EX.NO:5                 
TO DEVELOPE A TEMPLATE OF LINKED LIST CLASS AND ITS     METHODS
**********************************************************************************

AIM:
                To write a c++ program to develop a template of linked list class and its method.
ALGORITHM:
Step 1: Start the program
Step 2: Create a class with list
Step 3: Declare the data members and member functions for the class
Step 4: Create a member function for the constructor and initialize the data members
Step 5: Define the template  class of member function outside the class
Step 6: Allocate   memory to the template of the linked list class
Step 7: use the pointer to refer the next the list
Step 8: Perform the input operations to the linked list
Step 9: Display the listed input values using for loop
Step 10: Stop the program.

//PROGRAM
#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

RESULT:
                Thus the c++ program to develop the develop a template of linked list class and its method is verified successfully.


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

AIM:
                To write a c++ program to develop a template of linked list class and its method.
ALGORITHM:
Step 1: start the program
Step 2: Define a template function
Step 3: Define a function to swap the values
Step 4: Define another function as template
Step 5: Using that function call the swap function to perform operations
Step 6: Define the main function
Step 7: Get the values from the user using for loop
Step 8: Call the bubble sort function to perform operations
Step 9: Display the result
Step 10: Stop the program
//PROGRAM
#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




RESULT:
                Thus the program for development of the template of linked list class and its method is verified successfully.


**************************************************************
EX NO:6(B) 
TO DEVELOP A TEMPLATE OF STANDARD SORTING SUCH AS BUBBLESORT,MERGESORT,INSERTION SORT AND QUICK SORT
**************************************************************
AIM:
                To develop a template of standard sorting algorithm such as bubble sort , insertion sort , merge sort and quick sort
ALGORITHM:
Step 1: Start the program
Step 2: Declare and define the template class
Step 3: Declare  the member function , data member .
Step 4: Define the template function outside the class
Step 5: Get the input elements
Step 6: Display the options such as bubble sort , insertion sort , quick sort , merge sort and heap sort
Step 7: Get the choice from the user
Step 8: By using switch simultaneously call the corresponding sorting functions
Step 9: Display the sorted values
Step 10: End the program
//PROGRAM
#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











RESULT:
                Thus the c++ program for the develop a template of standard sorting algorithm such as bubble sort , insertion sort , merge sort and quick sort is verified successfully.

**************************************************************
EX.NO:7
TO IMPLEMENT THE INHERITANCE CONCEPT
**************************************************************
AIM:
                To write a c++ program to implement the inheritance concept using multiple inheritance.
ALGORITHM:
Step 1: start the program
Step 2: Create a base class 1
Step 3: Declare the data members in the protected mode
Step 4: Define the get_m function in the public part
Step 5: Similarly declare the class 2
Step 6: Define the get_n function in the public part
Step 7: Define the derived class
Step 8: Inherit the member of  the class 1&2
Step 9: Perform the operations and display it
Step 10: Stop the program
//PROGRAM
#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








RESULT:
                Thus the program to implement the inheritance concept using multiple inheritance is verified successfully.

**************************************************************
EX.NO:7(B) 
TO IMPLEMENT THE VIRTUAL BASE CLASS
**************************************************************
AIM:
                To write the c++ program to implement the virtual base class.
ALGORITHM:
STEP1 :Start the program.
 STEP 2: Include the header file.
STEP 3: Create the base on student.
STEP 4: Declare and define the data members of the member function of the class.
STEP 5: Create a derived class test.
STEP 6: Declare the necessary data member of the derived class.
STEP 7: Create a derived class as result.
STEP 8: Declare the necessary data member of the class.
STEP 9:Perform the operation and display the result.
STEP 10: Terminate the program.
//PROGRAM
#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

RESULT:
  Thus the c++ program to implement the virtual base class being successfully verified.
*************************************************************
EX.NO:8(A) 
TO IMPLEMENT THE QUEUE WITH EXCEPTION                        HANDLING
**************************************************************
AIM:
                To write a c++ program to implement the queue using exception handling.
ALGORITHM:
STEP 1: Start the program
STEP 2: Include the header file
STEP 3: Create a class queue of stack
STEP 4: Create a class necessary member function of the class
STEP 5: Using he call statement call the necessary member function.
STEP 6: Function are called using function call statement.
STEP 7: Perform the operation.
STEP 8:Display the result.
STEP 10: Terminate the program.
//PROGRAM
#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







RESULT:
                Thus the c++ program to implement the queue class using the exception handling being successfully verified.


*************************************************************
EX.NO:8(B) 
TO IMPLEMENT THE STACK WITH EXCEPTION                        HANDLING
**************************************************************
AIM:
                To write a c+ program to implement the stack class  using exception handling.
ALGORITHM:
STEP 1: Start the program
STEP 2: Include the header files.
STEP 3: Create a class queue.
STEP 4: Declare a throw block inside the de queue  member function.
STEP 5: Declare the another throw block in display function.
STEP 6: Declare then define the try block inside the main function.
STEP 7: Perform the operation.
STEP 8: Print the result.
STEP 10: Terminate the program.
//PROGRAM
#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






RESULT:
    Thus the c++ program to implement the stack class  using the exception handling  being successfully verified.



*************************************************************
EX.NO:9 
TO IMPLEMENT THE SPANNING TREE OF A GRAPH
**************************************************************
AIM:
                To write  a  c++ program for the spanning tree.
ALGORITHM:

STEP 1: Start the program.
STEP 2: Use the kruskal’s algorithm in that given recursion.
STEP 3: Create the class that to be used.
STEP 4: Under the private section the value can be declared.
STEP 5: Set a node to whether create a tree graph.
STEP 6: If any body the nodes from cycle it will be removed from that.
STEP 7: Define the main function.
STEP 8: Display the result.
STEP 9:Stop the program.
//PROGRAM
#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

RESULT:
   Thus the c++ program to implement the spanning  tree of a graph using the kruskal’s algorithm being successfully verified.
**************************************************************
EX.NO:10    
PROGRAM  TO RANDOMLY  GENERATE  THE  COMPLEX NUMBERS
**************************************************************
AIM:
                To implement the program that randomly generate the complex number
ALGORITHM:
STEP 1: Start and create file.
STEP 2: One write to read and another file to write.
STEP 3: Do the operation in one file and write the result in another file.
STEP 4: Display the result before that.
STEP 5: Under the switch operation perform that will asked.
STEP 6: Declare the value in float function.
STEP 7: Using the do while loop function for the file open and file close.
STEP 8: Display the result.
STEP 9: Stop the program.
#include<iostream.h>
#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)



RESULT:
   Thus the c++ program to implement the randomly generating complex number being successfully verified.

0 comments: